HOME > .NET Framework > Forum > INSERT ไม่ผ่านคับรบกวนช่วยดูให้ที นั่งมานานแล้วไม่ออกซักทีคับ ExecuteReader requires an open and available Connection. The connection's current state is closed.
INSERT ไม่ผ่านคับรบกวนช่วยดูให้ที นั่งมานานแล้วไม่ออกซักทีคับ ExecuteReader requires an open and available Connection. The connection's current state is closed.
ผมลองแก้ดูแล้วคับ แต่มันขึ้นว่า Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
ไม่รู้ว่าผิดตรงไหนคับ
ขึ้นError there is already an open datareader associated with this command which must be closed first[color=redควระปิด Debug ยังไงค่ะ รบกวนหน่อยค่ะ[/color]
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 & ","
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
Finally
Conn.Close()
End Try
End Sub