HOME > .NET Framework > Forum > ช่วยผมด้วยมีปัญหา .ExecuteNonQuery()... ExecuteNonQuery requires an open and available Connection. The connection's current state is closed
ช่วยผมด้วยมีปัญหา .ExecuteNonQuery()... ExecuteNonQuery requires an open and available Connection. The connection's current state is closed
ช่วยผมด้วยมีปัญหา .ExecuteNonQuery() มันขึ้นว่า ExecuteNonQuery requires an open and available Connection. The connection's current state is closed
รันได้ครับป้อนข้อมูลได้ตอนกด เพิ่ม ไม่ได้
นี่โค๊ดของผมครับ
Code (VB.NET)
Imports System.Data.OleDb
Imports System.Data
Public Class Form1
Public conn As New OleDbConnection
Private Sub FormLoad() Handles MyBase.Load
connectdata()
End Sub
Private Sub connectdata() Handles MyBase.Load
Dim conn As OleDbConnection
conn = New OleDbConnection(" Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/data11.mdb;Persist Security Info=False;")
conn.Open()
End Sub
Public Function ExecuteSQL(ByVal prmSQL As String) As Boolean
connectdata()
Dim Addcom As New OleDbCommand
With Addcom
.CommandType = CommandType.Text
.CommandText = prmSQL
.Connection = conn
.ExecuteNonQuery() มันมาติดตรงนี้ครับ
End With
ExecuteSQL = True
End Function
Private Sub From1() Handles Button1.Click
ExecuteSQL("insert into student(Fname,Lname)values(' " & TextBox1.Text & " ' ,' " & TextBox2.Text & " ' )")
'ExecuteSQL("INSERT INTO Student(Fname, Lname)VALUES ('" & txtFname.Text & "','" & txtLname.Text & "')")
End Sub
End Class
Imports System.Data
Imports System.Data.SqlClient
Public Class Form2
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label2.Text = Today
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sQL As String
sQL = "insert into Add(Date,Number,Namebook,Peice)"
sQL = sQL & "VALUES('"
sQL = sQL & TextBox1.Text & "','"
sQL = sQL & TextBox2.Text & "','"
sQL = sQL & TextBox3.Text & "')"
Dim cmd As SqlCommand
cmd = New SqlCommand
With cmd
.CommandType = CommandType.Text
.CommandText = sQL
.Connection = conn()
.ExecuteNonQuery() (มันขึ้น eroor ตรงนี้คับช่วยทีนะคับ)
End With
MsgBox("เพิ่มข้อมูลเรียบร้อยแล้ว")
TextBox1.Clear()
TextBox2.Clear()
TextBox2.Clear()
End Sub
End Class
Private Sub ButtonSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonSave.Click
If Not (IsNumeric(TextNumFloors.Text) OrElse Is Integer(TextNumFloors.Text)) Then
MessageBox.Show("จำนวนชั้นต้องเป็นเลขจำนวนเต็ม")
Exit Sub
ElseIf Not (IsNumeric(TextRoomsPerFloor.Text) OrElse IsInteger(TextRoomsPerFloor.Text)) Then
MessageBox.Show("จำนวนห้องในแต่ละชั้นต้องเป็นเลขจำนวนเต็ม")
Exit Sub
ElseIf Not IsNumeric(TextNumFloors.Text) Then
MessageBox.Show("ค่าเช่าต้องเป็นตัวเลข")
Exit Sub
End If
If hasRows Then
sql = "UPDATA Ref SET " & "NumFloors = @floors, " & "RoomsPerFloor = @rooms, " & "Rental = @rent"
Else
sql = "INSERT INTO Ref(NumFloors,RoomsPerFloors,Rental) " & "VALUES(@floors, @rooms, @rent)"
End If
command = New SqlCommand(sql, connection)
command.Parameters.Clear()
command.Parameters.AddWithValue("floors", TextNumFloors.Text)
command.Parameters.AddWithValue("rooms", TextRoomsPerFloor.Text)
command.Parameters.AddWithValue("rent", TextRental.Text)
Dim r As Integer = command.ExecuteNonQuery()
If r = 1 Then
MessageBox.Show("ปรับปรุงข้อมูล")
connection.Close()
FormRef_load(sender, e)
Else
MessageBox.Show("เกิดข้อผิดพลาด")
End If
End Sub