Click here to Skip to main content
Sign Up to vote bad
good
See more: C#C#4.0
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
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.
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 14 Dec '12 - 3:02
Edited 14 Dec '12 - 3:26


2 solutions

  Permalink  
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.
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 8,376
1 OriginalGriff 6,571
2 CPallini 3,533
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 14 Dec 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid