Imports System.Data.SqlClient
Public Class frmProduct
Dim conn As SqlConnection
Dim com As SqlCommand
Dim ap As SqlDataAdapter
Dim ds As New DataSet()
Dim dt As New DataTable
Dim dr As SqlDataReader
Dim s As Integer = 0
Private Sub frmProduct_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn = New SqlConnection()
With conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = Database.strConn.ToString
.Open()
End With
Dim sql2 As String = "SELECT ชื่อประเภทสินค้า FROM ประเภทสินค้า"
com = New SqlCommand
With com
.CommandType = CommandType.Text
.CommandText = sql2
.Connection = conn
.Parameters.Clear()
dr = .ExecuteReader()
If dr.HasRows Then
dt = New DataTable
dt.Load(dr)
Dim i As Integer = 0
While (i < dt.Rows.Count)
C2.Items.Add(dt.Rows(i).Item("ชื่อประเภทสินค้า").ToString)
i += 1
End While
conn.Close()
Else
conn.Close()
dr.Close()
End If
End With
ShowData()
End Sub
Private Sub ShowData()
With conn
If .State = ConnectionState.Open Then .Close()
.Open()
End With
ds.Clear()
Dim sql As String = "Select * From สินค้า Order by รหัสสินค้า"
com = New SqlCommand(sql, conn)
ap = New SqlDataAdapter(com)
ap.Fill(ds, "สินค้า")
DataGridView1.DataSource = ds.Tables("สินค้า")
DataGridView1.Columns.Item(0).Width = 120
DataGridView1.Columns.Item(1).Width = 120
DataGridView1.Columns.Item(2).Width = 120
DataGridView1.Columns.Item(3).Width = 120
DataGridView1.Columns.Item(4).Width = 125
DataGridView1.Columns.Item(5).Width = 125
conn.Close()
End Sub
Private Sub B1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If T1.Text = "" Or T2.Text = "" Or T3.Text = "" Or T4.Text = "" Or C1.Text = "" Or C2.Text = "" Then
MessageBox.Show("กรุณากรอกข้อมูลให้ครบทุกช่องด้วย", "การตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Else
If (s = 0) Then
With conn
If .State = ConnectionState.Open Then .Close()
.Open()
End With
Dim SQL1 As String = "INSERT INTO สินค้า(รหัสสินค้า,ชื่อสินค้า,จำนวน,หน่วยนับ,ราคาต่อหน่วย,ประเภทสินค้า) VALUES('" & T1.Text & "','" & T2.Text & "','" & CInt(T3.Text.Trim) & "','" & C1.Text & "','" & CDec(T4.Text.Trim) & "','" & C2.Text & "')"
com = New SqlCommand(SQL1, conn)
com.ExecuteNonQuery()
conn.Close()
Clear2()
T1.Text = ""
T1.Focus()
ShowData()
ElseIf (s = 1) Then
With conn
If .State = ConnectionState.Open Then .Close()
.Open()
End With
Dim sql As String = "UPDATE สินค้า SET ชื่อสินค้า ='" & T2.Text.Trim & "',จำนวน = '" & T3.Text.Trim & "',หน่วยนับ = '" & C1.Text.Trim & "',ราคาต่อหน่วย = '" & T4.Text.Trim & "',ประเภทสินค้า = '" & C2.Text.Trim & "' Where (รหัสสินค้า='" & T1.Text.Trim & "')"
com = New SqlCommand(sql, conn)
Dim a As Integer = com.ExecuteNonQuery
If a < 1 Then
Else
conn.Close()
ShowData()
Clear2()
T1.Text = ""
T1.Focus()
s = 0
End If
End If
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
C2.Items.Clear()
Clear2()
frmMain.Show()
Me.Close()
End Sub
Private Sub T1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles T1.TextChanged
With conn
If .State = ConnectionState.Open Then .Close()
.Open()
End With
Try
Dim ac As String = "SELECT * FROM สินค้า where รหัสสินค้า='" & T1.Text.Trim & "'"
com = New SqlCommand
With com
.CommandType = CommandType.Text
.CommandText = ac
.Connection = conn
.Parameters.Clear()
.Parameters.Add("สินค้า", SqlDbType.VarChar).Value = T1.Text.Trim
dr = .ExecuteReader()
If dr.HasRows Then
dt = New DataTable
dt.Load(dr)
T2.Text = dt.Rows(0).Item("ชื่อสินค้า").ToString
T3.Text = dt.Rows(0).Item("จำนวน").ToString
C1.Text = dt.Rows(0).Item("หน่วยนับ").ToString
T4.Text = dt.Rows(0).Item("ราคาต่อหน่วย").ToString
C2.Text = dt.Rows(0).Item("ประเภทสินค้า").ToString
dr.Close()
conn.Close()
T2.ReadOnly = True
T3.ReadOnly = True
T4.ReadOnly = True
C2.Enabled = False
C1.Enabled = False
B2.Enabled = True
B3.Enabled = True
Button1.Enabled = False
Else
Clear2()
End If
End With
Catch ex As Exception
End Try
End Sub
Private Sub Clear2()
T2.Text = ""
T3.Text = ""
T4.Text = ""
C1.Text = ""
C2.Text = ""
C2.SelectedIndex = -1
C1.SelectedIndex = -1
T2.ReadOnly = False
T3.ReadOnly = False
T4.ReadOnly = False
C2.Enabled = True
C1.Enabled = True
B2.Enabled = False
B3.Enabled = False
Button1.Enabled = True
End Sub
Private Sub B2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B2.Click
If (T1.Text <> "") Then
conn.Open()
Dim s As String = "DELETE FROM สินค้า WHERE รหัสสินค้า ='" & T1.Text.Trim & "'"
com = New SqlCommand(s, conn)
com.ExecuteNonQuery()
conn.Close()
ShowData()
T1.Text = ""
T1.Focus()
Clear2()
MessageBox.Show("ลบรายการเสร็จเรียบร้อย", "การดำเนินงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
End If
End Sub
Private Sub B3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B3.Click
Try
If T1.Text = "" Or T1.Text = "" Or T2.Text = "" Or T3.Text = "" Or C1.Text = "" Or T4.Text = "" Or C2.Text = "" Then
MessageBox.Show("กรุณากรอกข้อมูลให้ครบทุกช่องด้วยค่ะ", "การตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Else
conn.Open()
Dim sql As String = "UPDATE สินค้า SET ชื่อสินค้า= '" & T2.Text.Trim & "',จำนวน = '" & T3.Text.Trim & "',หน่วยนับ = '" & C1.Text.Trim & "',ราคาต่อหน่วย = '" & T4.Text.Trim & "',ประเภทสินค้า ='" & C2.Text.Trim & "' Where (รหัสพนักงาน='" & T1.Text.Trim & "')"
com = New SqlCommand(sql, conn)
Dim a As Integer = com.ExecuteNonQuery
If a < 1 Then
conn.Close()
MessageBox.Show("ชื่อสินค้านี้มีแล้ว", "การตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Warning)
T1.Text = ""
T1.Focus()
Else
conn.Close()
MessageBox.Show("ข้อมูลถูกจัดเก็บแล้ว", "การตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
ShowData()
Clear()
End If
End If
Catch ex As Exception
End Try
T2.ReadOnly = True
T3.ReadOnly = True
T4.ReadOnly = True
C2.Enabled = True
C1.Enabled = True
B2.Enabled = True
Button1.Enabled = True
s = 1
End Sub
Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter
End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub T3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles T3.TextChanged
End Sub
Private Sub Clear()
Throw New NotImplementedException
End Sub
Private Sub D1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
End Class
Private Sub B1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If T1.Text = "" Or T2.Text = "" Or T3.Text = "" Or T4.Text = "" Or C1.Text = "" Or C2.Text = "" Then
MessageBox.Show("กรุณากรอกข้อมูลให้ครบทุกช่องด้วย", "การตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Else
If (s = 0) Then
With conn
If .State = ConnectionState.Open Then .Close()
.Open()
End With
Dim SQL1 As String = "INSERT INTO สินค้า(รหัสสินค้า,ชื่อสินค้า,จำนวน,หน่วยนับ,ราคาต่อหน่วย,ประเภทสินค้า) VALUES('" & T1.Text & "','" & T2.Text & "','" & CInt(T3.Text.Trim) & "','" & C1.Text & "','" & CDec(T4.Text.Trim) & "','" & C2.Text & "')"
com = New SqlCommand(SQL1, conn)
com.ExecuteNonQuery()
conn.Close()
Clear2()
T1.Text = ""
T1.Focus()
ShowData()
ElseIf (s = 1) Then
With conn
If .State = ConnectionState.Open Then .Close()
.Open()
End With
Dim sql As String = "UPDATE สินค้า SET ชื่อสินค้า ='" & T2.Text.Trim & "',จำนวน = '" & T3.Text.Trim & "',หน่วยนับ = '" & C1.Text.Trim & "',ราคาต่อหน่วย = '" & T4.Text.Trim & "',ประเภทสินค้า = '" & C2.Text.Trim & "' Where (รหัสสินค้า='" & T1.Text.Trim & "')"
com = New SqlCommand(sql, conn)
Dim a As Integer = com.ExecuteNonQuery
If a < 1 Then
Else
conn.Close()
ShowData()
Clear2()
T1.Text = ""
T1.Focus()
s = 0
End If
End If
End If
End Sub
ถ้าตัวนี้เข้าใจว่าน่าจะติดเงื่อนไข IF ครับ ลอง Debug ดูว่ามันเข้าเงื่อนไขในการ Update หรือเปล่าครับ