HOME > Community Zone > Forum > error code : There is already an open DataReader associated with this Command which must be closed first. แนะนำทีไม่รุ้จะเพิ่งที่ไหน T^T
error code : There is already an open DataReader associated with this Command which must be closed first. แนะนำทีไม่รุ้จะเพิ่งที่ไหน T^T
T[font=Verdana]here is already an open DataReader associated with this Command which must be closed first.[/font]
แนะนำทีไม่รุ้จะเพิ่งที่ไหน T^T
Code (VB.NET)
Imports System.Data
Imports System.Data.SqlClient
Public Class Frmeitdepartment
Dim connetionString As String
Dim Conn As New SqlConnection
Dim da As New SqlDataAdapter
'Dim sc As New SqlCommand
Dim strCon As String
Dim ds As New DataSet
Dim cmd As New SqlCommand(strCon, Conn)
'Dim DR As SqlDataReader
Private Sub Frmeitdepartment_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
strCon = "Data Source=KA4L-C62AA9DB66\SA;Initial Catalog=DBMSS;User Id=sa;Password=123;"
Conn.ConnectionString = strCon
If Conn.State = ConnectionState.Open Then
Conn.Close()
End If
Conn.Open()
Dim sqlcb As String
sqlcb = "select departmentname from TbDepartment"
Dim cmdcb As New SqlCommand(sqlcb, Conn)
Dim DR2 As SqlDataReader = cmdcb.ExecuteReader
If DR2.Read Then
ComboBox1.Text = DR2("departmentname")
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim autodepart As String
autodepart = "SELECT max(iddepartment)+1 as iddepartment FROM Tbdepartment"
Dim cmdauto As New SqlCommand(autodepart, Conn)
Dim DR1 As SqlDataReader = cmdauto.ExecuteReader
If DR1.Read Then
TextBox1.Text = DR1("iddepartment")
Button4.Enabled = False
TextBox1.Enabled = False
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim newdepart As String
If MessageBox.Show("ยืนยันการบันทึกข้อมุล", "ยกเลิก", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
newdepart = "insert into Tbdepartment(iddepartment,departmentname,username,pw)values('" + TextBox1.Text + "'" & ",'" + TextBox2.Text + "'" & ",'" + TextBox3.Text + "'" & ",'" + TextBox4.Text + "')"
Dim cmdadd As New SqlCommand(newdepart, Conn)
cmdadd.ExecuteNonQuery()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sqldel As String = ("delete from Tbdepartment where namedepartment = '" & ComboBox1.Text & "'")
Dim cmddel As New SqlCommand(sqldel, Conn) ' เป็นการเชื่อฐานข้อมูลของท่าน
cmddel.ExecuteNonQuery()
End Sub
End Class