0

Solved it with a hiddenfield control:

    public void MyData(string prod)
    {
        SPWeb web = SPContext.Current.Web;
        SPList myList = web.Lists.TryGetList("YourList");

        if (myList!=null)
        {
            SPQuery query = new SPQuery();
            query.Query = string.Format(
                "<Where><BeginsWith><FieldRef Name='ProductNumber' />" +
                "<Value Type='Text'>{0}</Value></BeginsWith></Where>", prod);
            query.RowLimit = 10;

            SPListItemCollection items = myList.GetItems(query);

            foreach (SPListItem listItem in items)
            {
                TextBox_NameList.Text = listItem["Title"] != null ? listItem["Title"].ToString() : string.Empty;
                TextBox_ListPriceList.Text = listItem["ListPrice"] != null ? listItem["ListPrice"].ToString() : string.Empty;
                TextBox_ProdNumList.Text = listItem["ProductNumber"] != null ? listItem["ProductNumber"].ToString() : string.Empty;
                TextBox_ColorList.Text = listItem["Color"] != null ? listItem["Color"].ToString() : string.Empty;
                TextBox_MoreInformationList.Text = listItem["MoreInformation"] != null ? listItem["MoreInformation"].ToString() : string.Empty;
                HiddenField_ID.Value = listItem["ID"].ToString();
            }
        }
    }

My button code:

        protected void Button_UpdateProd_Click(object sender, EventArgs e)
    {
        SPWeb web = SPContext.Current.Web;
         //web.AllowUnsafeUpdates = true;

        SPList myList = web.Lists["Extern Products"];
        int listItemId = Convert.ToInt32(HiddenField_ID.Value);

        //Considering you have the ID of the List Item
         SPListItem itemToUpdate = myList.GetItemById(listItemId);

        if(itemToUpdate != null)
        {
            itemToUpdate["Title"] = TextBox_NameList.Text;
            itemToUpdate["ProductNumber"] = TextBox_ProdNumList.Text;
            itemToUpdate["Color"] = TextBox_ColorList.Text;
            itemToUpdate["ListPrice"] = TextBox_ListPriceList.Text;
            itemToUpdate["MoreInformation"] = TextBox_MoreInformationList.Text;

            itemToUpdate.Update();

            TextBox_NameList.Text = string.Empty;
            TextBox_ListPriceList.Text = string.Empty;
            TextBox_ProdNumList.Text = string.Empty;
            TextBox_ColorList.Text = string.Empty;
            TextBox_MoreInformationList.Text = string.Empty;
       }
    }

Hope it helps.

4
  • Are you saving List Item Id somewhere in third web part ? You need to save the ID column somewhere on selection! Dec 3, 2012 at 9:16
  • Please edit your question instead of adding comments.
    – Steve B
    Dec 3, 2012 at 10:23
  • Tick mark the answer below!! Dec 3, 2012 at 10:36
  • I cant within two days it says... So i've putted my answer above
    – krisal
    Dec 3, 2012 at 10:38

1 Answer 1

1

You don't need to save the ID, its automatically generated for you! "ID" is a read-only column, and it gets auto-increment each time you add a new item!

Where you have all the textboxes, take a HiddenField there, name it accordingly!

public void MyData(string prod)
{
    SPWeb web = SPContext.Current.Web;
    SPList myList = web.Lists.TryGetList("YourList");

    if (myList!=null)
    {
        SPQuery query = new SPQuery();
        query.Query = string.Format(
            "<Where><BeginsWith><FieldRef Name='ProductNumber' />" +
            "<Value Type='Text'>{0}</Value></BeginsWith></Where>", prod);
        query.RowLimit = 10;

        SPListItemCollection items = myList.GetItems(query);

        foreach (SPListItem listItem in items)
        {
            TextBox_NameList.Text = listItem["Title"] != null ? listItem["Title"].ToString() : string.Empty;
            TextBox_ListPriceList.Text = listItem["ListPrice"] != null ? listItem["ListPrice"].ToString() : string.Empty;
            TextBox_ProdNumList.Text = listItem["ProductNumber"] != null ? listItem["ProductNumber"].ToString() : string.Empty;
            TextBox_ColorList.Text = listItem["Color"] != null ? listItem["Color"].ToString() : string.Empty;
            TextBox_MoreInformationList.Text = listItem["MoreInformation"] != null ? listItem["MoreInformation"].ToString() : string.Empty;
            HiddenField_ID.Value = listItem["ID"].ToString();
        }
    }
}

Then on Update button click

SPWeb web = SPContext.Current.Web;
    //web.AllowUnsafeUpdates = true;

 SPList myList = web.Lists["Extern Products"];
 int listItemId = Convert.ToInt32(HiddenField.Value);

 //Considering you have the ID of the List Item
 SPListItem itemToUpdate = myList.GetItemById(listItemId);

    if(itemToUpdate != null)
    {

            listItem["Title"] = TextBox_NameList.Text;
            listItem["ProductNumber"] = TextBox_ProdNumList.Text;
            listItem["Color"] = TextBox_ColorList.Text;
            listItem["ListPrice"] = TextBox_ListPriceList.Text;
            listItem["MoreInformation"] = TextBox_MoreInformationList.Text;

            listItem.Update();

            TextBox_NameList.Text = string.Empty;
            TextBox_ListPriceList.Text = string.Empty;
            TextBox_ProdNumList.Text = string.Empty;
            TextBox_ColorList.Text = string.Empty;
            TextBox_MoreInformationList.Text = string.Empty;

   }

For delete simply use listItem.Delete(); instead of listItem.Update();

You don't need SPQuery in this case, since it is one Item you want to update and you have the ID for it!

Extra Information on SPQuery

Here is list of all operations available in CAML Query
Query Schema

For ease in CAML Queries, you can try one of the CAML Query Builders:
http://spcamlqueryhelper.codeplex.com/
http://www.u2u.net/res/Tools/CamlQueryBuilder.aspx

I hope this helps

2
  • If works, please delete all the comments, add complete code in Question and your description in Question too! - for delete simply use listItem.Delete(); instead of listItem.Update(); Dec 3, 2012 at 10:27
  • Also, mark the answer as correct.. and close all your previous questions by marking as answer! Also ask your partner Martin to close the questions too! Dec 3, 2012 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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