0

I'm trying to transfer values from a GridView web-control I'm using in a webpart into a SharePoint list, but whenever I click "save" the data isn't being kicked into the list. All that happens is a new item gets created on the list, one that has no name and none of the fields are filled in. What do I do? Been reading on View State, client side vs server side issues, and none of it is working.

when they click the button:

protected void btnSaveAsDraft_Click(object sender, EventArgs e)
    {
        int ID = 0;
        string confirmValue = HttpContext.Current.Request.Form["confirm_value"];
        AddGridRecords(ID);
        SPWeb web = SPContext.Current.Web;


        Page.Response.Redirect(web.Url + "/SitePages/Home.aspx?");
    }

private void AddGridRecords(int ID)
    {
        try
        {
            SPWeb web = SPContext.Current.Web;
            SPList List = web.Lists["FORM D List"];
            web.AllowUnsafeUpdates = true;
            SPListItem NewItem = List.Items.Add();
            NewItem.Update();

            CreateDataTable();

            DataTable tableContext;

            tableContext = CreateDataTable();

            foreach (GridViewRow row in FormDGrid.Rows)
           // for (int count = 0; count < FormDGrid.Rows.Count; count++)
            {                                        
                NewItem["State"] = ((Label)FormDGrid.FindControl("StateField")).Text;
                NewItem["County"] = ((DropDownList)FormDGrid.FindControl("CountyField")).Text;
                NewItem["City or Town"] = ((TextBox)FormDGrid.FindControl("CityField")).Text;
                NewItem.Update();
                List.Update();

            }
        }
        catch (Exception ex)
        {

        }
  • I guess issue might be in code. Share your code here. – P S Feb 21 '18 at 4:29
  • alright. it is there. – prejt Feb 21 '18 at 15:55
  • are you updating all grid rows or do you want only selected row of grid to be updated in the list – Sunil Sahu Feb 22 '18 at 12:39
  • I wanted all grid rows to be reflected as new items in the list with the controls holding in the user's inputs filling in the fields on that list. and I think I've narrowed down the problem. the code is actually unable to find the controls holding the values such as County or City. – prejt Feb 22 '18 at 15:50
2

Try below code

private void AddGridRecords(int ID)
    {
        try
        {
            SPWeb web = SPContext.Current.Web;
            SPList List = web.Lists["FORM D List"];
            web.AllowUnsafeUpdates = true;

            CreateDataTable();

            DataTable tableContext;

            tableContext = CreateDataTable();

            foreach (GridViewRow row in FormDGrid.Rows)
            {
                SPListItem NewItem = List.Items.Add();
                NewItem["State"] = ((Label)FormDGrid.FindControl("StateField")).Text;
                NewItem["County"] = ((DropDownList)FormDGrid.FindControl("CountyField")).Text;
                NewItem["City or Town"] = ((TextBox)FormDGrid.FindControl("CityField")).Text;
                NewItem.Update();                

            }
        web.AllowUnsafeUpdates = false;
        }
        catch (Exception ex)
        {

        }
}

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.