มันขึ้น error แบบนี้อ่ะคะ เป็น web app ใช้ visual studio2008 กับ my sql
There is already an open DataReader associated with this Connection which must be closed first.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: MySql.Data.MySqlClient.MySqlException: There is already an open DataReader associated with this Connection which must be closed first.
อย่าลืม ปิด ก่อนนะครับ ก่อน ที่จะ สั่งเปิด connection อีกครัง
เช่น หลังจากได้ ข้อมูลจาก data base มาแล้ว ต้องปิด data reader นะครับ
Code (VB.NET)
Dim objConn As System.Data.OleDb.OleDbConnection
Dim objCmd As System.Data.OleDb.OleDbCommand
Dim strConnString As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";Jet OLEDB:Database Password=;"
objConn = New System.Data.OleDb.OleDbConnection(strConnString)
objConn.Open()
Dim strSQL As String
strSQL = "SELECT * FROM customer"
Dim dtReader As System.Data.OleDb.OleDbDataReader
objCmd = New System.Data.OleDb.OleDbCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
'*** DataReader ***'
dtReader.Close()
dtReader = Nothing
objConn.Close()
objConn = Nothing
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ConnString = "Server=192.168.10.88;database=pt;UID=root;"
Dim Conn = New MySqlConnection(ConnString)
Conn.Open()
Dim dtAdapter As New MySqlDataAdapter
Dim ds As New Data.DataSet
Dim t1 = TextBox1.Text & " 00:00:00 "
Dim t2 = TextBox2.Text & " 00:00:00 "
Dim SQL = "select ecode,em_name,em_name2,indate,shift,time0,time1,time2, time3, time4, time5, time6, time7, tin, tbout, tbin, tout from shifttime,emper where(substring(em_code, 1, 7) = ecode) and indate between 't1' and 't2' and ebranch = '" & TextBox3.Text & "' order by indate,ecode "
Dim Cmd = New MySqlCommand(SQL, Conn)
Dim dtreader = Cmd.ExecuteReader()
dtAdapter.SelectCommand = Cmd 'Adapter เป็นตัวเชื่อมระหว่างฐานข้อมูลกับตัวระบบ
dtAdapter.Fill(ds) 'เป็นการรันsql
GridView1.DataSource = ds
GridView1.DataBind()
dtreader.Close()
dtreader = Nothing
End Sub
End Class
There is already an open DataReader associated with this Connection which must be closed first.
ไม่เคยใช้ class mysqlclient ซะด้วย แต่เดาว่าคงเหมือนกับ sqlclient
Code (VB.NET)
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ConnString = "Server=192.168.10.88;database=pt;UID=root;"
Dim Conn = New MySqlConnection(ConnString)
If Conn.State = ConnectionState.Open Then
Conn.Close()
End If
Conn.Open()
Dim dtAdapter As New MySqlDataAdapter
Dim ds As New Data.DataSet
Dim t1 = TextBox1.Text & " 00:00:00 "
Dim t2 = TextBox2.Text & " 00:00:00 "
Dim SQL = "select ecode,em_name,em_name2,indate,shift,time0,time1,time2, time3, time4, time5, time6, time7, tin, tbout, tbin, tout from shifttime,emper where(substring(em_code, 1, 7) = ecode) and indate between 't1' and 't2' and ebranch = '" & TextBox3.Text & "' order by indate,ecode "
Dim Cmd = New MySqlCommand(SQL, Conn)
Dim dtreader = Cmd.ExecuteReader()
dtAdapter.SelectCommand = Cmd 'Adapter เป็นตัวเชื่อมระหว่างฐานข้อมูลกับตัวระบบ
dtAdapter.Fill(ds) 'เป็นการรันsql
GridView1.DataSource = ds
GridView1.DataBind()
dtreader.Close()
dtreader = Nothing
End Sub
End Class
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ConnString = "Server=192.168.10.88;database=pt;UID=root;"
Dim Conn = New MySqlConnection(ConnString)
If Conn.State = ConnectionState.Open Then
Conn.Close()
End If
Conn.Open()
Dim ds As New Data.DataSet
Dim t1 = TextBox1.Text & " 00:00:00 "
Dim t2 = TextBox2.Text & " 00:00:00 "
Dim SQL = "select ecode,em_name,em_name2,indate,shift,time0,time1,time2, time3, time4, time5, time6, time7, tin, tbout, tbin, tout from shifttime,emper where(substring(em_code, 1, 7) = ecode) and indate between 't1' and 't2' and ebranch = '" & TextBox3.Text & "' order by indate,ecode "
Dim Cmd = New MySqlCommand(SQL, Conn)
Dim dtreader = Cmd.ExecuteReader()
GridView1.DataSource = dtreader
GridView1.DataBind()
dtreader.Close()
dtreader = Nothing
End Sub
End Class
แบบ connectionless
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ConnString = "Server=192.168.10.88;database=pt;UID=root;"
Dim Conn = New MySqlConnection(ConnString)
Dim dtAdapter As New MySqlDataAdapter
Dim ds As New Data.DataSet
Dim t1 = TextBox1.Text & " 00:00:00 "
Dim t2 = TextBox2.Text & " 00:00:00 "
Dim SQL = "select ecode,em_name,em_name2,indate,shift,time0,time1,time2, time3, time4, time5, time6, time7, tin, tbout, tbin, tout from shifttime,emper where(substring(em_code, 1, 7) = ecode) and indate between 't1' and 't2' and ebranch = '" & TextBox3.Text & "' order by indate,ecode "
Dim Cmd = New MySqlCommand(SQL, Conn)
dtAdapter.SelectCommand = Cmd 'Adapter เป็นตัวเชื่อมระหว่างฐานข้อมูลกับตัวระบบ
dtAdapter.Fill(ds) 'เป็นการรันsql
GridView1.DataSource = ds
GridView1.DataBind()
End Sub
End Class