Sub ShowDatastock()
'แสดงรายการของสินค้าให้เลือก
DGStockL.Visible = True
btnNewPur.Visible = True
Dim AdapterStock As New SqlDataAdapter("Select * From Stock Order By SId", Conn)
[font=Verdana] AdapterStock.Fill(Ds, "DataStock")[/font]
DGStockL.DataSource = Ds.Tables("dataStock")
DGStockL.DataKeyField = "St_Id"
DGStockL.DataBind()
End Sub
ขึ้น เออเล่อร์ว่า Exception Details: System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.
There is already an open DataReader associated with this Command which must be closed first.
รบกวนหน่อยค่ะ ถ้าโค้ดแบบนี้ Error there is already an open datareader associated with this command which must be closed first ต้องแก้ไขตรงไหนค่ะ
Code (VB.NET)
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If txtMemberID.Text = "" Then
MessageBox.Show("กรุณาระบุรหัสบัตรผู้ยืม !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtMemberID.Focus()
Exit Sub
End If
If lsvEquList.Items.Count = 0 Then
MessageBox.Show("กรุณาป้อนรายการยืมอุปกรณ์ !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtEquID.Focus()
Exit Sub
End If
Dim sqlLend As String = ""
Dim comLend As SqlCommand = New SqlCommand
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
Try
If MessageBox.Show("คุณต้องการบันทึกรายการยืมอุปกรณ์ ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
AutoGenerateLendID()
sqlLend = "INSERT INTO Lend (LendID,MemberID,NetReceived,Return_StatusAll) "
sqlLend &= " VALUES('" & LastLendID & "',"
sqlLend &= "'" & txtMemberID.Text & "',"
sqlLend &= "0,'0')"
With comLend
.CommandType = CommandType.Text
.CommandText = sqlLend
.Connection = Conn
.ExecuteNonQuery()
End With
Dim i As Integer = 0
Dim tmpEquipmentID As String = ""
Dim sqlEquipment As String = ""
Dim tmpEquipmentInStock As Integer = 0
Dim tmpEquipmentInLend As Integer = 0
Dim tmpEquipmentNum As Integer = 0
Dim tmpDateNum As Integer = 0
Dim tmpAmount As Integer = 0
Dim tmpSum As Integer = 0
Dim tmpNet As Integer = 0
Dim tmpLendDate As Date
Dim tmpReturnDate As Date
tmpLendDate = Date.Now
For i = 0 To lsvEquList.Items.Count - 1
sqlLend = "INSERT INTO LendDetail(LendID,EquipmentID,Lend_Date,"
sqlLend &= " Return_Date,Lend_Num,AmountReceived,Return_Status)"
sqlLend &= " VALUES('" & LastLendID & "',"
tmpEquipmentID = lsvEquList.Items(i).SubItems(0).Text
tmpEquipmentNum = CInt(lsvEquList.Items(i).SubItems(2).Text)
tmpDateNum = CInt(lsvEquList.Items(i).SubItems(3).Text)
tmpAmount = CInt(lsvEquList.Items(i).SubItems(4).Text)
sqlLend &= "'" & tmpEquipmentID & "',"
sqlLend &= "'" & tmpLendDate & "',"
tmpReturnDate = tmpLendDate.AddDays(tmpDateNum)
sqlLend &= "'" & tmpReturnDate & "',"
sqlLend &= tmpEquipmentNum & ","
tmpSum = (tmpEquipmentNum * tmpDateNum * tmpAmount)
sqlLend &= tmpNet & ","
sqlLend &= "'0')"
With comLend
.CommandText = sqlLend
.ExecuteNonQuery()
End With
sqlEquipment = "SELECT EquipmentID,Equip_InStock,Equip_InLend FROM Equipment"
sqlEquipment &= " WHERE (EquipmentID='" & tmpEquipmentID & "')"
da.SelectCommand.CommandText = sqlEquipment
da.Fill(ds, "Equipment")
tmpEquipmentInStock = CInt(ds.Tables("Equipment").Rows(0).Item("Equip_InStock"))
tmpEquipmentInLend = CInt(ds.Tables("Equipment").Rows(0).Item("Equip_InLend"))
ds.Tables("Equipment").Clear()
sqlLend = "UPDATE Equipment"
sqlLend &= " SET Equip_InStock=" & tmpEquipmentInStock - tmpEquipmentNum & ","
sqlLend &= " Equip_InLend=" & tmpEquipmentInLend + tmpEquipmentNum
sqlLend &= " WHERE (EquipmentID='" & tmpEquipmentID & "')"
With comLend
.CommandText = sqlLend
.ExecuteNonQuery()
End With
Next
MessageBox.Show("บันทึกรายการยืมอุปกรณ์เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
If IsInstallPrinter() = True Then
If prDlg.ShowDialog = DialogResult.OK Then
prDoc.Print()
End If
Else
MessageBox.Show("กรุณาติดตั้งเครื่อง Printer ก่อนสั่งพิมพ์เอกสาร", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
ClearAllMemberData()
ClearAllEquipmentData()
lsvEquList.Items.Clear()
cboPosition.SelectedIndex = 0
txtMemberID.Enabled = True
txtMemberID.Focus()
End If
Catch ErrProcess As Exception
MessageBox.Show("ไม่สามารถบันทึกรายการยืมอุปกรณ์ได้ เนื่องจาก " & ErrProcess.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End Try
End Sub