HOME > .NET Framework > Forum > อยากทราบว่า ทำไมตอนที่เราเพิ่มข้อมูลในหน้าฟอล์มแล้วมันเพิ่มได้ แต่ในฐานข้อมูลมันไม่บันทึกข้อมูลที่เราเพิ่มจากหน้าฟอล์มคับ
Imports System.Data
Imports System.Data.SqlClient
Public Class FrmCategory
Private Sub UpdateBinding()
TextID.DataBindings.Clear()
TextName.DataBindings.Clear()
sql = "SELECT * FROM Categories"
command = New SqlCommand(sql, connection)
adapter = New SqlDataAdapter(command)
dataSt = New DataSet()
adapter.Fill(dataSt, "category")
bindingSrc = New BindingSource()
bindingSrc.DataSource = dataSt.Tables("category")
TextID.DataBindings.Add("Text", bindingSrc, "CategoryID")
TextName.DataBindings.Add("Text", bindingSrc, "CategoryName")
ListBox1.DataSource = dataSt.Tables("category")
ListBox1.DisplayMember = "CategoryName"
End Sub
Private Sub FrmCategory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
connection = New SqlConnection(conStr)
If connection.State = ConnectionState.Closed Then
connection.Open()
End If
UpdateBinding()
BtnSave.Enabled = False
End Sub
Private Sub ListBox1_SelectedIndexChanged() _
Handles ListBox1.SelectedIndexChanged
If (bindingSrc Is Nothing) Then
Exit Sub
End If
bindingSrc.Position = ListBox1.SelectedIndex
Toolstriplabel1.Text = _
"ลำดับที่: " & (bindingSrc.Position + 1) & "/" & bindingSrc.Count
ShowDetails()
End Sub
Private Sub ShowDetails()
If Not (dataSt.Tables("product") Is Nothing) Then
dataSt.Tables("product").Clear()
End If
sql = "SELECT ProductID,ProductName,UnitPrice FROM Product "
command.CommandText = sql
adapter.SelectCommand = command
adapter.Fill(dataSt, "product")
DataGridView1.DataSource = dataSt.Tables("product")
Dim headerText() As String = {"รหัสสินค้า", "ชื่อสินค้า", "ราคาขาย"}
For i = 0 To headerText.Count - 1
DataGridView1.Columns(i).HeaderText = headerText(i)
Next
End Sub
Private Sub BtnAdd_Click() _
Handles BtnAdd.Click
If BtnAdd.Text = "เพิ่มข้อมูล" Then
BtnAdd.Text = "ยกเลิก"
BtnSave.Enabled = True
ListBox1.Enabled = False
TextID.Text = ""
TextName.Text = ""
Else
BtnAdd.Text = "เพิ่มข้อมูล"
BtnSave.Enabled = False
ListBox1.Enabled = True
UpdateBinding()
End If
End Sub
Private Sub BtnSave_Click() Handles BtnSave.Click
If IsDataComplete() = False Then
Exit Sub
End If
sql = " INSERT INTO Categories(CategoryName)VALUES(@name)"
command.Parameters.Clear()
command.CommandText = sql
command.Parameters.AddWithValue("name", TextName.Text)
Dim result As Integer = command.ExecuteNonQuery()
If result = -1 Then
MessageBox.Show("เกิดข้อผิดพลาดไม่สามารถเพิ่มข้อมูลได้")
Else
MessageBox.Show("บันทึกข้อมูลแล้ว")
BtnAdd.PerformClick()
End If
End Sub
Private Function IsDataComplete() As Boolean
TextName.Text.Trim()
If TextName.Text = "" Then
MessageBox.Show("กรุณาใส่ข้อมูลให้ครบ")
Return False
Else
Return True
End If
End Function
Private Sub BtnEdit_Click() Handles BtnEdit.Click
If TextID.Text = "" Or IsDataComplete() = False Then
Exit Sub
End If
sql = "UPDATE Categories SET CategoryName = @name WHERE CategoryID = @id"
command.Parameters.Clear()
command.CommandText = sql
command.Parameters.AddWithValue("name", TextName.Text)
command.Parameters.AddWithValue("id", TextID.Text)
If command.ExecuteNonQuery() = 1 Then
MessageBox.Show("บันทึกการเปลี่ยนแล้ว")
UpdateBinding()
Else
MessageBox.Show("เกิดข้อพลาดในการแก้ไขข้อมูล")
End If
End Sub
Private Sub BtnDelete_Click() Handles BtnDelete.Click
If TextID.Text = "" Then
Exit Sub
End If
Dim result As DialogResult = _
MessageBox.Show("ท่านต้องการลบข้อมูลแถวนี้จริงหรือไม่", "ยืนยันการลบ", MessageBoxButtons.OKCancel)
If result = DialogResult.Cancel Then
Exit Sub
End If
sql = "DELETE FROM Categories WHERE CategoryID = " & TextID.Text
command.Parameters.Clear()
command.CommandText = sql
command.ExecuteNonQuery()
UpdateBinding()
End Sub
End Class