 |
|
INSERT ไม่ผ่านคับรบกวนช่วยดูให้ที นั่งมานานแล้วไม่ออกซักทีคับ ExecuteReader requires an open and available Connection. The connection's current state is closed. |
|
 |
|
|
 |
 |
|
Code (VB.NET)
Dim sql As String
Conn = New OleDbConnection()
Conn.ConnectionString = strConnString
Conn.Open()
sql = "INSERT INTO [TB_Rent] (Cus_ID,Em_ID,Rent_Date) Values ('" & txtfind.Text & "','" & txtEmc.Text & "','" & Date.Today & "')"
Dim cmd As OleDbCommand = New OleDbCommand(sql, Conn)
Dim rdr As OleDbDataReader = cmd.ExecuteReader()
'Conn.Open()
Conn.Close()
Dim sql2 As String
Conn = New OleDbConnection()
Conn.ConnectionString = strConnString
Conn.Open()
Dim i As Integer
i = DataGridView1.CurrentRow.Index
sql2 = "INSERT INTO [TB_RentDetail] (Book_ID,Rent_Price,Rent_Quantity,Rent_Date,Deadline_Date,Price_Penalty,Price_Total,Status) Values ('" & DataGridView1.Item(0, i).Value & "','" & DataGridView1.Item(1, i).Value & "','" & lbCheck.Text & "','" & DataGridView1.Item(4, i).Value & "','" & Date.Today.AddDays(+3) & "','" & DataGridView1.Item(5, i).Value & "','" & lbTotal.Text & "','" & CInt(txtRentc.Text) - CInt(lbCheck.Text) & "')"
Dim cmd2 As OleDbCommand = New OleDbCommand(sql2, Conn)
[font=Verdana] Dim rdr2 As OleDbDataReader = cmd.ExecuteReader()[/font]
Conn.Close()
มันขึ้นว่า ExecuteReader requires an open and available Connection. The connection's current state is closed.
Tag : VB.NET
|
ประวัติการแก้ไข 2012-01-03 16:03:33
|
 |
 |
 |
 |
Date :
2012-01-03 16:01:54 |
By :
n_Plus |
View :
1822 |
Reply :
6 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณปิดๆ เปิด ๆ Connection ครับ ลองดูว่ามันผิดตรงไหน จาก Error คือ คุณไม่ได้ทำการเปิด Connection หรือถุกปิดไปแล้วครับ
|
 |
 |
 |
 |
Date :
2012-01-03 17:35:44 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมลองแก้ดูแล้วคับ แต่มันขึ้นว่า Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
ไม่รู้ว่าผิดตรงไหนคับ
|
 |
 |
 |
 |
Date :
2012-01-03 17:59:27 |
By :
n_Plus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แก้มาเป็น
Code (VB.NET)
Dim sql As String
Conn = New OleDbConnection()
Conn.ConnectionString = strConnString
Conn.Open()
sql = "INSERT INTO [TB_Rent] (Cus_ID,Em_ID,Rent_Date) Values ('" & txtfind.Text & "','" & txtEmc.Text & "','" & Date.Today & "')"
Dim cmd As OleDbCommand = New OleDbCommand(sql, Conn)
Dim rdr As OleDbDataReader = cmd.ExecuteReader()
Dim sql2 As String
Conn = New OleDbConnection()
Conn.ConnectionString = strConnString
Dim i As Integer
i = DataGridView1.CurrentRow.Index
sql2 = "INSERT INTO [TB_RentDetail] (Book_ID,Rent_Price,Rent_Quantity,Rent_Date,Deadline_Date,Price_Penalty,Price_Total,Status) Values ('" & DataGridView1.Item(0, i).Value & "','" & DataGridView1.Item(1, i).Value & "','" & lbCheck.Text & "','" & DataGridView1.Item(3, i).Value & "','" & Date.Today.AddDays(+3) & "','" & DataGridView1.Item(4, i).Value & "','" & lbTotal.Text & "','" & CInt(lbCheck.Text) - CInt(txtRentc.Text) & "')"
Dim cmd2 As OleDbCommand = New OleDbCommand(sql2, Conn)
Dim rdr2 As OleDbDataReader = cmd.ExecuteReader()
Conn.Close()
มันก็ยัง Error : There is already an open DataReader associated with this Command which must be closed first.
ผมมือใหม่คับรบกวนช่วยดูทีคับ
|
 |
 |
 |
 |
Date :
2012-01-03 18:11:01 |
By :
n_Plus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าให้ผม คิดน่ะคับ อาจจะตอบไม่ตรงประเด็นหน่อยแต่มีคำถาม นึงคับ
อย่างแรก ทำไม ไม่ใช่ cmd.ExcuteNonQuery() เพราะไม่เห็น เอา rdr ไปใช้ ทำอะไร ต่อเลยหนิคับ ถ้าเปลี่ยนไปใช้ ที่ผมบอก
ก็จะไม่ต้องประกาศ Dim rdr As OleDbDataReader เลยน่ะคับ ถ้าให้อธิบาย ที่ คุณใช้มันคือการบันทึกข้อมูลลง DB อะคับ
ไม่มีการ Return ค่ากลับมา เพราะฉะนั้นไม่จำเป็นต้องเก็บค่าก็ได้ คับ ที่ผม คิดว่ามัน error น่าจะมาจากตัวนี้คับ เพราะว่า คุณมีการใช้
cmd ตัวเดียวกันซึ่งในการ insert ครั้งแรก มันได้มี return ค่าอะไร ก็ตามกลับมาแล้ว ในตัวแปร cmd ก็เลย เก็บ ค่าอยู่ เสร็จแล้วหลังจากนั้น
คุณก็ใช้ cmd ตัวเดียวกันในการ query อีกมันก็เลย error มาว่า command already อะคับ ถ้าจะแก้ ผมก็แนะนำให้ cmd ค่าใหม่ คับ
ใช้ cmd ตัวเดิมนั้นแหละคับ แล้วมา new ค่าเหมือนกับ Dim cmd As OleDbCommand = New OleDbCommand(sql, Conn)
อีกครั้งคับ คิดว่าน่าจะหาย วิธีที่สองก็คือ เปลี่ยน เป็น cmd.ExcuteNonQuery() เลยคับ แล้วไม่ต้องประกาศ ตัวแปร Reader คับ
คิดว่าหาย เช่นกัน ลองดูคับ
|
 |
 |
 |
 |
Date :
2012-01-04 17:21:26 |
By :
PaOAllY |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ๋อ ผม เจอ และคับที่มันผิด คือ cmd.ExecuteReader() ลองดูคับ เห็น ตัวล่างคุณประกาศ ไว้เป็น cmd2 คับ แต่พอเวลา execute คุณใช้ cmd คับ โทษที อธิบายไปซะยาว ไม่ได้ดูโค๊ด ดีๆ
|
 |
 |
 |
 |
Date :
2012-01-04 17:24:05 |
By :
PaOAllY |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขึ้น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
|
 |
 |
 |
 |
Date :
2013-01-30 02:52:01 |
By :
Teaw |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|