Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i use below code to add new row to my datagridview which has a dataset as datasource.it gives an error saying "Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound" when i use
C#
datagridview1.Rows.Add()
. So i searched on internet found below solution.But now it gives an NullReferenceException saying "Object reference not set to an instance of an object."


// ds_ITEMS is my dataset name.
C#
DataRow dr = ds_ITEMS.Tables["StkTrx"].NewRow(); // NullReferenceException gives in this line
            dr["Code"] = this.txtCode.Text;
            dr["ItemDes"] = this.txtDes.Text;
            dr["Qty"] = this.txtQty.Text;
            dr["UnitPrice"] = this.txtCostPrice.Text;
            dr["Amount"] = this.txtAmount.Text;

            ds_ITEMS.Tables["StkTrx"].Rows.Add(dr);


Any suggestions?
Posted
Updated 14-Dec-12 3:26am
v3

Please take a reference from here :
Dynamically Adding and Deleting rows from ASP.NET Gridview [^]
 
Share this answer
 
The null reference exception is because the dataSet'ds_ITEMS' is not loaded with any tables. The ds_ITEMS.Tables.Count might be zero and there is no such table with the name 'StkTrx".

Double check your dataSet preparation is done as expected and if you are using database queries to fill up the dataset check the sql statement and the table name it returns matches the name StkTrx.
 
Share this answer
 

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