英文:
i'm experiencing a weird behavior with winform c# timer
问题
I'm trying to update datagridview from sql server database using a timer. First, I got 2 strings
string pervCount = "0";
string newCount = "0";
They are out of my timer; the rest is inside the timer (edit1). Then I'm counting the rows in my SQL table using:
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT COUNT(InvoiceID) FROM InvoicesHeaderTable;", conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
newCount = dr.GetValue(0).ToString();
}
conn.Close();
Then I'm checking:
if (pervCount != newCount)
{
conn.Open();
SqlCommand cmd1 = new SqlCommand("select InvoiceID, CustomerName, TotalInvoicePrice from InvoicesHeaderTable where CompleteStatus=0", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd1);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
CashierDgv.DataSource = dt;
}
conn.Close();
pervCount = newCount;
}
The if statement should not work since pervCount = newCount
since I'm assigning both variables to be equal in the if statement. But somehow my datagridview is constantly refreshed.
英文:
i'm trying to update datagridview from sql server database using a timer. First i got 2 strings
string pervCount = "0";
string newCount = "0";
they are out of my timer, the rest is inside the timer (edit1)
then i'm counting the rows in my sql table using:
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT COUNT(InvoiceID) FROM InvoicesHeaderTable;", conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
newCount = dr.GetValue(0).ToString();
}
conn.Close();
then i'm checking:
if (pervCount != newCount)
{
conn.Open();
SqlCommand cmd1 = new SqlCommand("select InvoiceID, CustomerName,TotalInvoicePrice from InvoicesHeaderTable where CompleteStatus=0", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd1);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
CashierDgv.DataSource = dt;
}
conn.Close();
pervCount = newCount;
}
the if statement should not work since pervCount = newCount since i'm a assigning both variables to be equal int the if statement. But some how my datagridview is constantly refreshed.
答案1
得分: 0
我使用了一个简化的代码,解决了我的问题,实现了我想要的效果。
int pervCount = 0;
在我的定时器外部,然后在我的定时器内部:
SqlCommand cmd = new SqlCommand("SELECT InvoiceID,CustomerName,TotalInvoicePrice FROM InvoicesHeaderTable where CompleteStatus =0", conn);
SqlDataAdapter adab = new SqlDataAdapter(cmd);
DataTable table = new DataTable();
adab.Fill(table);
if (table.Rows.Count > pervCount)
{
CashierDgv.DataSource = table;
pervCount = table.Rows.Count;
}
英文:
i used a simpler code that fixed my problem and did exactly what i want.
int pervCount = 0;
out of my timer, then inside my timer:
SqlCommand cmd = new SqlCommand("SELECT InvoiceID,CustomerName,TotalInvoicePrice FROM InvoicesHeaderTable where CompleteStatus =0", conn);
SqlDataAdapter adab = new SqlDataAdapter(cmd);
DataTable table = new DataTable();
adab.Fill(table);
if (table.Rows.Count > pervCount)
{
CashierDgv.DataSource = table;
pervCount = table.Rows.Count;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论