Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys, i'm using tick function to show updated data from database to datatable. Now, the problem is my current database is null, data will be insert by user later. When i'm run the program it's showing error "object reference not set to an instance", now how can i make sure the tick function is running without throwing the error?

What I have tried:

C#
private void timer_Tick (object sender, EventArgs e)
{
   this.tbl_StoredDataTableAdapter.Fill(this.gpdbDataSet.tbl_StoredData);
}


Please help me. Thank you!
Posted
Updated 13-Mar-18 23:20pm
Comments
F-ES Sitecore 14-Mar-18 5:21am    
First you need to find out what in that code is null, it could be "this", "tbl_StoredDataTableAdapter" or "gpdbDataSet". We can't run your code for you so we don't know. Next you need to decide what you want to happen when that object is null. Again we can't read your mind so we don't know. If you simply want to "do nothing" then at the start of the Tick event check if the relevant object is null and if it is just "return;"

1 solution

Simple: check for nulls.
private void timer_Tick (object sender, EventArgs e)
   {
   if (tbl_StoredDataTableAdapter != null && gpdbDataSet != null && gpdbDataSet.tbl_StoredData != null)
      {
      this.tbl_StoredDataTableAdapter.Fill(this.gpdbDataSet.tbl_StoredData);
      }
   }
 
Share this answer
 
Comments
Member 13485822 14-Mar-18 5:25am    
Thank you very much!
OriginalGriff 14-Mar-18 5:26am    
You're welcome!

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