Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Public Class frmAssign12
Dim sqlConn As SqlConnection
Dim sqlDa As SqlDataAdapter
Dim sqlCom As SqlCommand
Dim sqlDr As SqlDataReader
Dim daSet As DataSet
Dim sqlTs As SqlTransaction
Dim strCon As String = ConfigurationManager.ConnectionStrings("VisualProgQ1.My.MySettings.daStudent").ConnectionString
Dim strSql As String
Dim actStatus As String
Private Sub frmAssign12_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
sqlConn = New SqlConnection
sqlConn.ConnectionString = strCon
sqlConn.Open()
strSql = "select stdId, stdName, stdLname, address, sex, gpa, tbDepartment.departName from tbStudent, tbDepartment where tbStudent.departID=tbDepartment.departID"
sqlDa = New SqlDataAdapter(strSql, strCon)
daSet = New DataSet
daSet.Clear()
sqlDa.Fill(daSet, "Show")
dgvStudent.DataSource = daSet.Tables("Show")
End Sub
Private Sub dgvStudent_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvStudent.CellEnter
Dim Row As Integer
Row = dgvStudent.CurrentCell.RowIndex
txtStdId.Text = dgvStudent.Item(0, Row).Value
txtStdName.Text = dgvStudent.Item(1, Row).Value
txtStdLname.Text = dgvStudent.Item(2, Row).Value
txtAddress.Text = dgvStudent.Item(3, Row).Value
If dgvStudent.Item(4, Row).Value = "ชาย" Then
radMale.Checked = True
Else
radFemale.Checked = True
End If
txtGpa.Text = dgvStudent.Item(5, Row).Value
cboDepartName.SelectedIndex = cboDepartName.Items.Add(dgvStudent.Item(6, Row).Value)
End Sub
Private Sub showDep()
If sqlConn.State = ConnectionState.Closed Then
sqlConn.Open()
End If
strSql = "Select departName from tbDepartment"
sqlCom = New SqlCommand(strSql, sqlConn)
sqlDr = sqlCom.ExecuteReader
If sqlDr.HasRows Then
While sqlDr.Read
cboDepartName.Items.Add(sqlDr(0))
End While
cboDepartName.SelectedIndex = cboDepartName.Items.Count = 0
sqlDr.Close()
End If
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
actStatus = "insert"
dgvStudent.Enabled = False
panStudent.Enabled = True
btnAdd.Enabled = False
btnEdit.Enabled = False
btnDel.Enabled = False
btnSave.Enabled = True
btnCance.Enabled = True
txtStdId.Clear()
txtStdName.Clear()
txtStdLname.Clear()
txtAddress.Clear()
txtGpa.Clear()
radMale.Checked = True
txtStdId.Focus()
cboDepartName.Items.Clear()
If sqlConn.State = ConnectionState.Closed Then
sqlConn.Open()
End If
strSql = "Select departName from tbDepartment"
sqlCom = New SqlCommand(strSql, sqlConn)
sqlDr = sqlCom.ExecuteReader
If sqlDr.HasRows Then
While sqlDr.Read
cboDepartName.Items.Add(sqlDr(0))
End While
cboDepartName.SelectedIndex = cboDepartName.Items.Count = 0
sqlDr.Close()
End If
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
actStatus = "edit"
dgvStudent.Enabled = False
panStudent.Enabled = True
btnAdd.Enabled = False
btnEdit.Enabled = False
btnDel.Enabled = False
btnSave.Enabled = True
btnCance.Enabled = True
cboDepartName.Items.Clear()
End Sub
Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim sex As String
Dim intResult As Integer
If radMale.Checked = True Then
sex = "ชาย"
Else
sex = "หญิง"
End If
If actStatus = "insert" Then
strSql = "insert into tbStudent values('" & txtStdId.Text & "','" & txtStdName.Text & "','" & txtStdLname.Text & "','" & txtAddress.Text & "','" & sex & "','" & txtGpa.Text & "','" & cboDepartName.Text & "')"
End If
sqlCom = New SqlCommand
sqlCom.CommandType = CommandType.Text
sqlCom.CommandTimeout = 15
sqlCom.CommandText = strSql
sqlCom.Connection = sqlConn
sqlTs = sqlConn.BeginTransaction
sqlCom.Transaction = sqlTs
Try
intResult = sqlCom.ExecuteNonQuery
MessageBox.Show("บันทึกข้อมูลเรียบร้อย...")
sqlTs.Commit()
Call frmAssign12_Load(sender, e)
Catch
MessageBox.Show("เกิดข้อผิดพลาด ไม่สามารถบันทึกได้...")
sqlTs.Rollback()
End Try
Call btnCance_Click(sender, e)
End Sub
Private Sub btnCance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCance.Click
dgvStudent.Enabled = True
panStudent.Enabled = False
btnAdd.Enabled = True
btnEdit.Enabled = True
btnDel.Enabled = True
btnSave.Enabled = False
btnCance.Enabled = False
cboDepartName.Items.Clear()
Call frmAssign12_Load(sender, e)
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
ถ้ามีวิธีที่ดีกว่าก็ไม่ต้องสนวิธีผมก็ได้นะครับ
เพิ่ม Dim _id As String
ลงไปด้านล่าง
Dim actStatus As String
เพิ่ม
Private Sub cboDepartName_TextChanged(ByVal sender As System.Object, Byval e As System.EventArgs) Handles cboDepartName.TextChanged
sqlConn = New SqlConnection
sqlConn.ConnectionString = strCon
sqlConn.Open()
strSql = "select departID From tbDepartment WHERE departName = @departName"
sqlCom = New SqlCommand
sqlCom.CommandType = CommandType.Text
sqlCom.CommandTimeout = 15
sqlCom.CommandText = strSql
sqlCom.Connection = sqlConn
sqlCom.Parameter.Addwithvalue("@departName", cboDepartName.text)
Dim sqldataReader = new SQLdatareader()
sqldataReader = sqlCom.executereader()
sqldatareader.read()
if sqldatareader.hasrow then
_id = sqldatareader("departID ")
end if
End Sub
ลงไปด้านล่างก่อน end class
Code (VB.NET)
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Public Class frmAssign12
Dim sqlConn As SqlConnection
Dim sqlDa As SqlDataAdapter
Dim sqlCom As SqlCommand
Dim sqlDr As SqlDataReader
Dim daSet As DataSet
Dim sqlTs As SqlTransaction
Dim strCon As String = ConfigurationManager.ConnectionStrings("VisualProgQ1.My.MySettings.daStudent").ConnectionString
Dim strSql As String
Dim actStatus As String
Dim _id As String
Private Sub frmAssign12_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
sqlConn = New SqlConnection
sqlConn.ConnectionString = strCon
sqlConn.Open()
strSql = "select stdId, stdName, stdLname, address, sex, gpa, tbDepartment.departName from tbStudent, tbDepartment where tbStudent.departID=tbDepartment.departID"
sqlDa = New SqlDataAdapter(strSql, strCon)
daSet = New DataSet
daSet.Clear()
sqlDa.Fill(daSet, "Show")
dgvStudent.DataSource = daSet.Tables("Show")
End Sub
Private Sub dgvStudent_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvStudent.CellEnter
Dim Row As Integer
Row = dgvStudent.CurrentCell.RowIndex
txtStdId.Text = dgvStudent.Item(0, Row).Value
txtStdName.Text = dgvStudent.Item(1, Row).Value
txtStdLname.Text = dgvStudent.Item(2, Row).Value
txtAddress.Text = dgvStudent.Item(3, Row).Value
If dgvStudent.Item(4, Row).Value = "ชาย" Then
radMale.Checked = True
Else
radFemale.Checked = True
End If
txtGpa.Text = dgvStudent.Item(5, Row).Value
cboDepartName.SelectedIndex = cboDepartName.Items.Add(dgvStudent.Item(6, Row).Value)
End Sub
Private Sub showDep()
If sqlConn.State = ConnectionState.Closed Then
sqlConn.Open()
End If
strSql = "Select departName from tbDepartment"
sqlCom = New SqlCommand(strSql, sqlConn)
sqlDr = sqlCom.ExecuteReader
If sqlDr.HasRows Then
While sqlDr.Read
cboDepartName.Items.Add(sqlDr(0))
End While
cboDepartName.SelectedIndex = cboDepartName.Items.Count = 0
sqlDr.Close()
End If
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
actStatus = "insert"
dgvStudent.Enabled = False
panStudent.Enabled = True
btnAdd.Enabled = False
btnEdit.Enabled = False
btnDel.Enabled = False
btnSave.Enabled = True
btnCance.Enabled = True
txtStdId.Clear()
txtStdName.Clear()
txtStdLname.Clear()
txtAddress.Clear()
txtGpa.Clear()
radMale.Checked = True
txtStdId.Focus()
cboDepartName.Items.Clear()
If sqlConn.State = ConnectionState.Closed Then
sqlConn.Open()
End If
strSql = "Select departName from tbDepartment"
sqlCom = New SqlCommand(strSql, sqlConn)
sqlDr = sqlCom.ExecuteReader
If sqlDr.HasRows Then
While sqlDr.Read
cboDepartName.Items.Add(sqlDr(0))
End While
cboDepartName.SelectedIndex = cboDepartName.Items.Count = 0
sqlDr.Close()
End If
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
actStatus = "edit"
dgvStudent.Enabled = False
panStudent.Enabled = True
btnAdd.Enabled = False
btnEdit.Enabled = False
btnDel.Enabled = False
btnSave.Enabled = True
btnCance.Enabled = True
cboDepartName.Items.Clear()
End Sub
Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim sex As String
Dim intResult As Integer
If radMale.Checked = True Then
sex = "ชาย"
Else
sex = "หญิง"
End If
If actStatus = "insert" Then
strSql = "insert into tbStudent values('" & txtStdId.Text & "','" & txtStdName.Text & "','" & txtStdLname.Text & "','" & txtAddress.Text & "','" & sex & "','" & txtGpa.Text & "','" & cboDepartName.Text & "')"
End If
sqlCom = New SqlCommand
sqlCom.CommandType = CommandType.Text
sqlCom.CommandTimeout = 15
sqlCom.CommandText = strSql
sqlCom.Connection = sqlConn
sqlTs = sqlConn.BeginTransaction
sqlCom.Transaction = sqlTs
Try
intResult = sqlCom.ExecuteNonQuery
MessageBox.Show("บันทึกข้อมูลเรียบร้อย...")
sqlTs.Commit()
Call frmAssign12_Load(sender, e)
Catch
MessageBox.Show("เกิดข้อผิดพลาด ไม่สามารถบันทึกได้...")
sqlTs.Rollback()
End Try
Call btnCance_Click(sender, e)
End Sub
Private Sub btnCance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCance.Click
dgvStudent.Enabled = True
panStudent.Enabled = False
btnAdd.Enabled = True
btnEdit.Enabled = True
btnDel.Enabled = True
btnSave.Enabled = False
btnCance.Enabled = False
cboDepartName.Items.Clear()
Call frmAssign12_Load(sender, e)
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub cboDepartName_TextChanged(ByVal sender As System.Object, Byval e As System.EventArgs) Handles cboDepartName.TextChanged
sqlConn = New SqlConnection
sqlConn.ConnectionString = strCon
sqlConn.Open()
strSql = "select departID From tbDepartment WHERE departName = @departName"
sqlCom = New SqlCommand
sqlCom.CommandType = CommandType.Text
sqlCom.CommandTimeout = 15
sqlCom.CommandText = strSql
sqlCom.Connection = sqlConn
sqlCom.Parameter.Addwithvalue("@departName", cboDepartName.text)
Dim sqldataReader = new SQLdatareader()
sqldataReader = sqlCom.executereader()
sqldatareader.read()
if sqldatareader.hasrow then
_id = sqldatareader("departID ")
end if
End Sub
End Class