|
 |
|
C# ตั้งเวลา Timer แล้ว Error ตามรูป ไม่รู้ว่าผิดตรงไหน |
|
 |
|
|
 |
 |
|
คือผมเขียนโปรแกรมแล้วต้องการให้ Timer มันเช็คตลอดเวลาว่ามี Record ใหม่เข้ามารึยัง ถ้ามีให้ รีเฟชร
ตัวคิวรี่ทุกอย่างถูกหมดแล้ว
แต่ติดตรงที่ว่า พอเปิดหน้าจอทิ้งไว้ประมาณ 10 นาที มันก็ Error ตามรูป
ไม่รู้ว่าผิดตรงไหน

Code (C#)
private void timer1_Tick(object sender, EventArgs e)
{
string strConn;
strConn = DBConnString.strConn;
Conn = new SqlConnection();
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();
string sqlShow;
sqlShow = " SELECT C_Time ";
sqlShow += " FROM iUserCard INNER JOIN tEnter ON substring(iUserCard.C_CardNum,1,50) = substring(tEnter.C_Card,1,50) ";
sqlShow += " INNER JOIN tUser ON iUserCard.L_UID = tUser.L_ID ";
sqlShow += " WHERE ( SUBSTRING(tUser.C_Unique,1,50) IN ( SELECT SUBSTRING(Student_ID,1,50)";
sqlShow += " FROM tTAD_RegisStudent ";
sqlShow += " WHERE Subject_Code = '" + txtSubCode.Text.Trim() + "' AND Semester = '" + txtSemester.Text.Trim() + "' ) ";
sqlShow += " AND ( L_TID = '" + txtTerminal.Text + "' ) ";
sqlShow += " AND ( C_Date = '" + DateNow + "' )";
sqlShow += " AND ( C_Time >= '" + txtTimeIN.Text.Substring(0, 2) + txtTimeIN.Text.Substring(3, 2) + "00" + "' ) ";
sqlShow += " AND ( scb_flag = 0 ) )";
com = new SqlCommand();
com.CommandType = CommandType.Text;
com.CommandText = sqlShow;
com.Connection = Conn;
SqlDataReader dr = com.ExecuteReader();
if (dr.HasRows)
{
ShowDataInOut();
dgvComeIN.Rows.Clear();
ShowClassInOut();
ShowStudentNoPresent();
}
dr.Close();
}
Tag : .NET, Ms SQL Server 2005, Crystal Report, C#

|
ประวัติการแก้ไข 2012-10-10 21:28:34
|
 |
 |
 |
 |
Date :
2012-10-10 21:25:11 |
By :
SeedNew |
View :
1469 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
private void timer1_Tick(object sender, EventArgs e)
{
try{
string strConn;
strConn = DBConnString.strConn;
Conn = new SqlConnection();
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();
string sqlShow;
sqlShow = " SELECT C_Time ";
sqlShow += " FROM iUserCard INNER JOIN tEnter ON substring(iUserCard.C_CardNum,1,50) = substring(tEnter.C_Card,1,50) ";
sqlShow += " INNER JOIN tUser ON iUserCard.L_UID = tUser.L_ID ";
sqlShow += " WHERE ( SUBSTRING(tUser.C_Unique,1,50) IN ( SELECT SUBSTRING(Student_ID,1,50)";
sqlShow += " FROM tTAD_RegisStudent ";
sqlShow += " WHERE Subject_Code = '" + txtSubCode.Text.Trim() + "' AND Semester = '" + txtSemester.Text.Trim() + "' ) ";
sqlShow += " AND ( L_TID = '" + txtTerminal.Text + "' ) ";
sqlShow += " AND ( C_Date = '" + DateNow + "' )";
sqlShow += " AND ( C_Time >= '" + txtTimeIN.Text.Substring(0, 2) + txtTimeIN.Text.Substring(3, 2) + "00" + "' ) ";
sqlShow += " AND ( scb_flag = 0 ) )";
com = new SqlCommand();
com.CommandType = CommandType.Text;
com.CommandText = sqlShow;
com.Connection = Conn;
SqlDataReader dr = com.ExecuteReader();
if (dr.HasRows)
{
ShowDataInOut();
dgvComeIN.Rows.Clear();
ShowClassInOut();
ShowStudentNoPresent();
}
}catch(exception ex){
messagebox.show(ex.tostring());
}finally{
dr.Close();
}
}
|
 |
 |
 |
 |
Date :
2012-10-11 00:08:06 |
By :
dekkuza |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ได้เหมือนเดิมครับ โค้ดที่ให้มามันแค่เช็คว่า Error อะไรนิครับ
ลองแล้วครับ พอเปิดโปรแกรมทิ้งไว้ 10-15 นาที ก็ขึ้น messagebox ตามรูปด้านล่าง

|
 |
 |
 |
 |
Date :
2012-10-11 14:48:53 |
By :
SeedNew |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วเหมือนว่าโปรแกรม มัน Conect บ่อยๆ ก็ Errors ดังรูป
คราวนี้ลองย้ายโค้ด
Code (C#)
string strConn;
strConn = DBConnString.strConn;
Conn = new SqlConnection();
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();
ไปไว้ใน Form Load ก็ได้ ไม่มี Errors
เหลือใน Timer
Code (C#)
string sqlShow;
sqlShow = " SELECT C_Time ";
sqlShow += " FROM iUserCard INNER JOIN tEnter ON substring(iUserCard.C_CardNum,1,50) = substring(tEnter.C_Card,1,50) ";
sqlShow += " INNER JOIN tUser ON iUserCard.L_UID = tUser.L_ID ";
sqlShow += " WHERE ( SUBSTRING(tUser.C_Unique,1,50) IN ( SELECT SUBSTRING(Student_ID,1,50)";
sqlShow += " FROM tTAD_RegisStudent ";
sqlShow += " WHERE Subject_Code = '" + txtSubCode.Text.Trim() + "' AND Semester = '" + txtSemester.Text.Trim() + "' ) ";
sqlShow += " AND ( L_TID = '" + txtTerminal.Text + "' ) ";
sqlShow += " AND ( C_Date = '" + DateNow + "' )";
sqlShow += " AND ( C_Time >= '" + txtTimeIN.Text.Substring(0, 2) + txtTimeIN.Text.Substring(3, 2) + "00" + "' ) ";
sqlShow += " AND ( scb_flag = 0 ) )";
com = new SqlCommand();
com.CommandType = CommandType.Text;
com.CommandText = sqlShow;
com.Connection = Conn;
SqlDataReader dr = com.ExecuteReader();
if (dr.HasRows)
{
ShowDataInOut();
dgvComeIN.Rows.Clear();
ShowClassInOut();
ShowStudentNoPresent();
}
dr.Close();
ลองเปิดทิ้งไว้ 1 ชม ไม่มี Errors
|
 |
 |
 |
 |
Date :
2012-10-13 00:12:25 |
By :
SeedNew |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณลืม Conn.Close() หรือเปล่าครับถ้าเปิด Connection มากๆ โดยไม่ปิด Connection ก็น่าจะเต็มนะครับ
|
 |
 |
 |
 |
Date :
2012-10-13 11:36:06 |
By :
pStudio |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|