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
. 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.
DataRow dr = ds_ITEMS.Tables["StkTrx"].NewRow(); 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;
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.
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)