ลองเอา Code มาดูทั้งหมดครับ ตัวแปร Connจะต้องเป็น Public / Privated ที่สามารถเรียกได้ทั้ง Class ครับ ไม่ใช่เฉพาะใน Sub ครับ
Date :
2012-03-11 07:01:12
By :
webmaster
No. 7
Guest
Code (VB.NET)
Imports System.Data
Imports System.Data.SqlClient
'Module Module1
'Friend strConn As String = "Data Source=PRIKII;" & "Initial Catalog=Books;Integrated Security=True"
'End Module
Public Class FormCustomers
Dim strConn As String = "Data Source=prikii;" & "Initial Catalog=Books;Integrated Security=True"
Dim Conn As SqlConnection = New System.Data.SqlClient.SqlConnection()
Dim ds As DataSet = New DataSet()
Dim cmd As SqlCommand = New SqlCommand()
Dim rowCount As Integer = 0
Dim positionn As Integer = 0
Private Sub FormCustomers_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
With Conn
If .State = ConnectionState.Open Then
.Close()
.ConnectionString = strConn
.Open()
End If
End With
ReadData()
FillListBox()
Bindings()
End Sub
Private Sub FormCustomers_FormClosed(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.FormClosed
Conn.Close()
End Sub
Private Sub frmCustomer_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Disposed
If Conn.State = ConnectionState.Open Then Conn.Close()
End Sub
Private Sub ReadData()
Dim sql As String = "SELECT * FROM Customer"
'cmd = New SqlCommand(sql, Conn)
Dim adapter As New SqlDataAdapter(sql, Conn)
'adapter = New SqlDataAdapter(cmd)
ds = New DataSet()
adapter.Fill(ds, "cust")
rowCount = ds.Tables("cust").Rows.Count
End Sub
Private Sub FillListBox()
Dim custname As String = ""
ListBox1.Items.Clear()
For i = 0 To ds.Tables("cust").Rows.Count - 1
custname = ds.Tables("cust").Rows(i)("CustomerName")
ListBox1.Items.Add(custname)
Next
If (ListBox1.Items.Count > 0) Then
ListBox1.SelectedIndex = 0
End If
End Sub
Private Sub Bindings()
TbxID.DataBindings.Add("Text", ds, "cust.CustomerID")
TbxName.DataBindings.Add("Text", ds, "cust.CustomerName")
TbxAdrs.DataBindings.Add("Text", ds, "cust.Address")
TbxTel.DataBindings.Add("Text", ds, "cust.Telephone")
TbxBirth.DataBindings.Add("Text", ds, "cust.Birthday")
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles ListBox1.SelectedIndexChanged
Dim index As Integer = ListBox1.SelectedIndex
Me.BindingContext(ds, "cust").Position = index
positionn = index
UpdateStatus()
End Sub
Private Sub UpdateStatus()
ToolStripStatusLabel1.Text = (positionn + 1) & " of " & rowCount
End Sub
Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles TabControl1.SelectedIndexChanged
If (TabControl1.SelectedIndex = 1) Then
ClearBindings()
BtnDelete.Enabled = False
BtnSave.Enabled = False
ElseIf (TabControl1.SelectedIndex = 0) Then
ReadData()
If (rowCount = 0) Then
Return
End If
Bindings()
BtnDelete.Enabled = True
ListBox1.Enabled = True
ListBox1.SetSelected(0, True)
End If
End Sub
Private Sub ClearBindings()
For Each c As Object In GroupBox1.Controls
If (TypeOf c Is TextBox) Then
c.Text = ""
c.DataBindings.Clear()
End If
Next
End Sub
Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles BtnSave.Click
If (TbxName.Text = "" Or TbxAdrs.Text = "" Or TbxTel.Text = "" Or TbxBirth.Text = "") Then
MsgBox("กรุณาใส่ข้อมูลให้ครบ !")
Return
End If
Dim sql As String = ""
If (TbxID.Text = "") Then
sql = "INSERT INTO Customer("
sql &= "CustomerName, Address ,Telephone, Birthday) "
sql &= "VALUES(@nm, @adrs, @tel, @birth)"
Else
sql = "UPDATE Customer SET "
sql &= "CustomerName = @nm, Address = @adrs, Telephone = @tel, Birthday = @birth "
sql &= "WHERE CustomerID = " & TbxID.Text
End If
cmd = New SqlCommand(sql, Conn)
cmd.Parameters.AddWithValue("nm", TbxName.Text)
cmd.Parameters.AddWithValue("adrs", TbxAdrs.Text)
cmd.Parameters.AddWithValue("tel", TbxTel.Text)
cmd.Parameters.AddWithValue("birth", TbxBirth.Text)
Dim affectedRow As Integer = cmd.ExecuteNonQuery()
If (affectedRow < 1) Then
ToolStripStatusLabel1.Text = "เกิดข้อผิดพลาดในการบันทึกข้อมูล"
Else
ToolStripStatusLabel1.Text = "ข้อมูลถูกจัดเก็บแล้ว"
Dim name As String = TbxName.Text
ReadData()
FillListBox()
ClearBindings()
If (TabControl1.SelectedIndex = 0) Then
Bindings()
End If
Dim idx As Integer = ListBox1.FindStringExact(name)
If (idx > -1) Then
ListBox1.SetSelected(idx, True)
End If
UpdateStatus()
End If
End Sub
Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
If (TbxID.Text = "") Then
Return
End If
If (MsgBox("ต้องการลบข้อมูล ?", MsgBoxStyle.OkCancel) = MsgBoxResult.Cancel) Then
Return
End If
Dim sql As String
sql = "DELETE * FROM Order"
sql &= "WHERE CustomerName = @cust"
cmd = New SqlCommand(sql, Conn)
Dim r As Integer = cmd.ExecuteNonQuery()
If (r > 0) Then
ToolStripStatusLabel1.Text = "ข้อมูลถูกลบแล้ว"
End If
Dim idx0 As Integer = ListBox1.SelectedIndex
ClearBindings()
ReadData()
FillListBox()
Bindings()
If (idx0 > 0) Then
ListBox1.SetSelected(idx0 - 1, True)
End If
sql = "DELETE * FROM Order WHERE CustomerName = @cust"
cmd = New SqlCommand(sql, Conn)
cmd.Parameters.AddWithValue("cust", TbxName.Text)
cmd.ExecuteNonQuery()
End Sub
End Class