Click here to Skip to main content
16,011,849 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I fix this error when I delete data from Excel sheet?
Object reference not set to an instance of an object.

This is my code:

C#
DataSet ds = new DataSet();
                       conn = new OleDbConnection();
                       conn.ConnectionString = gm.ConnectionString;
                       OleDbDataAdapter da = new OleDbDataAdapter();
                       da.SelectCommand = new OleDbCommand("select * from [ExecutiveMember]", conn);
                       da.Fill(ds);
                       DataRow dtrowdelete = ds.Tables["ExecutiveMember"].Rows[index];
                       
                       dtrowdelete.Delete();
                       ds.Tables["ExecutiveMember"].AcceptChanges();
                       da.Update(ds.Tables["ExecutiveMember"]);
                      MessageBox.Show("Data Deleted!...");
Posted
Updated 24-Aug-11 3:04am
v2

Either ds is null, the table you're reference in ds is null, or the row at the specifid index doesn't exist. You merely have to write code to handle those possible points of failure.
 
Share this answer
 
Comments
Rohit Sharma706 24-Aug-11 9:31am    
thanks for your reply sir.
ds have a value.it is not a null.and index is specified with a value.
ds.Tables["ExecutiveMember"].Rows[index] in this
allowing null value. handle the null value.
 
Share this answer
 
v2
Comments
Rohit Sharma706 24-Aug-11 9:25am    
thanks for your reply.But i send a particular index value over there.(index=37).
Rohit Sharma706 24-Aug-11 23:49pm    
sir, can you tell how to handle null value

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