Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["MyList"];
SPListItem item = list.GetItemById(1);

item["Title"] = TextBox_Name.Text;
item["ProductNumber"] = TextBox_ProdNum.Text;
item["ListPrice"] = TextBox_ListPrice.Text;
item["Color"] = TextBox_Color.Text;
item["More Info"] = TextBox_MoreInfo.Text;

item.SystemUpdate(false);
list.Update();

after writing following code in the list its only replace the first product not if i take more products in the list its only updates the one on place 1... can somebody help me ?

share|improve this question

2 Answers 2

GetItemById takes the Item whose Id is given in the function! so if you hardcode it to 1, it will always take the first List Item!

You should learn basics of SharePoint Object Model, if you want all the Items in the List simply use SPList.Items property to get all the items in the List

You can use SPQuery to get your desired items and use List.GetItems!

Once you run a query you will get SPListItemCollection and you can use foreach with collection to update bulk items!

So it depends upon what you are trying to achieve, get hold of SP Object Model!

share|improve this answer
1  
Just a warning: SPList.Items is executed exerytime it is evaluated see here. There are therefore some things to condider. Like: Prefer SPList.AddItem over SPList.Items.Add, store Items in a variable, etc., etc. –  Markus May 24 at 8:43

You can use a for loop to iterate thought the ID collection which you want to update

Something like below

SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["MyList"];

int ids = { 1, 2, 3, 4, 5 };

for(int id in ids)
{
    SPListItem item = list.GetItemById(id);

    if(item != null)
    {
        item["Title"] = TextBox_Name.Text;
        item["ProductNumber"] = TextBox_ProdNum.Text;
        item["ListPrice"] = TextBox_ListPrice.Text;
        item["Color"] = TextBox_Color.Text;
        item["More Info"] = TextBox_MoreInfo.Text;

        item.SystemUpdate(false);
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.