Imports System.Data
Imports System.Data.SqlClient
Public Class frmpurchase
Dim connect As New SqlConnection
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Private Sub btncus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncus.Click
Dim searchcustomer As New searchcustomer
searchcustomer.MdiParent = frmmain
searchcustomer.Show()
Me.Hide()
End Sub
Private Sub btnemp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnemp.Click
Dim sqlshowemp As String
Dim empid As String
empid = txtempid.Text
connect.ConnectionString = "Data Source=(local);Initial Catalog=udomdate;Integrated Security=True"
connect.Open()
sqlshowemp = "select * from employee where empid='" & empid & "'"
da = New SqlDataAdapter(sqlshowemp, connect)
da.Fill(ds, "emp")
txtempname.Text = ds.Tables("emp").Rows(0)("empname").ToString
ds.Clear()
connect.Close()
End Sub
Private Sub frmpurchase_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sqlshowproduct As String
Dim i As Integer ' i มาจาก interval คือการวนลูบข้อมูล รหัสสินค้าสู่ combobox
connect.ConnectionString = "Data Source=(local);Initial Catalog=udomdate;Integrated Security=True"
connect.Open()
sqlshowproduct = "select * from product"
da = New SqlDataAdapter(sqlshowproduct, connect)
da.Fill(ds, "pro")
For i = 0 To ds.Tables("pro").Rows.Count - 1
cboproid.Items.Add(ds.Tables("pro").Rows(i)("proid").ToString)
Next
connect.Close()
End Sub
Private Sub cboproid_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboproid.SelectedIndexChanged
Dim sqlshowproduct As String
Dim proid As String
proid = cboproid.SelectedItem
connect.ConnectionString = "Data Source=(local);Initial Catalog=udomdate;Integrated Security=True"
connect.Open()
sqlshowproduct = "select * from product where proid='" & proid & "'"
da = New SqlDataAdapter(sqlshowproduct, connect)
da.Fill(ds, "pro1")
txtproname.Text = ds.Tables("pro1").Rows(0)("proname").ToString
txtproprice.Text = ds.Tables("pro1").Rows(0)("proprice").ToString
txtprostock.Text = ds.Tables("pro1").Rows(0)("prostock").ToString
ds.Clear()
connect.Close()
End Sub
Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
'ประกาศตัวแปรที่เกี่ยวข้อง
Dim proid, proname As String
Dim proprice, prosum As Double 'prosum คือตัวแปรรับผลรวมจาก จำนวน * ราคา
Dim pronum As Integer
' เนื่องจากต้องตรวจสอบรายการที่ซื้อด้วย จะต้องใช้ วนลูป จึงต้องประกาศตัวแปร
Dim i As Integer
'ในการเก็บข้อมูลแต่ละรายการสู่ listview จะต้องกระทำพร้อมกัน โดยผ่านตัวแปร listview
'ดังนั้นเพื่อให้สะดวกในการจัดเก็บข้อมูลสู่ listview จึงควรนำค่าที่ได้จาก combobox และ textbox
'ใส่ตัวแปร array
Dim proitem(5) As String
Dim lvpro As New ListViewItem
' ตรวจสอบว่ามีการป้อนข้อมูลไว้ใน textbox จำนสน เรียบร้อย
If txtnum.Text = "" Then
MessageBox.Show("กรุณาป้อนจำนวน")
txtnum.Focus()
Exit Sub
Else
If txtnum.text <= 0 Then
MessageBox.Show("กรุณาป้อนจำนวนซื้อที่มากกว่า 0")
txtnum.clear()
txtnum.focus()
Exit Sub
Else
proid = cboproid.SelectedItem
pronum = txtnum.Text
For i = 0 To lsvbuy.Items.Count - 1
If lsvbuy.Items(i).SubItems(0).Text = proid Then
If (MessageBox.Show("คุณต้องการซื้อเพิ่มใช่หรือไม่", "ยินยันการซื้อ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then
pronum = pronum + lsvbuy.Items(i).SubItems(3).Text
lsvbuy.Items(i).Remove()
Exit For
Else 'ถ้าเลือก no คือไม่เพิ่มจำนวน
Exit Sub
End If
End If
Next
proname = txtproname.Text
proprice = txtproprice.Text
prosum = pronum * proprice
proitem(0) = proid
proitem(1) = proname
proitem(2) = proprice
proitem(3) = pronum
proitem(4) = prosum
lvpro = New ListViewItem(proitem)
lsvbuy.Items.Add(lvpro)
End If
End If
End Sub
Private Sub btndel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndel.Click
Dim itemdel As Object
For Each itemdel In lsvbuy.CheckedItems
If (MessageBox.Show("จะลบจริงๆหรอ", "ลบให้แล้วนะ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then
lsvbuy.Items.Remove(itemdel)
End If
Next
End Sub
Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
Dim cusid As String
Dim empid As String
Dim proid As String
Dim proprice As Double
Dim pronum As Integer
Dim prosum As Double
Dim purtotal As Double
Dim purdate As Date
Dim purid As Integer
Dim i As Integer
Dim sqlcheckorder As String
Dim sqladdorder As String
Dim sqladddetail As String
Dim cmd As New SqlCommand
Dim cmd1 As New SqlCommand
If txtcusid.Text = "" Then
MessageBox.Show("กรุณาเลือกลูกค้า")
Else
If txtempid.Text = "" Then
MessageBox.Show("กรุณาระบุพนักงานขาย")
Else
cusid = txtcusid.Text
empid = txtempid.Text
If lsvbuy.Items.Count <> 0 Then
For i = 0 To lsvbuy.Items.Count - 1
purtotal = purtotal + lsvbuy.Items(i).SubItems(4).Text
Next
End If
purdate = Today
connect.ConnectionString = "Data Source=(local);Initial Catalog = udomdate;Integrated Security=true"
connect.Open()
sqladdorder = "insert into purchase(cusid,empid,purdate,purtotal) values('" & cusid & "','" & empid & "'," & purdate & "," & purtotal & ")"
cmd.CommandType = CommandType.Text
cmd.CommandText = sqladdorder
cmd.Connection = connect
cmd.ExecuteNonQuery()
sqlcheckorder = "select max(purid) from purchase"
da = New SqlDataAdapter(sqlcheckorder, connect)
da.Fill(ds, "pur")
purid = ds.Tables("pur").Rows(0).Item(0).ToString
For i = 0 To lsvbuy.Items.Count - 1
proid = lsvbuy.Items(i).SubItems(0).Text
proprice = lsvbuy.Items(i).SubItems(2).Text
pronum = lsvbuy.Items(i).SubItems(3).Text
prosum = lsvbuy.Items(i).SubItems(4).Text
sqladddetail = "insert into detail(purid,proid,proprice,pronum,prosum) values(" & purid & ",'" & proid & "'," & proprice & "," & pronum & "," & prosum & " ) "
cmd.CommandType = CommandType.Text
cmd.CommandText = sqladddetail
cmd.Connection = connect
cmd.ExecuteNonQuery()
Next
MessageBox.Show("การสั่งซื้อสินค้าเรียบร้อย")
ds.Clear()
connect.Close()
txtcusid.Clear()
txtcusname.Clear()
txtcusaddress.Clear()
txtcustel.Clear()
txtempname.Clear()
cboproid.SelectedItem = ""
txtproname.Clear()
txtproprice.Text = 0
txtprostock.Text = 0
txtnum.Text = 0
lsvbuy.Items.Clear()
End If
End If
End Sub
End Class
Tag : .NET, Ms SQL Server 2008, VBScript, VS 2010 (.NET 4.x)
Public Sub CreateCommand(ByVal queryString As String, _
ByVal connectionString As String)
Using connection As New SqlConnection(connectionString)
Dim command As New SqlCommand(queryString, connection)
command.Connection.Open()
command.ExecuteNonQuery()
End Using
End Sub