โค้ดนี้ใช้เพิ่มลบแก้ไขข้อมูลสินค้านะคะ แต่เวลาเลือกสินค้าแล้วกดลบมันจะขึ้นว่า Incorrect syntax near '*'.
ส่วนเวลาจะกดเพิ่มข้อมูลก็จะขึ้นว่า Incorrect syntax near 'SETCustomerName'. ค่ะ ไม่ทราบว่าต้องแก้ที่ตรงไหนบ้างคะ
Code (VB.NET)
Imports System.Data
Imports System.Data.SqlClient
Public Class Form2
Private dataset As DataSet
Private conn As SqlConnection
Private cmd As SqlCommand
Private rowCount As Integer = 0
Private position As Integer = 0
Private Sub Form2_Load() Handles Me.Load
conn = New SqlConnection(conStr)
conn.Open()
ReadData()
FillListBox()
Bindings()
End Sub
Private Sub Form2_FormClosed() Handles Me.FormClosed
conn.Close()
End Sub
Private Sub ReadData()
Dim sql As String = "SELECT * FROM Customers"
cmd = New SqlCommand(sql, conn)
Dim adapter As New SqlDataAdapter(cmd)
dataset = New DataSet()
adapter.Fill(dataset, "cust")
rowCount = dataset.Tables("cust").Rows.Count
End Sub
Private Sub FillListBox()
Dim custname As String = ""
ListBox1.Items.Clear()
For i = 0 To dataset.Tables("cust").Rows.Count - 1
custname = dataset.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", dataset, "cust.CustomerID")
tbxname.DataBindings.Add("Text", dataset, "cust.CustomerName")
tbxadrs.DataBindings.Add("Text", dataset, "cust.CustomerAddress")
Me.BindingContext(dataset, "cust").Position = 0
End Sub
Private Sub ListBox1_SelectedIndexChanged() Handles ListBox1.SelectedIndexChanged
Dim index As Integer = ListBox1.SelectedIndex
Me.BindingContext(dataset, "cust").Position = index
position = index
UpdateStatus()
End Sub
Private Sub UpdateStatus()
ToolStripStatusLabel1.Text = (position + 1) & "of" & rowCount
End Sub
Private Sub TabControl_SelectdIndexChanged() Handles TabControl1.SelectedIndexChanged
If (TabControl1.SelectedIndex = 1) Then
ClearBindings()
btndel.Enabled = False
ListBox1.Enabled = False
ElseIf (TabControl1.SelectedIndex = 0) Then
ReadData()
If (rowCount = 0) Then
Return
End If
Bindings()
btndel.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 = "") Then
MsgBox("ใส่ข้อมูลไม่ครบ")
Return
End If
Dim sql As String = ""
If (tbxid.Text = "") Then
sql = "INSERT INTO Customers("
sql &= "CustomerName,CustomerAddress)"
sql &= "VALUES(@nm,@adrs)"
Else
sql = "UPDATE Customers SET"
sql &= "CustomerName=@nm,CustomerAddress=@adrs"
sql &= "WHERE CustomerID= " & tbxid.Text
End If
cmd = New SqlCommand(sql, conn)
cmd.Parameters.AddWithValue("nm", tbxname.Text)
cmd.Parameters.AddWithValue("adrs", tbxadrs.Text)
Dim affectedRow As Integer = cmd.ExecuteNonQuery()
If (affectedRow < 1) Then
ToolStripStatusLabel1.Text = "error"
Else
ToolStripStatusLabel1.Text = "save"
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 btndel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndel.Click
If (tbxid.Text = "") Then
Return
End If
If (MsgBox("delete", MsgBoxStyle.OkCancel) = MsgBoxResult.Cancel) Then
Return
End If
Dim sql As String
sql = "DELETE * FROM Orders"
sql &= "WHERE Customername=@cust"
cmd = New SqlCommand(sql, conn)
Dim r As Integer = cmd.ExecuteNonQuery()
If (r > 0) Then
ToolStripStatusLabel1.Text = "finish"
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 Orders WHERE CustomerName=@cust"
cmd = New SqlCommand(sql, conn)
cmd.Parameters.AddWithValue("cust", tbxname.Text)
cmd.ExecuteNonQuery()
End Sub
End Class