Click here to Skip to main content
15,903,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
    private void AddNewRow()
    {
       
        int rowIndex = 0;
        if (ViewState["table1"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["table1"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {

             oreach (System.Data.DataColumn col in dtCurrentTable.Columns) col.ReadOnly = false; 
                    DropDownList ddltype = (DropDownList)gvFormula.Rows[rowIndex].Cells[1].FindControl("ddltype");
                    DropDownList ddlproduct = (DropDownList)gvFormula.Rows[rowIndex].Cells[2].FindControl("ddlproduct");
                    TextBox txtwt = (TextBox)gvFormula.Rows[rowIndex].Cells[3].FindControl("txtWeight");


                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;                    
                    dtCurrentTable.Rows[i - 1]["product"] = ddlproduct.SelectedItem.Text;
                    dtCurrentTable.Rows[i - 1]["weight"]= txtwt.Text;
                    rowIndex++;

                }
               
                dtCurrentTable.Rows.Add(drCurrentRow);

//Error  " Column 'Product' does not allow nulls."

                ViewState["table1"] = dtCurrentTable;
                gvFormula.DataSource = dtCurrentTable;
                gvFormula.DataBind();
               
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        SetPreviousData();
    }


I have gridview when I click add button Iam calling this method . the agenda of this method is to add the new row > But I am Getting the error " Column 'Product' does not allow nulls."
Posted
Updated 24-Sep-13 2:26am
v3
Comments
Thomas ktg 24-Sep-13 8:53am    
ddlproduct.SelectedItem.Text for the column Product is coming null it seems. You check the null or empty value before assigning to the datable.
Bhagavan Raju M 24-Sep-13 9:16am    
I checked with debbuger but I didnt get any null value in the ddlproduct.SelectedItem.text

1 solution

See this link and verify what mistake you made while copying the code.

Dynamically adding and deleting rows from ASP.NET GridView[^]

Regards...
 
Share this answer
 
Comments
Mukesh Madhav 5-Oct-14 4:32am    
Have you found an answer to this ?

Facing the same error now :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900