locked
how to save data on click of custom button of a user contreol form in MOSS 2007 portal RRS feed

  • Question

  • i have created a customized form want to save my data how to save (like sharepoint default behave) as we save data in a custom list we navigate to all item.aspx page and also can see our data rowwise..................

    Please let me know what code shud i write.


    sudhanshu sharma Do good and cast it into river :)

    Thursday, March 29, 2012 12:44 PM

Answers

  •  

    Hi sudhanshuSharma,

    You can do it as the following. But you will add reference Microsoft.SharePoint.dll in your project. The file is in “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN”

            protected void Button1_Click(object sender, EventArgs e)
            {
                    SPSite site = new SPSite("http://jack-7716f30e37:2012/personal/Test/");
                    using (SPWeb Web = site.OpenWeb())
                    {
                        site.AllowUnsafeUpdates = true;
                        Web.AllowUnsafeUpdates = true;
                        SPList list = Web.Lists["A"];//get your list here
                        SPListItem Item = list.Items.Add();
                        Item["Title"] = "test add an item";
                        Item.Update();
                    }
            }

    Thanks,

    Jack




    Friday, March 30, 2012 5:41 AM
    Moderator

All replies

  •  

    Hi sudhanshuSharma,

    You can do it as the following. But you will add reference Microsoft.SharePoint.dll in your project. The file is in “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN”

            protected void Button1_Click(object sender, EventArgs e)
            {
                    SPSite site = new SPSite("http://jack-7716f30e37:2012/personal/Test/");
                    using (SPWeb Web = site.OpenWeb())
                    {
                        site.AllowUnsafeUpdates = true;
                        Web.AllowUnsafeUpdates = true;
                        SPList list = Web.Lists["A"];//get your list here
                        SPListItem Item = list.Items.Add();
                        Item["Title"] = "test add an item";
                        Item.Update();
                    }
            }

    Thanks,

    Jack




    Friday, March 30, 2012 5:41 AM
    Moderator
  • HI Jack,

    I am getting unexpected error occured  my code is-

     SPSite site = new SPSite("http://172.20.14.11:44/IS/Lists/");
                       {
                           using (SPWeb Web = site.OpenWeb())
                            {
                                site.AllowUnsafeUpdates = true;
                                Web.AllowUnsafeUpdates = true;
                                SPList list = Web.Lists["RPM_Trail"];//get your list here
                                SPListItem Item = list.Items.Add();
                                //Item["Title"] = "test add an item";
                                Item["RSOR Number"] = RSORDropDownList1.SelectedValue.ToString();
                                Item["RPS Number"] = RPSNumberTextBox1.Text;
                                Item.Update();
                                list.Update(); 
                                Response.Redirect("http://172.20.14.11:44/IS/Lists/RPM_Trail/AllItems.aspx");
                               
                            }
                       }


    sudhanshu sharma Do good and cast it into river :)


    Friday, March 30, 2012 8:31 AM
  •  

    Hi sudhanshuSharma,

    Could you please put your error details? This will help me to fix it. I find your code like the following .

    Item["RSOR Number"] ……

    If you have a blank white space in it, you shouldn’t do it.

    First, you should create a column called “RSORNumber”, you can’t add a blank white space in it.

    Second, rename the column as “RSOR Number”, this time you can add a blank white space in it.

    So, after these, you can do it as the following code.

    Item["RSORNumber"] ……

    Just do it as the above, it will be no error.

    Thanks,

    Jack


    Monday, April 2, 2012 1:47 AM
    Moderator
  • HI Jack,

    See now i have changed columns without space

     protected void SaveButton1_Click(object sender, EventArgs e)
               {
                   
                  
                       SPSite site = new SPSite("http://172.20.14.16:4444/IS/Lists/");
                       {
                           using (SPWeb Web = site.OpenWeb())
                           {
                               site.AllowUnsafeUpdates = true;
                               Web.AllowUnsafeUpdates = true;
                               SPList list = Web.Lists["RPM_Trail"];//get your list here
                               SPListItem Item = list.Items.Add();
                               //Item["Title"] = "test add an item";
                               Item["RSORNumber"] = RSORDropDownList1.SelectedValue.ToString();
                               Item["RPSNumber"] = RPSNumberTextBox1.Text;
                               Item.Update();
                               list.Update();
                               Response.Redirect("http:/172.20.14.16:4444/IS/Lists/RPM_Trial/SavePage.aspx");
                              

                               
                            }
                       }
         



                    }

    As i click on save i dont navigate in another page  rather i go to same page http://172.20.14.16:4444/IS/Lists/RPM_Trial/NewForm.aspx and it diplay an unexpected error has occured....

    I also comment all code only i checked i am navigating or no..so this below  line -

    Response.Redirect("http:/172.20.14.16:4444/IS/Lists/RPM_Trial/AllItem.aspx");

    It was fine but am not able to add....


    sudhanshu sharma Do good and cast it into river :)

    Monday, April 2, 2012 6:30 AM
  • Hi sudhanshu,

    Are you sure this error is caused by "Response.Redirect("http:/172.20.14.16:4444/IS/Lists/RPM_Trial/AllItem.aspx");"? So, could you use the another address replace the IP address.

    When I use "Response.Redirect("http://jack-7716f30e37:2012/personal/Test/Lists/A/AllItems.aspx");"  My machine  is ok.

    So, you can debug your project with "Response.Redirect("http://www.google.com"); if the problem still, please put some detail error about it or some screenshot.

    Jack

    Thanks,
    Monday, April 2, 2012 6:56 AM
    Moderator
  • I got output thanks for your help....actualy i was getting error bcoz of some required field validator now i removed it and got out put ...your given code was perfect thanks for your help ..

    But i am surprised as i insert required field validator for my 1 field my web part shows error......

    Anyways many thanks


    sudhanshu sharma Do good and cast it into river :)

    Monday, April 2, 2012 10:25 AM