 |
|
ช่วยดูโค๊ดตัดสต็อกสินค้าให้หน่อยครับ รันผ่านแต่จำนวนสินค้ายังเท่าเดิมครับ |
|
 |
|
|
 |
 |
|
นี่ครับโค๊ด
Code (VB.NET)
'''''''''''''การตัดสต็อกสินค้า'''''''''''''''''''''
Dim QuantityFirst As Integer = 0
Dim QuantityNow As Integer = 0
Dim QuantitySale As Integer = 0
Dim sqlAUp As String = ""
For i = 0 To lv_salepro.Items.Count - 1
sb.Remove(0, sb.Length)
sb.Append("SELECT Quantity From Product WHERE Product_ID=id")
sqlAUp = sb.ToString()
cmd = New OleDbCommand
With cmd
.CommandText = sqlAUp
.CommandType = CommandType.Text
.Connection = conn
.Transaction = tr
.Parameters.Clear()
.Parameters.AddWithValue("id", lv_salepro.Items(i).SubItems(0).Text)
.ExecuteNonQuery()
End With
Dim dt As DataTable
dt = New DataTable("Quantity")
dr = cmd.ExecuteReader
dt.Load(dr)
QuantityFirst = CInt(CStr((dt.Rows(0)("Quantity"))))
QuantitySale = CInt((lv_salepro.Items(i).SubItems(4).Text))
QuantityNow = QuantityFirst - QuantitySale
sb.Remove(0, sb.Length)
sb.Append("UPDATE Product SET Quantity=@quantity WHERE Product_ID=id")
sqlAUp = sb.ToString()
cmd = New OleDbCommand
With cmd
.CommandText = sqlAUp
.CommandType = CommandType.Text
.Connection = conn
.Transaction = tr
.Parameters.Clear()
.Parameters.AddWithValue("id", lv_salepro.Items(i).SubItems(0).Text)
.Parameters.AddWithValue("quantity", QuantityNow.ToString)
.ExecuteNonQuery()
End With
Next
tr.Commit()
MessageBox.Show("บันทึกข้อมูลการขายสินค้าเรียบร้อยแล้ว!!!", "ผลการทำงาน", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
คือเมื่อผมกดบันทึกแล้ว ก็ทำการบันทึกปกติครับ ขึ้นmsgว่า บันทึกข้อมูลการขายสินค้าเรียบร้อยแล้ว แต่พอไปดูจำนวนสินค้ายังเท่าเดิมครับ ไม่ขึ้นเออเร่อ เลยไม่รู้ว่าผิดตรงไหน ขอคำแนะนำด้วยครับ
ป.ล. โค๊ดมั่วเองครับ อิอิ
Tag : .NET, Ms Access, VB.NET
|
|
 |
 |
 |
 |
Date :
2011-01-10 17:40:12 |
By :
aneklove |
View :
3001 |
Reply :
25 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
@quantity เเล้วไม่ต้อง @id เหรอครับ อันนี้ก็ไม่ทราบนะ ตกลงเเล้วต้องมี @ ไหมครับ
|
 |
 |
 |
 |
Date :
2011-01-10 18:23:09 |
By :
EucifeR |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ต้องมี @ ครับ แก้ไขแล้วครับ แต่ยังเหมือนเดิม
|
 |
 |
 |
 |
Date :
2011-01-10 18:31:52 |
By :
aneklove |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่มีคนตอบเยย
โปรเจคจบจะไม่ทันแล้วครับ ช่วยด้วยครับ
|
 |
 |
 |
 |
Date :
2011-01-10 22:58:44 |
By :
aneklove |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองดูค่า lv_salepro.Items(i).SubItems(0).Text ว่าค่าถูกต้องไหมครับ ถ้าถูกต้อง ลอง Debug ดูว่า ตอน Run มันวิ่งผ่าน บรรทัดที่ 48 หรือไม่
|
 |
 |
 |
 |
Date :
2011-01-11 08:26:19 |
By :
kaimuk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอโทษครับ ทำยังไงเหรอครับ ผมสงสัยมันจะผิดที่คำสั่ง update รึป่าวครับ
เพราะว่าตรงตัวแปร 3 ตัวที่ทำการลบจำนวนสินค้านั่นผมลองเอาเม้าท์จิ้มดูก็ได้จำนวนถูกต้องครับ
ผมเลยสงสัยว่ามันจะผิดที่คำสั่งอัพเดด เพราะผมไม่เคยเขียนอัพเดดเฉพาะฟิลด์เดียวอ่ะครับ
เคยเขียนแต่ให้มันอัพเดดทุกฟิลด์เลยครับ
|
 |
 |
 |
 |
Date :
2011-01-11 20:06:46 |
By :
aneklove |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
sb.Append("UPDATE Product SET Quantity=@quantity WHERE Product_ID=@id")
.Parameters.AddWithValue("@id", lv_salepro.Items(i).SubItems(0).Text)
.Parameters.AddWithValue("@quantity", QuantityNow.ToString)
ลองเอาไปแทนดู
|
 |
 |
 |
 |
Date :
2011-01-12 08:49:26 |
By :
kaimuk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณคุณ KaimuK มากนะครับ แต่ลองเปลี่ยนตามที่ว่าแล้วยังเหมือนเดิมครับ
|
 |
 |
 |
 |
Date :
2011-01-12 12:38:06 |
By :
aneklove |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทดลองเอา .Transaction = tr ออกแล้วดูว่าได้หรือไม่ครับ
|
 |
 |
 |
 |
Date :
2011-01-12 13:36:01 |
By :
kaimuk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอา .Transaction = tr ออกแล้วเป็อย่างนี้ครับ

|
 |
 |
 |
 |
Date :
2011-01-12 14:34:45 |
By :
aneklove |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
นี่ครับโค๊ดทั้งหมด ทั้งการบันทึกการซื้อ-ขาย-ตัดสต็อกด้วยครับ
Code (VB.NET)
Private Sub AddSalePro()
If tb_emp.Text.Trim = "" Then
MessageBox.Show("กรุณาระบุรหัสพนักงาน!!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
tb_emp.Focus()
Exit Sub
End If
If tb_mem.Text.Trim = "" Then
MessageBox.Show("กรุณาระบุรหัสมาชิก!!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
tb_mem.Focus()
Exit Sub
End If
If lv_salepro.Items.Count = 0 Then
MessageBox.Show("กรุณาระบุสินค้าที่ขาย!!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
tr = conn.BeginTransaction()
'''''''''''''บันทึกการขายสินค้าของพนักงาน'''''''''''''''''''''
Dim sqladd As String = ""
Dim i As Integer
For i = 0 To lv_salepro.Items.Count - 1
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO SaleProduct (Product_ID,Employee_ID,Sdate,Quantity) ")
sb.Append("VALUES (@proid,@empid,@date,@quantity)")
sqladd = sb.ToString()
cmd = New OleDbCommand
With cmd
.CommandText = sqladd
.CommandType = CommandType.Text
.Connection = conn
.Transaction = tr
.Parameters.Clear()
.Parameters.AddWithValue("proid", lv_salepro.Items(i).SubItems(0).Text)
.Parameters.AddWithValue("empid", tb_emp.Text.ToString)
.Parameters.AddWithValue("date", CDate(Date_sale.Value.Date))
.Parameters.AddWithValue("quantity", lv_salepro.Items(i).SubItems(4).Text)
.ExecuteNonQuery()
End With
Next
'''''''''''''บันทึกการซื้อสินค้าของสมาชิก'''''''''''''''''''''
sqladd = ""
For i = 0 To lv_salepro.Items.Count - 1
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO BuyProduct (Member_ID,Product_ID,Quantity,PriceUnit,Bdate) ")
sb.Append("VALUES (@memid,@proid,@quantity,@punit,@date)")
sqladd = sb.ToString()
cmd = New OleDbCommand
With cmd
.CommandText = sqladd
.CommandType = CommandType.Text
.Connection = conn
.Transaction = tr
.Parameters.Clear()
.Parameters.AddWithValue("memid", tb_mem.Text.ToString)
.Parameters.AddWithValue("proid", lv_salepro.Items(i).SubItems(0).Text)
.Parameters.AddWithValue("Ouantity", lv_salepro.Items(i).SubItems(4).Text)
.Parameters.AddWithValue("punit", tb_price.Text)
.Parameters.AddWithValue("date", CDate(Date_sale.Value.Date))
.ExecuteNonQuery()
End With
Next
'''''''''''''การตัดสต็อกสินค้า'''''''''''''''''''''
Dim QuantityFirst As Integer = 0
Dim QuantityNow As Integer = 0
Dim QuantitySale As Integer = 0
Dim sqlAUp As String = ""
For i = 0 To lv_salepro.Items.Count - 1
sb.Remove(0, sb.Length)
sb.Append("SELECT Quantity From Product WHERE Product_ID=@id")
sqlAUp = sb.ToString()
cmd = New OleDbCommand
With cmd
.CommandText = sqlAUp
.CommandType = CommandType.Text
.Connection = conn
.Transaction = tr
.Parameters.Clear()
.Parameters.AddWithValue("id", lv_salepro.Items(i).SubItems(0).Text)
.ExecuteNonQuery()
End With
Dim dt As DataTable
dt = New DataTable("Quantity")
dr = cmd.ExecuteReader
dt.Load(dr)
QuantityFirst = CInt(CStr((dt.Rows(0)("Quantity"))))
QuantitySale = CInt((lv_salepro.Items(i).SubItems(4).Text))
QuantityNow = QuantityFirst - QuantitySale
sb.Remove(0, sb.Length)
sb.Append("UPDATE Product SET Quantity=@quantity WHERE Product_ID=@id")
sqlAUp = sb.ToString()
cmd = New OleDbCommand
With cmd
.CommandText = sqlAUp
.CommandType = CommandType.Text
.Connection = conn
.Transaction = tr
.Parameters.Clear()
.Parameters.AddWithValue("id", lv_salepro.Items(i).SubItems(0).Text)
.Parameters.AddWithValue("quantity", QuantityNow)
.ExecuteNonQuery()
End With
Next
tr.Commit()
MessageBox.Show("บันทึกข้อมูลการขายสินค้าเรียบร้อยแล้ว!!!", "ผลการทำงาน", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
End Sub
|
 |
 |
 |
 |
Date :
2011-01-12 14:36:53 |
By :
aneklove |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใส่
Try
'***โค้ด Update
Catch
'***ข้อมูลที่ผิดพลาด
End Try
จะได้รู้ว่าผิดอะไรลองดูครับ
|
 |
 |
 |
 |
Date :
2011-01-12 15:26:44 |
By :
misteryou |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใส่อย่างนี้ใช่มั้ยครับ ลองแล้วเหมือนเดิมครับ
ผมลองเขียนคำสั่ง sql ให้ผิดยังไม่เออเร่อเลยครับ
Code (VB.NET)
Try
sb.Remove(0, sb.Length)
sb.Append("UPDATE Product SET Quantity=@quantity WHERE Product_ID=@id")
sqlUp = sb.ToString()
cmd = New OleDbCommand
With cmd
.CommandText = sqlUp
.CommandType = CommandType.Text
.Connection = conn
.Transaction = tr
.Parameters.Clear()
.Parameters.AddWithValue("id", lv_salepro.Items(i).SubItems(0).Text)
.Parameters.AddWithValue("quantity", QuantityNow)
.ExecuteNonQuery()
End With
Catch ex As OleDbException
MsgBox(ex.Message)
Exit Sub
End Try
รู้สึกเหมือนมันจะโดดข้ามคำสั่งนี้ไปเลยครับ เหอๆๆ
|
ประวัติการแก้ไข 2011-01-12 17:34:33
 |
 |
 |
 |
Date :
2011-01-12 17:33:17 |
By :
aneklove |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Catch ex As Exception
MessageBox.Show(ex.Message, "ไม่พร้อมใช้งาน", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
ใส่งี้ดูครับ
|
 |
 |
 |
 |
Date :
2011-01-12 17:38:35 |
By :
EucifeR |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณ คุณ Shadow[L] มากครับ แต่ใส่แล้วยังเหมือนเดิม
จนปัญญาละครับ
พอกดบันทึกก็ขึ้นอย่างนี้เลยครับ

|
ประวัติการแก้ไข 2011-01-12 17:53:28
 |
 |
 |
 |
Date :
2011-01-12 17:48:23 |
By :
aneklove |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
1. เอาบรรทัดที่มีคำสั่ง 2 บรรทัดนี้
tr = conn.BeginTransaction()
.Transaction = tr
ออกให้หมด
2. ทำ Breakpoint แล้วลองรันทีละ Step ดูดีกว่า
เผื่อจะเจอเงื่อนงำ
|
 |
 |
 |
 |
Date :
2011-01-12 17:56:49 |
By :
หางอึ่ง |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองแล้วทุกอย่างครับ ข้าน้อยจนปัญญา
|
 |
 |
 |
 |
Date :
2011-01-12 18:53:44 |
By :
aneklove |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ส่ง PM ไปหาคุณ aneklove แล้วนะถ้าสนใจทำตามนั้นเลยครับ
|
 |
 |
 |
 |
Date :
2011-01-12 20:48:54 |
By :
EucifeR |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทดลอง เปลี่ยน Code จากบรรทัด ที่ 109 - 123 เป็นอันนี้แทน
Code (VB.NET)
sb.Append(“UPDATE Product SET Quantity=”)
sb.Append(QuantityNow)
sb.Append(“ WHERE Product_ID=’”)
sb.Append(lv_salepro.Items(i).SubItems(0).Text)
sb.Append(“’”)
sqlAUp = sb.ToString()
cmd = New OleDbCommand
With cmd
.CommandText = sqlAUp
.CommandType = CommandType.Text
.Connection = conn
.Transaction = tr
.ExecuteNonQuery()
End With
แล้วทดลอง ดีบัก ดูตรง sqlAUp ว่าได้ค่าอะไร กรุณา Post ให้ดูด้วยนะครับว่า ได้ค่าอะไร
|
 |
 |
 |
 |
Date :
2011-01-13 08:12:51 |
By :
kaimuk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ดีบักแล้ว

ดู sqlaup อย่างนี้หรือเปล่าครับ

|
 |
 |
 |
 |
Date :
2011-01-13 13:04:37 |
By :
aneklove |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ค่า Parameter มันไม่ส่งเข้าไปครับบบบบบบบบบบบบบ
|
 |
 |
 |
 |
Date :
2011-01-13 13:58:03 |
By :
misteryou |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอขอบคุณ คุณ KaimuK และพี่ๆทุกท่านนะครับ ทำได้แล้วครับ
แก้โค๊ดที่คุณ KaimuK ให้มานิดหน่อย ผมใส่ '"& &"' ครอบ quantity และ lv_salepro.Items(i).SubItems(0).Text
ผ่านแล้วคร๊าบบบ
ขอบคุณทุกท่านมากๆๆๆๆๆๆๆๆ
Code (VB.NET)
Try
sb.Remove(0, sb.Length)
sb.Append("UPDATE Product SET Quantity=")
sb.Append("'" & QuantityNow & "'")
sb.Append(" WHERE Product_ID=")
sb.Append("'" & lv_salepro.Items(i).SubItems(0).Text & "'")
sqlAUp = sb.ToString()
cmd = New OleDbCommand
With cmd
.CommandText = sqlAUp
.CommandType = CommandType.Text
.Connection = conn
.Transaction = tr
.ExecuteNonQuery()
End With
|
 |
 |
 |
 |
Date :
2011-01-13 17:14:32 |
By :
aneklove |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
กรรม
เพิ่งเจอคนเก็บข้อมูลยอดคงเหลือ ด้วย Text นี่แหละ
ยอมรับว่าเครียด

|
ประวัติการแก้ไข 2011-01-13 18:04:28
 |
 |
 |
 |
Date :
2011-01-13 18:01:28 |
By :
หางอึ่ง |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
กระัทู้นี้มันส์มากๆ ช่วยกันบี้ซะปิดงานเลย
ขอบคุณพี่น้องทุกๆ คน
|
 |
 |
 |
 |
Date :
2011-01-13 19:02:07 |
By :
mrsitti |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอโค้ดแบบสมบูรณ์เลยได้มั้ยค่ะ.....พอดีว่า...ก็ได้ทำเรื่องนี้เหมือนกัน.....แต่ยังทำไม่ได้เลย....ขอดูดค้ดแบบเต็มๆหน่อยนะคะ....
|
 |
 |
 |
 |
Date :
2011-03-28 22:19:35 |
By :
pimjai |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
'แก้ 2 ที่ครับ
sb.Append("SELECT Quantity From Product WHERE Product_ID=id")
sb.Append("UPDATE Product SET Quantity=@quantity WHERE Product_ID=id")
'เป็นแบบนี้ แตกต่างกันทีเครื่องหมาย "?" ครับ ลองดูครับ Good Luck
sb.Append("SELECT Quantity From Product WHERE Product_ID=?")
sb.Append("UPDATE Product SET Quantity=? WHERE Product_ID=?")
|
 |
 |
 |
 |
Date :
2011-04-27 15:10:29 |
By :
Lord Darkhallow |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|