HOME > .NET Framework > Forum > รบกวนพี่ๆนิดหนึ่งครับ Error Code "there is already an open datareader associated with this command which must be closed first "
รบกวนพี่ๆนิดหนึ่งครับ Error Code "there is already an open datareader associated with this command which must be closed first "
Imports System.Data.SqlClient
Public Class FrmViewInven
Public tmp, Time As String
Dim adt As SqlDataAdapter
Dim dt As DataTable
Dim ds As DataSet
Private Sub FrmViewInven_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
With myConn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strconn
.Open()
End With
Connect()
myConn.Open()
liv()
Catch ex As Exception
MsgBox(ex.Message)
Finally
myConn.Close()
End Try
End Sub
Private Sub BtnView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnView.Click
Try
With myConn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strconn
.Open()
End With
myConn.Open()
If livInvo.SelectedItems.Count = 0 Then
MessageBox.Show("กรุณาเลือกใบเบิกพัสดุที่ต้องการ !!!", "ผลการเลิกใบเบิกพัสดุ", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Else
'****************************
'เป็นการเลือกที่ liv แล้วเปิดเอกสารที่ต้องการ
'****************************
Dim A As Form = New FrmAppWithDraw
A.Show()
' Show(FrmAppWithDraw)
End If
Catch ex As Exception
MsgBox(ex.Message, "Error2")
Finally
myConn.Close()
End Try
End Sub
Sub liv()
Try
sql = "SELECT WithDrawInvo.Withdraw_id, WithDrawInvo.DateWithDraw, WithDrawInvo.UserPK, WithDrawInvo.ActiveTF, " & _
"WithDrawInvo.TotalMoney, WithDrawInvo.ActiveBoss " & _
"FROM WithDrawInvo INNER JOIN " & _
" DbMember ON WithDrawInvo.UserPK = DbMember.UserPK " & _
"WHERE (WithDrawInvo.ActiveBoss = 0) AND (DbMember.Division_id = '" & DivisionID & "')"
' Dim adt As SqlDataAdapter = New SqlDataAdapter("select * from WithDrawInvo where Activeboss= 0 and division_id='" & DivisionID & "' ", myConn)
Dim adt As SqlDataAdapter = New SqlDataAdapter(sql, myConn)
Dim ds As DataSet = New DataSet
adt.Fill(ds, "View")
Dim dt As DataTable = New DataTable
dt = ds.Tables("View")
For i = 0 To dt.Rows.Count - 1 Step 1
Dim lvi = New ListViewItem(dt.Rows(i).Item(0).ToString) 'ID
'lbNo.Text = dt.Rows(i).Item(0).ToString
lvi.SubItems.Add(Format(CDate(dt.Rows(i).Item(1).ToString), "dd/MM/yyyy"))
lvi.SubItems.Add(dt.Rows(i).Item(2).ToString)
lvi.SubItems.Add(dt.Rows(i).Item(3).ToString)
lvi.SubItems.Add(dt.Rows(i).Item(4).ToString)
livInvo.Items.Add(lvi)
Next
livInvo.Sort()
Catch ex As Exception
MsgBox(ex.Message, "Error1")
Finally
myConn.Close()
End Try
End Sub
Private Sub livInvo_ItemSelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles livInvo.ItemSelectionChanged
With myConn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strconn
.Open()
End With
adt = New SqlDataAdapter("select * from WithDrawInvo", myConn)
ds = New DataSet
adt.Fill(ds, "View")
dt = New DataTable
dt = ds.Tables("View")
For i = 0 To livInvo.SelectedItems.Count - 1 Step 1
'lbNo.Text = livInvo.SelectedItems.Item(i).SubItems(0).Text
tmp = livInvo.SelectedItems.Item(i).SubItems(0).Text
Time = livInvo.SelectedItems.Item(i).SubItems(1).Text
Name = livInvo.SelectedItems.Item(i).SubItems(2).Text
Next
End Sub
Private Sub livInvo_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles livInvo.DoubleClick
BtnView_Click(sender, e)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
livInvo.Items.Clear()
FrmViewInven_Load(sender, e)
End Sub
End Class
รบกวนหน่อยค่ะ คือว่า มีขึ้น Error there is already an open datareader associated with this command which must be closed first
ต้องแก้ไขยังไงค่ะ เพราะว่าก็ปิด sqlDataReader แล้วทำไม่ขึ้น Error
Code
Private Sub AutoGenerateLendID()
Dim sqlTmp As String = ""
Dim comTmp As SqlCommand = New SqlCommand
Dim drLend As SqlDataReader
Dim tmpLendID As Integer = 0
sqlTmp = "SELECT TOP 1 LendID FROM Lend ORDER BY LendID DESC"
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
Try
With comTmp
.CommandType = CommandType.Text
.CommandText = sqlTmp
.Connection = Conn
drLend = .ExecuteReader()
End With
drLend.Read()
tmpLendID = CInt(CStr(drLend.Item("LendID")))
tmpLendID = tmpLendID + 1
LastLendID = tmpLendID.ToString("0000000")
drLend.Close()
Catch
LastLendID = "0000001"
End Try
End Sub