Click here to Skip to main content
15,888,190 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello.
I am developing application with C#.net and sql DB, i am using dataset and adapters for data retrieve. I get dataset as empty if i run in timer or thread but if i run same query in sql server it give me some count of rows. When i run this code outside timer(on page lode or on button click) then it give me expected results
Issue with timer or thread.
I don't know why this is happening.

here 'ds' in empty in below code
tools : vs10, sqlserver 2008 R2

What I have tried:

private void timer_Tick(object sender, EventArgs e)
   {
       SqlConnection con = new SqlConnection("Integrated security=false;Initial Catalog=" + Catalog + ";Data source=" + DataSource + ";User ID=" + DBUserID + ";Password=" + DBPassword + "");
       string sSql = "SELECT * from table ";
       DataSet ds = new DataSet();
       SqlDataAdapter adap = new SqlDataAdapter(sSql, con);
       adap.Fill(ds);
       if (ds.Tables[0].Rows.Count > 0)
       {
       }
   }
Posted
Updated 29-Mar-19 3:55am

1 solution

The test for rows count does not actually do anything; how do you know it returns zero? Also, you are creating a new DatasSet and SqlDataAdapter inside the timer_Tick method. So as soon as that method returns, both objects go out of scope and disappear. Why are you using a timer in the first place?
 
Share this answer
 
Comments
Nitin Surya 29-Mar-19 13:12pm    
The test for rows count does not actually do anything; how do you know it returns zero? : i have not posted the code which is supposed to be done after rowcount > 0 for the sake of simplicity in question, i have just posted the code where i am getting issue...
i am checking in debug mode.

Also, you are creating a new DatasSet and SqlDataAdapter inside the timer_Tick method : yes.. here i will perform one task which is supposed to be performed on regular intervals... so this DataSet and SqlDataAdapter will not be used outside any where in project..
Richard MacCutchan 29-Mar-19 13:54pm    
None of which tells us what results you get in these instances. You need to use your debugger to see what is happening. Do you really need to extract all records each time?
Nitin Surya 2-Apr-19 7:09am    
yes

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