 |
|
สอบถามการตัดสต๊อก ใน vb มี code ปุ่ม save ของ order สั่งซื้อ แต่อยากเขียนโค้ดตัดสต๊อก ลงไป ไม่รู้ จะเพิ่ม มัน ลงไปตรงไหน ขอคำแนะนำด้วย ขอบคุนค่ะ |
|
 |
|
|
 |
 |
|
Private Sub bsa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bsa.Click
If tpl.Items.Count > 0 Then
If MessageBox.Show("Do you want to save this Order?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
tr = Conn.BeginTransaction()
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO Orders (CustomerID,OrderDate,Total)")
sb.Append(" VALUES ('" & tcid.Text & "','" & DateTime.Today & "','" & tt.Text & "')")
Dim sqlSave As String = sb.ToString()
Dim com = Conn.CreateCommand()
With com
.CommandType = CommandType.Text
.CommandText = sqlSave
.Connection = Conn
.Transaction = tr
.ExecuteNonQuery()
End With
sb.Remove(0, sb.Length)
sb.Append("SELECT TOP 1 OrderID FROM Orders")
sb.Append(" ORDER BY OrderID DESC")
sqlSave = sb.ToString()
Dim LastOrderID As Integer
With com
.CommandType = CommandType.Text
.CommandText = sqlSave
.Connection = Conn
dr = .ExecuteReader()
If dr.HasRows Then
dr.Read()
LastOrderID = dr.GetInt32(dr.GetOrdinal("OrderID"))
Else
LastOrderID = 10000
End If
End With
dr.Close()
Dim i As Integer
For i = 0 To tpl.Items.Count - 1
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO OrderDetails (OrderID,ProductID,SoldPrice,Quantity)")
sb.Append(" VALUES (" & LastOrderID & ",")
sb.Append("'" & tpl.Items(i).SubItems(0).Text & "','")
sb.Append(tpl.Items(i).SubItems(2).Text & "','")
sb.Append(tpl.Items(i).SubItems(3).Text & "')")
sqlSave = sb.ToString()
With com
.CommandText = sqlSave
.ExecuteNonQuery()
End With
Next
tr.Commit()
sb.Remove(0, sb.Length)
sb.Append("SELECT OrderID")
sb.Append(" FROM Orders")
sb.Append(" WHERE (" & LastOrderID & ")")
Dim sqlOrder As String = sb.ToString
Dim dtOrder As DataTable
With com
.CommandType = CommandType.Text
.CommandText = sqlOrder
.Connection = Conn
dr = .ExecuteReader()
If dr.HasRows Then
dtOrder = New DataTable()
dtOrder.Load(dr)
MessageBox.Show("Save Order done.", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End With
tpl.Clear()
ClearProductData()
ClearCutomerData()
tt.Text = "0"
tpid.Focus()
End If
End If
End Sub
Tag : .NET, Ms Access, VB.NET
|
|
 |
 |
 |
 |
Date :
2012-03-11 10:36:54 |
By :
chopin13 |
View :
2764 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
For i As Integer = 0 To Listview1.Items.Count - 1
Dim Upstock As String = ""
Upstock = ("Update Products")
Upstock &= (" set Stock = Stock - " & Listview1.Items(i).SubItems(4).Text & " where ProductID = @ProID")
With cm
.CommandType = CommandType.Text
.CommandText = Upstock
.Connection = cn
.Parameters.Clear()
.Parameters.Add("@ProID", SqlDbType.NVarChar, 10).Value = Listview1.Items(i).SubItems(1).Text
.ExecuteNonQuery()
End With
Next
ที่ต้องมี For เพราะบ้างสินค้าไม่ได้มีเพียงแค่รายการเดียว อาจจะมี 2 3 4 5 รายการก้ได้ ...For iมันจะ Update ตั้งแต่แถวที่ 0 จนถึงแถวสุดท้ายใน listview..
[font=Verdana]ไม่รุ้ว่าต้องการแบบนี้หรือป่าว ลองไปประยุกต์ใช้เอาครับ [/font]
|
 |
 |
 |
 |
Date :
2012-03-11 12:43:12 |
By :
kaing_chaiyan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
.Parameters.Add("@ProID", SqlDbType.NVarChar, 10).Value = Listview1.Items(i).SubItems(1).Text มัน error ตรงบรรทัดนี้อ่ะค่ะ ไม่รุแก้ไง
|
 |
 |
 |
 |
Date :
2012-03-12 11:33:30 |
By :
chopin13 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Private Sub btnSave_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim sqlAdd As String
Dim sqlEdit As String
Dim comAdd As New OleDbCommand
Select Case btnChk 'ตรวจสอบการคลิกปุ่ม
Case "Add" 'กรณีถ้าคลิกปุ่ม เพิ่ม
'ตรวจสอบการป้อนข้อมูลว่าครบถ้วนหรือไม่
If (txtName.Text = "") Or (txtPosition.Text = "") Or (txtSex.Text = "") Or (txtSalary.Text = "") Then
MsgBox("โปรดป้อนข้อมูลให้ครบ")
txtName.Focus()
Exit Sub
End If
'คำสั่ง SQL เพื่อแทรกข้อมูลในตาราง tblCustomer
sqlAdd = "Insert Into tblCustomer(CustName,Position,Sex,Salary)"
sqlAdd &= " VALUES ('" & txtName.Text & "',"
sqlAdd &= "'" & txtPosition.Text & "',"
sqlAdd &= "'" & txtSex.Text & "',"
sqlAdd &= "'" & txtSalary.Text & "')" 'การสั่งออปเจค comAdd ให้ทำงาน
With comAdd
.CommandType = CommandType.Text 'เซตประเภทคอมมาน
.CommandText = sqlAdd 'ใส่ชุดคำสั่ง SQL
.Connection = Conn 'บอกว่าจะไปทางไหน
.ExecuteNonQuery() 'ไปทำงานได้ (ส่งคำสั่งไปแบบไม่ต้องการผลลัพธ์คืนมา)
End With
MsgBox("บันทึกข้อมูลเรียบร้อย") 'แสดง MessageBox ว่าได้บันทึกข้อมูลแล้ว
ShowAllCustomers() 'แสดงข้อมูลใน DataGridView
Case "Update" 'กรณีถ้าคลิกปุ่ม แก้ไข
'คำสั่ง SQL ในการปรับปรุงข้อมูลในตาราง tblCustomer ตาม รหัสพนักงาน
sqlEdit = "UPDATE tblCustomer"
sqlEdit &= " SET CustName = '" & txtName.Text & "',"
sqlEdit &= " Position = '" & txtPosition.Text & "',"
sqlEdit &= " Sex = '" & txtSex.Text & "',"
sqlEdit &= " Salary = '" & txtSalary.Text & "'"
sqlEdit &= " WHERE (CustID = " & CInt(txtID.Text) & ")"
'การสั่งออปเจค comAdd ให้ทำงาน
With comAdd
.CommandType = CommandType.Text
.CommandText = sqlEdit
.Connection = Conn
.ExecuteNonQuery()
End With
MsgBox("แก้ไขข้อมูลเรียบร้อย") 'แสดง MessageBox ว่าได้แก้ไขข้อมูลแล้ว
ShowAllCustomers() 'แสดงข้อมูลใน DataGridView
Case Else
MsgBox("ไม่มีการทำงาน")
End Select
btnInsert.Enabled = True 'ปุ่ม เพิ่ม เปิดการทำงาน
btnUpdate.Enabled = True 'ปุ่ม แก้ไข เปิดการทำงาน
btnDelete.Enabled = True 'ปุ่ม ลบ เปิดการทำงาน
btnSave.Enabled = False 'ปุ่ม ตกลง เปิดการทำงาน
btnCancel.Enabled = False 'ปุ่ม ยกเลิก เปิดการทำงาน
End Sub
คำถาม เวลารันแล้วมันจะติดอยู่ที่ Insert Into มีวิธีแก้อย่างงัยคะ
|
 |
 |
 |
 |
Date :
2012-10-01 20:15:58 |
By :
kk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|