 |
|
insert ข้อมูลจาก Gridview ที่มีมากกว่า 1 Row ลง DB เจอ Error PK ซ้ำอ่ะค่ะ |
|
 |
|
|
 |
 |
|
insert ข้อมูลจาก Gridview มากกว่า 1 Row เจอ Error: Violation of PRIMARY KEY constraint 'PK_Orders'. Cannot insert duplicate key in object 'dbo.Orders'. The duplicate key value is (OR1500002).
The statement has been terminated.
นี่คือโค้ดค่ะที่เขียนไว้ค่ะ เราต้องแก้โค้ดยังไงดีคะให้ถ้ามีมากกว่า 1 row จะรัน AutoNumber ได้อ่ะค่ะ
Code (VB.NET)
Protected Sub btnsubmit_Click(ByVal sender As Object, e As ImageClickEventArgs) Handles btnsubmit.Click
Dim dt As DataTable = DirectCast(ViewState("Order"), DataTable)
Dim SqlCon As New SqlConnection(con)
Dim da As New SqlDataAdapter
Dim i As Integer
Dim AuOrderID As String = ORID()
cmd = New SqlCommand(strSQL, SqlCon)
strSQL = "INSERT INTO Orders(orderID,empID,orderDate,createdDate,createdBy)"
strSQL &= "VALUES('" & AuOrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & lblempid.Text & "')"
SqlCon.Open()
For i = 0 To dt.Rows.Count - 1
With dt.Rows(i)
With cmd
.CommandType = CommandType.Text
.CommandText = strSQL
.Connection = SqlCon
.ExecuteNonQuery()
End With
End With
Next
SqlCon.Close()
dt.Clear()
GridView1.DataSource = dt
GridView1.DataSource = Nothing
End Sub
'*** AUTOKEY ***'
Public Function ORID() As String
Dim iMAX As Integer = 1
Dim intNum As String = ""
Dim cmd As New SqlCommand
With cmd
.Connection = SqlCon
.CommandText = "SELECT MAX(RIGHT(orderID,5)) As orderID FROM Orders"
SqlCon.ConnectionString = con
SqlCon.Open()
If IsDBNull(.ExecuteScalar()) = True Then
intNum = "OR" & CStr(Now.Year).Substring(2) & "0000" & iMAX
Else
iMAX = .ExecuteScalar() + 1
End If
If iMAX <= 9 Then
intNum = "OR" & CStr(Now.Year).Substring(2) & "0000" & iMAX
ElseIf iMAX <= 99 Then
intNum = "OR" & CStr(Now.Year).Substring(2) & "000" & iMAX
ElseIf iMAX <= 999 Then
intNum = "OR" & CStr(Now.Year).Substring(2) & "00" & iMAX
ElseIf iMAX <= 9999 Then
intNum = "OR" & CStr(Now.Year).Substring(2) & "0" & iMAX
ElseIf iMAX <= 99999 Then
intNum = "OR" & CStr(Now.Year).Substring(2) & iMAX
End If
End With
SqlCon.Close()
Return intNum
End Function
Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), VB.NET
|
ประวัติการแก้ไข 2015-11-28 13:14:01
|
 |
 |
 |
 |
Date :
2015-11-28 12:12:19 |
By :
heaunn |
View :
2302 |
Reply :
102 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Error ก็แจ้งแล้วนะครับ ถ้า เป็น Key ซ้ำ มันก็ Insert ไม่ได้ 
|
 |
 |
 |
 |
Date :
2015-11-28 14:05:01 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มีวิธีเขียนโค้ดวนให้ insert แบบคีย์ AutoNumber ไม่ซ้ำไหมค่ะ
เพราเขียนโค้ดรัน autonumber ไว้แล้ว แต่พอมาเขียน insert ลง DB จาก Gridview ที่มีมากกว่า1 row มันก็ insert PK ซ้ำ
|
ประวัติการแก้ไข 2015-11-28 15:16:40 2015-11-28 15:18:15 2015-11-28 15:21:08
 |
 |
 |
 |
Date :
2015-11-28 15:16:05 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Protected Sub btnsubmit_Click(ByVal sender As Object, e As ImageClickEventArgs) Handles btnsubmit.Click
Dim dt As DataTable = DirectCast(ViewState("Order"), DataTable)
Dim SqlCon As New SqlConnection(con)
Dim da As New SqlDataAdapter
Dim i As Integer
cmd = New SqlCommand(strSQL, SqlCon)
For i = 0 To dt.Rows.Count - 1
With dt.Rows(i)
With cmd
strSQL = "INSERT INTO Orders(orderID,empID,orderDate,createdDate,createdBy)"
strSQL &= "VALUES('" & ORID() & "',"
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & lblempid.Text & "')"
SqlCon.Open()
.CommandType = CommandType.Text
.CommandText = strSQL
.Connection = SqlCon
.ExecuteNonQuery()
End With
End With
Next
SqlCon.Close()
dt.Clear()
GridView1.DataSource = dt
GridView1.DataSource = Nothing
End Sub
'*** AUTOKEY ***'
Public Function ORID() As String
Dim iMAX As Integer = 1
Dim intNum As String = ""
Dim cmd As New SqlCommand
With cmd
.Connection = SqlCon
.CommandText = "SELECT MAX(RIGHT(orderID,5)) As orderID FROM Orders"
SqlCon.ConnectionString = con
SqlCon.Open()
If IsDBNull(.ExecuteScalar()) = True Then
intNum = "OR" & CStr(Now.Year).Substring(2) & "0000" & iMAX
Else
iMAX = .ExecuteScalar() + 1
End If
If iMAX <= 9 Then
intNum = "OR" & CStr(Now.Year).Substring(2) & "0000" & iMAX
ElseIf iMAX <= 99 Then
intNum = "OR" & CStr(Now.Year).Substring(2) & "000" & iMAX
ElseIf iMAX <= 999 Then
intNum = "OR" & CStr(Now.Year).Substring(2) & "00" & iMAX
ElseIf iMAX <= 9999 Then
intNum = "OR" & CStr(Now.Year).Substring(2) & "0" & iMAX
ElseIf iMAX <= 99999 Then
intNum = "OR" & CStr(Now.Year).Substring(2) & iMAX
End If
End With
SqlCon.Close()
Return intNum
End Function
แค่นี้ก็ได้แล้วนิครับ
เปลี่ยนจาก AuOrderID เป็น ORID()
|
 |
 |
 |
 |
Date :
2015-11-28 16:03:24 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากค่ะ ขอถามอีกคำถามนะค่ะว่า ถ้าจะ insert ข้อมูลลงอีก 1 ตาราง คือ ORderDetails โดยใช้ OrderID เดียวกับตาราง Orders ต้องทำยังไงค่ะ
|
ประวัติการแก้ไข 2015-11-28 16:56:19 2015-11-28 16:56:43 2015-11-28 17:00:58
 |
 |
 |
 |
Date :
2015-11-28 16:31:28 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Protected Sub btnsubmit_Click(ByVal sender As Object, e As ImageClickEventArgs) Handles btnsubmit.Click
Dim dt As DataTable = DirectCast(ViewState("Order"), DataTable)
Dim SqlCon As New SqlConnection(con)
Dim da As New SqlDataAdapter
Dim i As Integer
cmd = New SqlCommand(strSQL, SqlCon)
Dim AuOrderID As String
For i = 0 To dt.Rows.Count - 1
With dt.Rows(i)
With cmd
AuOrderID = ORID()
strSQL = "INSERT INTO Orders(orderID,empID,orderDate,createdDate,createdBy)"
strSQL &= "VALUES('" & AuOrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & lblempid.Text & "')"
SqlCon.Open()
.CommandType = CommandType.Text
.CommandText = strSQL
.Connection = SqlCon
.ExecuteNonQuery()
strSQL = " INSERT INTO ORderDetails(?????) VALUES(??????) " ' เอา AuOrderID มา INSERT แบบบ้านๆเลยครับ
.CommandText = strSQL
.ExecuteNonQuery()
End With
End With
Next
SqlCon.Close()
dt.Clear()
GridView1.DataSource = dt
GridView1.DataSource = Nothing
End Sub
ประมาณนี้ครับ
ถ้า ORderDetails มีหลายๆ rows ก็ for เหมือน Orders ครับ
|
 |
 |
 |
 |
Date :
2015-11-28 17:49:29 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ่อ ขอบคุณค่ะ จะลองทำตามคำแนะนำนะค่ะ 
|
 |
 |
 |
 |
Date :
2015-11-28 17:55:15 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Violation of PRIMARY KEY constraint 'PK_OrderDetails'. Cannot insert duplicate key in object 'dbo.OrderDetails'. The duplicate key value is (OR1500005).
The statement has been terminated.
ติดปัญหาเหมือนเดิมเลยค่ะ คือ insert ขอมูลลง OrderDetails ถ้ามีมากกว่า 1 row จะมีปัญหา PK ซ้ำอ่ะค่ะ ตอนนี้เขียนโค้ดแบบนี้ค่ะ 
Code (VB.NET)
Dim dt As DataTable = DirectCast(ViewState("Order"), DataTable)
Dim SqlCon As New SqlConnection(con)
Dim da As New SqlDataAdapter
Dim AuOrderID As String
cmd = New SqlCommand(strSQL, SqlCon)
For i As Integer = 0 To dt.Rows.Count - 1
With dt.Rows(i)
SqlCon.Open()
With cmd
AuOrderID = ORID()
strSQL = "INSERT INTO Orders(orderID,empID,orderDate,createdDate,createdBy)"
strSQL &= "VALUES('" & ORID() & "',"
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & lblempid.Text & "')"
.CommandType = CommandType.Text
.CommandText = strSQL
.Connection = SqlCon
.ExecuteNonQuery()
For j As Integer = 0 To dt.Rows.Count - 1
strSQL = "INSERT INTO OrderDetails(orderID,storefood_rkey,optionDetails,qty,unitPrice,amount)"
strSQL &= "VALUES('" & AuOrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("foodID"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("option"), String) & "',"
strSQL &= "'" & CType(dt.Rows(i)("qty"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("price"), Decimal) & "',"
strSQL &= "'" & CType(dt.Rows(i)("amount"), Decimal) & "')"
.CommandText = strSQL
.ExecuteNonQuery()
Next
End With
End With
Next
SqlCon.Close()
dt.Clear()
GridView1.DataSource = dt
GridView1.DataSource = Nothing
|
 |
 |
 |
 |
Date :
2015-11-28 20:35:19 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ออกแบบ. Database. ใหม่ครับ orderID ไม่ควรเปน Auto. นะครับเพราะมันควรซ้ำกันได้นะครับ
อย่าง orderID หนึ่งมี orderdetail ได้หลายๆตัวไรแบบนี้อ่าครับ
|
ประวัติการแก้ไข 2015-11-28 22:27:13
 |
 |
 |
 |
Date :
2015-11-28 20:42:47 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
-orderID คือ รหัสการสั่งซื้อ และใช้เป็น pk ใช่หรือป่าวครับ
- ดูเหมือน ในการวน loop แต่ล่ะรอบ จะใช้ ORID ค่าเดิมหรือป่าวครับ ถึง insert ได้แค่รอบแรก รอบเดียว
-เพราะฉะนั้น รอบ 2 error pk ซ้ำ แน่นอน ถ้าใช้ ORID ตัวเดิม
- ลอง debug ดูค่า ORID ครับ
---ไม่รู้ผมเข้าใจ ถูกหรือป่าว ผิดพลาดขออภัยครับ-----
|
ประวัติการแก้ไข 2015-11-29 04:49:38
 |
 |
 |
 |
Date :
2015-11-28 22:43:12 |
By :
weerachai.va |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
OrderDetails.orderID ควรกำหนดไม่ให้เป๊น PK ครับ
ส่วนจะเพิ่ม OrderDetailsID เพื่อมาเป็น PK ของ OrderDetails หรือไม่ขึ้นยุกับความเหมาะสมครับ
ถ้าเพิ่ม OrderDetailsID จะออกมาเป็นแบบนี้ ครับ
Code (VB.NET)
Dim dt As DataTable = DirectCast(ViewState("Order"), DataTable)
Dim SqlCon As New SqlConnection(con)
Dim da As New SqlDataAdapter
Dim AuOrderID As String
cmd = New SqlCommand(strSQL, SqlCon)
For i As Integer = 0 To dt.Rows.Count - 1
With dt.Rows(i)
With cmd
AuOrderID = ORID()
strSQL = "INSERT INTO Orders(orderID,empID,orderDate,createdDate,createdBy)"
strSQL &= "VALUES('" & AuOrderID & "'," ' ต้องใช้ AuOrderID ไม่งั้นจะส่งค่าให้ OrderDetails ไม่ถูกต้องครับ
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & lblempid.Text & "')"
SqlCon.Open() 'ย้ายลงมาเพื่อป้องกัน Error เพราะ มันจะปิดไปจาก SqlCon.Close() ใน ORID()
.CommandType = CommandType.Text
.CommandText = strSQL
.Connection = SqlCon
.ExecuteNonQuery()
For j As Integer = 0 To dt.Rows.Count - 1
strSQL = "INSERT INTO OrderDetails(OrderDetailsID ,orderID,storefood_rkey,optionDetails,qty,unitPrice,amount)"
strSQL &= "VALUES('" & OrderDetailsID??? & "','" & AuOrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("foodID"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("option"), String) & "',"
strSQL &= "'" & CType(dt.Rows(i)("qty"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("price"), Decimal) & "',"
strSQL &= "'" & CType(dt.Rows(i)("amount"), Decimal) & "')"
.CommandText = strSQL
.ExecuteNonQuery()
Next
End With
End With
Next
SqlCon.Close()
dt.Clear()
GridView1.DataSource = dt
GridView1.DataSource = Nothing
OrderDetailsID??? เป็นตัวที่เราจะเพิ่มมาเพื่อเป็น PK ใน OrderDetails ครับ
|
 |
 |
 |
 |
Date :
2015-11-29 09:47:49 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

นี่คือตารางตัวอย่างที่จะ insert เข้า Orders และ OrderDetails โดยที่ OrderDetails จะเป็นรายละเอียดในการสั่งอาหารของแต่ละ row อ่ะค่ะ
Orders (orderID,empID,orderDate,total,paymentStatus,paymentDate,createdDate,createdBy) => empID เป็นของใคร , createdBy เป็นคนสั่ง
OrderDetails (orderID,storefood_rkey,optionDetails,qty,unitPrice,amount)
|
ประวัติการแก้ไข 2015-11-29 18:26:08 2015-11-29 18:28:49 2015-11-29 18:32:10
 |
 |
 |
 |
Date :
2015-11-29 18:24:22 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าเราแก้ปัญหาแบบสร้าง autonumber อีกอันให้มีรูปแบบเหมือน PK ของตาราง orders แบบนี้มันจะมีปัญหาอะไรไหมคะเช่นเวลาที่มีคนใช้งานเยอะๆ PK ของ orderDetails มันจะไม่ตรงกับ PK ของ Orders ใน row นั้นๆ ไหมคะ?
หรือใช้วิธีสร้าง PK ให้ orderDetails แล้วเอา orderID เป็น FK จะง่ายกว่า 
|
 |
 |
 |
 |
Date :
2015-11-30 08:15:47 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 12 เขียนโดย : heaunn เมื่อวันที่ 2015-11-30 08:15:47
รายละเอียดของการตอบ ::
ตอนนี้ปัญหาอยู่ที่ว่า
Orders.orderID เป็น PK แล้ว INSERT INTO เข้าไปค่าเดียว
orderDetails.orderID เป็น PK แล้ว INSERT INTO เข้าไปค่าเดียว *** ซึ่งแบบนี้ PK มันจะไม่ได้ครับ
[font=Verdana]"ถ้าเราแก้ปัญหาแบบสร้าง autonumber อีกอันให้มีรูปแบบเหมือน PK ของตาราง orders แบบนี้มันจะมีปัญหาอะไรไหมคะเช่นเวลาที่มีคนใช้งานเยอะๆ PK ของ orderDetails มันจะไม่ตรงกับ PK ของ Orders ใน row นั้นๆ ไหมคะ?
หรือใช้วิธีสร้าง PK ให้ orderDetails แล้วเอา orderID เป็น FK จะง่ายกว่า"[/font]
ใช้วิธีสร้าง PK ให้ orderDetails แล้วเอา orderID เป็น FK จะถูกต้องกว่าครับ
Orders (orderID,empID,orderDate,total,paymentStatus,paymentDate,createdDate,createdBy)
OrderDetails (OrderDetailsID,orderID,storefood_rkey,optionDetails,qty,unitPrice,amount)
OrderDetails.OrderDetailsID เป็น PK
แบบนี้ Orders.orderID หนึ่ง ก็สามารถมี OrderDetails.OrderDetailsID มากกว่า 1 ได้ครับ
หรือไม่ก็ลองนึกภาพ บิล 7-11 makro lotus หรือ อื่นๆ ที่จะมี
orderID แล้วมี รายการเยอะๆๆ ตามลงมา แบบนั้นครับ OrderDetailsID มีไว้ให้จัดการ ค้นหา ลบ แก้ไข ข้อมูลได้ง่ายขึ้น
|
 |
 |
 |
 |
Date :
2015-11-30 08:32:15 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้า orderID OR1500001 มี orderDetails สั่งจำนวนอาหาร 2 อย่าง เช่น ข้าวมันไก่กับก๋วยเตี๋ยว สามารถเอามา Sum หา Total ได้ไหมคะ? เพราะตอนนี้ยังคิดวิธีเอาค่า total (ใน Orders) ออกมาไม่ได้เลยค่ะ ขอบคุณสำหรับคำแนะนำค่ะ TOR_CHEMISTRY 
|
ประวัติการแก้ไข 2015-11-30 08:45:58
 |
 |
 |
 |
Date :
2015-11-30 08:45:30 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (SQL)
Public Function Sumamount(orderID As Integer ) As Double
Dim _Sum As Double = 0
Dim cmd As New SqlCommand
With cmd
.Connection = SqlCon
.CommandText = "SELECT Sum(amount) As _Sumamount FROM orderDetails Where orderID = " & orderID
SqlCon.ConnectionString = con
SqlCon.Open()
If IsDBNull(.ExecuteScalar()) <> True Then
_Sum= .ExecuteScalar() ' อาจจะต้อง convert เป็น Double นะครับ
End If
End With
SqlCon.Close()
Return _Sum
End Function
โค้ดบ้าน ๆ น่าจะประมาณนี้ครับ
|
 |
 |
 |
 |
Date :
2015-11-30 09:23:02 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ช่วยตอบแทบทุกกระทู้เลยขอบคุณมากๆ ค่ะ 
|
ประวัติการแก้ไข 2015-11-30 09:37:19
 |
 |
 |
 |
Date :
2015-11-30 09:37:01 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ฟังก์ชั่นSUM ใน NO.15 เรียกใช้ยังไงอ่ะคะ
|
 |
 |
 |
 |
Date :
2015-11-30 14:03:57 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Dim _sum As Double
_sum= Sumamount(1) 'หาผลรวม amount ใน orderDetails โดย orderDetails.orderID = 1
label1.Text = "ยอดรวม " & _sum.ToString() & " Bth."
ประมาณนั้นครับ(ถ้าผมเขียนโค้ดถูกนะครับ 5555)
|
 |
 |
 |
 |
Date :
2015-11-30 14:45:27 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ข้อมูลลงตาราง OrderDetails ซ้ำกันอ่ะค่ะ
Orders

OrderDetails

ตอนนี้เขียนโค้ดแบบนี้ค่ะ
Code (VB.NET)
Protected Sub btnsubmit_Click(ByVal sender As Object, e As ImageClickEventArgs) Handles btnsubmit.Click
Dim dt As DataTable = DirectCast(ViewState("Order"), DataTable)
Dim SqlCon As New SqlConnection(con)
Dim da As New SqlDataAdapter
Dim AuOrderID As String
cmd = New SqlCommand(strSQL, SqlCon)
For i As Integer = 0 To dt.Rows.Count - 1
With dt.Rows(i)
With cmd
AuOrderID = ORID()
strSQL = "INSERT INTO Orders(orderID,empID,orderDate,createdDate,createdBy)"
strSQL &= "VALUES('" & ORID() & "',"
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & lblempid.Text & "')"
SqlCon.Open()
.CommandType = CommandType.Text
.CommandText = strSQL
.Connection = SqlCon
.ExecuteNonQuery()
For j As Integer = 0 To dt.Rows.Count - 1
strSQL = "INSERT INTO OrderDetails(orderID,storefood_rkey,optionDetails,qty,unitPrice,amount,empID)"
strSQL &= "VALUES('" & AuOrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("foodID"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("option"), String) & "',"
strSQL &= "'" & CType(dt.Rows(i)("qty"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("price"), Decimal) & "',"
strSQL &= "'" & CType(dt.Rows(i)("amount"), Decimal) & "',"
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "')"
.CommandText = strSQL
.ExecuteNonQuery()
Next
End With
End With
SqlCon.Close()
Next
dt.Clear()
GridView1.DataSource = dt
GridView1.DataSource = Nothing
End Sub
|
 |
 |
 |
 |
Date :
2015-11-30 14:54:09 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Protected Sub btnsubmit_Click(ByVal sender As Object, e As ImageClickEventArgs) Handles btnsubmit.Click
Dim dt As DataTable = DirectCast(ViewState("Order"), DataTable)
Dim SqlCon As New SqlConnection(con)
Dim da As New SqlDataAdapter
Dim AuOrderID As String
cmd = New SqlCommand(strSQL, SqlCon)
For i As Integer = 0 To dt.Rows.Count - 1
With dt.Rows(i)
With cmd
AuOrderID = ORID()
strSQL = "INSERT INTO Orders(orderID,empID,orderDate,createdDate,createdBy)"
strSQL &= "VALUES('" & AuOrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & lblempid.Text & "')"
SqlCon.Open()
.CommandType = CommandType.Text
.CommandText = strSQL
.Connection = SqlCon
.ExecuteNonQuery()
For j As Integer = 0 To dt.Rows.Count - 1
strSQL = "INSERT INTO OrderDetails(orderID,storefood_rkey,optionDetails,qty,unitPrice,amount,empID)"
strSQL &= "VALUES('" & AuOrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("foodID"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("option"), String) & "',"
strSQL &= "'" & CType(dt.Rows(i)("qty"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("price"), Decimal) & "',"
strSQL &= "'" & CType(dt.Rows(i)("amount"), Decimal) & "',"
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "')"
.CommandText = strSQL
.ExecuteNonQuery()
Next
End With
End With
SqlCon.Close()
Next
dt.Clear()
GridView1.DataSource = dt
GridView1.DataSource = Nothing
End Sub
strSQL &= "VALUES('" & ORID() & "'," ต้องเปลี่ยนเป็น strSQL &= "VALUES('" & AuOrderID & "',"
ไม่งั้นค่าจะไม่ตรงกันนะครับ
ส่วน orKey นี่คือ Autonumber ใช่ไม๊ครับ(ถามเพื่อความชัว) ลองดูอีกซักรอบครับ สู้ ๆๆๆ
|
 |
 |
 |
 |
Date :
2015-11-30 15:09:44 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองลบ For j As Integer = 0 To dt.Rows.Count - 1 แล้วข้อมูลลง OrderDetails ไม่ซ้ำแล้วค่ะ
orKey เป็น Autonumber ค่ะแต่เพราะลบข้อมูล 11 แถวก่อนหน้านี้ทิ้งค่ะในรูปเลยเริ่มต้นที่ 12
Code (VB.NET)
Protected Sub btnsubmit_Click(ByVal sender As Object, e As ImageClickEventArgs) Handles btnsubmit.Click
Dim dt As DataTable = DirectCast(ViewState("Order"), DataTable)
Dim SqlCon As New SqlConnection(con)
Dim da As New SqlDataAdapter
Dim AuOrderID As String
cmd = New SqlCommand(strSQL, SqlCon)
For i As Integer = 0 To dt.Rows.Count - 1
With dt.Rows(i)
With cmd
AuOrderID = ORID()
strSQL = "INSERT INTO Orders(orderID,empID,orderDate,createdDate,createdBy)"
strSQL &= "VALUES('" & AuOrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & lblempid.Text & "')"
SqlCon.Open()
.CommandType = CommandType.Text
.CommandText = strSQL
.Connection = SqlCon
.ExecuteNonQuery()
strSQL = "INSERT INTO OrderDetails(orderID,storefood_rkey,optionDetails,qty,unitPrice,amount,empID)"
strSQL &= "VALUES('" & AuOrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("foodID"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("option"), String) & "',"
strSQL &= "'" & CType(dt.Rows(i)("qty"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("price"), Decimal) & "',"
strSQL &= "'" & CType(dt.Rows(i)("amount"), Decimal) & "',"
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "')"
.CommandText = strSQL
.ExecuteNonQuery()
End With
End With
SqlCon.Close()
Next
dt.Clear()
GridView1.DataSource = dt
GridView1.DataSource = Nothing
End Sub
|
ประวัติการแก้ไข 2015-11-30 15:31:12 2015-11-30 15:32:02 2015-11-30 15:37:27 2015-11-30 15:40:18
 |
 |
 |
 |
Date :
2015-11-30 15:22:29 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

ตอนนี้ insert ลงDB จะได้ orderID (OR1500001,OR1500002,OR1500003) แต่ความต้องการคือ ถ้าเป็น ID เดียวกันให้ลงเป็น 1 ออเดอร์ เช่น 12345 สั่งอาหารสองอย่างรวมเป็น OR1500002 แล้วค่อยไปลงรายละเอียดการสั่งอาหารแต่ละรายการที่ตาราง orderDetails ต้องแก้โค้ดยังไงดีคะ?
|
 |
 |
 |
 |
Date :
2015-12-01 08:34:23 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 22 เขียนโดย : heaunn เมื่อวันที่ 2015-12-01 08:34:23
รายละเอียดของการตอบ ::
ไม่แนะนำให้นำเอา Sum ลงใน database ไม่สะดวกในการแก้ไข
ให้โชว์ตอนดึงออกมาใช้งานก็พอ
แต่หากยังต้องการแบบนั้น
ลองใช้ for loop และ if เก็บค่า amount ที่มี orderID ตามต้องการได้ครับ
Dim _amount As Double = 0
for
if orderID = OR1500001 ' บวกเพิ่ม
next
Orders(orderID,empID,orderDate,createdDate,createdBy)
OrderDetails(orderID,storefood_rkey,optionDetails,qty,unitPrice,amount,empID)
OrderDetails.empID สามารถตัดออกได้ครับเพราะมันมีใน Orders.empID อยู่แล้วครับ
ความจริงหากมี ตาราง สินค้า สามารถเอามา join กัน จะลดรูปแบบ database และเป็นสัดส่วนเยอะขึ้นครับ
|
 |
 |
 |
 |
Date :
2015-12-01 08:51:31 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 26 เขียนโดย : lamaka.tor เมื่อวันที่ 2015-12-01 14:22:57
รายละเอียดของการตอบ ::
datatable ยังเหมือนเดิมค่ะ นี่คือโค้ดตอนกดปุ่ม Submit ให้ลง DB ค่ะ
Code (VB.NET)
Protected Sub btnsubmit_Click(ByVal sender As Object, e As ImageClickEventArgs) Handles btnsubmit.Click
Dim dt As DataTable = DirectCast(ViewState("Order"), DataTable)
Dim SqlCon As New SqlConnection(con)
Dim da As New SqlDataAdapter
Dim AuOrderID As String
Dim orKeyID As String
cmd = New SqlCommand(strSQL, SqlCon)
For i As Integer = 0 To dt.Rows.Count - 1
With dt.Rows(i)
With cmd
AuOrderID = ORID()
strSQL = "INSERT INTO Orders(orderID,empID,orderDate,createdDate,createdBy)"
strSQL &= "VALUES('" & AuOrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & lblempid.Text & "')"
SqlCon.Open()
.CommandType = CommandType.Text
.CommandText = strSQL
.Connection = SqlCon
.ExecuteNonQuery()
orKeyID = Orkey()
strSQL = "INSERT INTO OrderDetails(orKey,orderID,storefood_rkey,optionDetails,qty,unitPrice,amount,empID)"
strSQL &= "VALUES('" & orKeyID & "','" & AuOrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("foodID"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("option"), String) & "',"
strSQL &= "'" & CType(dt.Rows(i)("qty"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("price"), Decimal) & "',"
strSQL &= "'" & CType(dt.Rows(i)("amount"), Decimal) & "',"
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "')"
.CommandText = strSQL
.ExecuteNonQuery()
End With
End With
SqlCon.Close()
Next
dt.Clear()
GridView1.DataSource = dt
GridView1.DataSource = Nothing
End Sub
โทษทีค่ะที่พึ่งมาตอบเมื่อวานโดนคอมเม้นเรื่อง userinterface เลยนั่งแก้อยู่ไม่มีเวลาเข้ามาดูกระทู้เลย
|
ประวัติการแก้ไข 2015-12-02 08:20:59
 |
 |
 |
 |
Date :
2015-12-02 08:19:52 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่รู้ผมเข้าใจถูกไม๊
Dim dt As DataTable = DirectCast(ViewState("Order"), DataTable)
dt ที่ว่าคือดึงมาจากตารางด้านบนใช่ไม๊ครับ
หากใช่ เรื่องมันจะเป็นโอละพ่อจากที่ว่ามาตั้งแต่ต้น
หากไม่ใช่ dt มาจากส่วนไหนเรอะครับ
|
 |
 |
 |
 |
Date :
2015-12-02 09:36:13 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ค่ะ ดึงมาจากตารางข้างบน
|
 |
 |
 |
 |
Date :
2015-12-02 12:57:53 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เหมือนมันจะขัดแย้งกับความเป็นจริงยุนะครับ
ความจริง ชื่อ-นามสกุลผู้ซื้อ ควรแยกออกไปต่างหากจาก GridView ด้านบนนะครับ
โดยทั่วไปในการทำรายการ 1 บิล ควรจะมีผู้ซื้อเพียงคนเดียว
ส่วน GridView ด้านบนก็จะมีแค่รายละเอียดสินค้า และ ราคา ครับ(ยกตัวอย่าง บิล 7-11 lotus หรือ ที่อื่นๆดูครับ)
จากนั้นการ Insert ก็จะง่ายขึ้น
Code (VB.NET)
Protected Sub btnsubmit_Click(ByVal sender As Object, e As ImageClickEventArgs) Handles btnsubmit.Click
Dim dt As DataTable = DirectCast(ViewState("Order"), DataTable)
Dim SqlCon As New SqlConnection(con)
Dim da As New SqlDataAdapter
Dim AuOrderID As String
Dim orKeyID As String
cmd = New SqlCommand(strSQL, SqlCon)
AuOrderID = ORID()
strSQL = "INSERT INTO Orders(orderID,empID,orderDate,createdDate,createdBy)"
strSQL &= "VALUES('" & AuOrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & lblempid.Text & "')"
SqlCon.Open()
With cmd
.CommandType = CommandType.Text
.CommandText = strSQL
.Connection = SqlCon
.ExecuteNonQuery()
End With
'บันทึกข้อมูลลงใน Orders ก่อนจากนั้นค่อย loop เพื่อบันทึก OrderDetails
For i As Integer = 0 To dt.Rows.Count - 1
With cmd
orKeyID = Orkey()
strSQL = "INSERT INTO OrderDetails(orKey,orderID,storefood_rkey,optionDetails,qty,unitPrice,amount,empID)"
strSQL &= "VALUES('" & orKeyID & "','" & AuOrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("foodID"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("option"), String) & "',"
strSQL &= "'" & CType(dt.Rows(i)("qty"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("price"), Decimal) & "',"
strSQL &= "'" & CType(dt.Rows(i)("amount"), Decimal) & "',"
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "')"
.CommandType = CommandType.Text
.CommandText = strSQL
.Connection = SqlCon
.ExecuteNonQuery()
End With
Next
dt.Clear()
GridView1.DataSource = dt
GridView1.DataSource = Nothing
SqlCon.Close()
End Sub
รูปแบบบ้านๆก็น่าจะเป็นแบบนี้ครับสามารถใช้ได้กับหลายๆงานได้เลย
|
 |
 |
 |
 |
Date :
2015-12-02 13:48:32 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อันนี้ทำความต้องการของที่ฝึกงานอ่ะค่ะเป็นโปรเจคจบ เขาต้องการให้พนักงานสามารถสั่งอาหารแทนกันได้ด้วยอ่ะค่ะ 
|
ประวัติการแก้ไข 2015-12-02 16:11:23
 |
 |
 |
 |
Date :
2015-12-02 16:10:11 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
งั้นรูปแบบตามที่เสนอล่าสุดเลยครับ
|
 |
 |
 |
 |
Date :
2015-12-02 18:44:01 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ประมาณว่าใน บิลเดียว ให้จัด ออเดอร์ ตามพนักงาน
เหมือนอยากรู้ว่าคนไหนสั่งไรบ้างจะได้เก็บตังถูกไประมาณนั้น 555
ความคิดบ้านผมมี 2 ทางเลือกครับ
1.
แก้ตารางควรเป็น
Orders(orderID,orderDate,createdDate,createdBy)" ตัด empID ออกครับ
OrderDetails(orKey,orderID,storefood_rkey,optionDetails,qty,unitPrice,amount,empID)"
OrderID น่าจะใช้ร่วมกันได้เพราะ OrderDetails จะบอกอยู่แล้วครับว่าใครสั่งไร
2.
แต่ถ้ายังบอกว่าไม่ได้อีก
อาจจะต้องเปลี่ยน Orders.orderID เป็นอย่างอื่น เช่น Orders.IntvoiceID
ส่วน OrderDetails.orderID ใช้เหมือนเดิม แต่ต้องแยกตาม empID
ประกาศตัวแปร List<string> เพื่อเอามาเก็บศพ empID แล้วเอาไป for loop ดูว่ามีไม๊ ถ้าไม่มี ออก orderID ใหม่
ถ้ามี ก็ดึง orderID ใน List<string> มาใช้เพิ่มต่อได้แบบเห็น
ทั้ง 2 ได้มุมมองไม่ต่างกันมากครับ
จัดรูปแบบ database ดูก่อนที่เหลือค่อยว่ากัน    
|
 |
 |
 |
 |
Date :
2015-12-03 15:56:37 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้ครับ เพราะมันมีorderID อยู่แล้วครับแถมง่ายกว่าด้วย แค่select. ตาม orderID. ก้จะแยกออกมาแต่ละคนเลย
|
 |
 |
 |
 |
Date :
2015-12-03 19:56:54 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Orders(orderID,empID,orderDate,createdDate,createdBy)
OrderDetails(orKey,orderID,storefood_rkey,optionDetails,qty,unitPrice,amount) " ตัด empID ออกครับ
ระกาศตัวแปร List<string> เพื่อเอามาเก็บศพ empID แล้วเอาไป for loop ดูว่ามีไม๊ ถ้าไม่มี ออก orderID ใหม่
ถ้ามี ก็ดึง orderID ใน List<string> มาใช้เพิ่มต่อได้แบบเห็น
ถ้าแบบนี้เราต้องเพิ่มมาอีก 1 ตาราง เพื่อจะเอามาเก็บ Orders.orderID
ให้อยู่ในกลุ่มเดียวกัน
ไม่งั้นครั้งต่อไปที่เราเรียกดูเราจะไม่รู้ว่าจะเอา Orders.orderID ไหนมาบ้างนะครับ
ตัวอย่าง
OR1500001 และ OR1500002 อยู่ใน บิลเดียวกัน
OR1500003 แยกออกมา.
ครั้งต่อไปถ้าเราจะเรียกมาแก้ไขเราจะ Select ยังไงรึครับ
นี่แค่ปัญหาเล็กน้อยๆ อาจจะมีตามมาอีกเรื่อยๆครับ
|
 |
 |
 |
 |
Date :
2015-12-04 08:25:41 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
1. เพิ่ม Class OrderEmp
Code (VB.NET)
Public Class OrderEmp
Public Emp As String
Public OrderID As String
End Class
2. เพิ่ม Function checkOrderEmp เพื่มเชค emp ว่ามีรึยังไม่มีให้สร้าง OrderID ใหม่
Code (VB.NET)
Dim lst As List(Of OrderEmp)
Function checkOrderEmp(_emp As String) As OrderEmp
Dim _OrderEmp As OrderEmp
Dim str As String = ""
For Each oe As OrderEmp In lst
If oe.Emp = _emp Then
_OrderEmp = oe
str = _emp
Exit For
End If
Next
If lst.Count < 0 Or str = "" Then
lst.Add(New OrderEmp())
lst(lst.Count - 1).OrderID = ORID()
lst(lst.Count - 1).Emp = _emp
_OrderEmp = lst(lst.Count - 1)
End If
Return _OrderEmp
End Function
'ปรับใหม่แบบนี้
Private Sub btnsubmit_Click(sender As Object, e As EventArgs) Handles btnsubmit.Click
Dim dt As DataTable = DirectCast(ViewState("Order"), DataTable)
Dim SqlCon As New SqlConnection(con)
Dim da As New SqlDataAdapter
Dim orKeyID As String
cmd = New SqlCommand()
Dim strSQL As String
Dim _OrderEmp As OrderEmp
For i As Integer = 0 To dt.Rows.Count - 1
With dt.Rows(i)
With cmd
_OrderEmp = checkOrderEmp(CType(dt.Rows(i)("empID"), String))
strSQL = "INSERT INTO Orders(orderID,empID,orderDate,createdDate,createdBy)"
strSQL &= "VALUES('" & _OrderEmp.OrderID & "',"
strSQL &= "'" & _OrderEmp.OrderID & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & lblempid.Text & "')"
SqlCon.Open()
.CommandType = CommandType.Text
.CommandText = strSQL
.Connection = SqlCon
.ExecuteNonQuery()
orKeyID = Orkey()
strSQL = "INSERT INTO OrderDetails(orKey,orderID,storefood_rkey,optionDetails,qty,unitPrice,amount,empID)"
strSQL &= "VALUES('" & orKeyID & "','" & _OrderEmp.OrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("foodID"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("option"), String) & "',"
strSQL &= "'" & CType(dt.Rows(i)("qty"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("price"), Decimal) & "',"
strSQL &= "'" & CType(dt.Rows(i)("amount"), Decimal) & "',"
strSQL &= "'" & _OrderEmp.Emp & "')"
.CommandText = strSQL
.ExecuteNonQuery()
End With
End With
SqlCon.Close()
Next
dt.Clear()
GridView1.DataSource = dt
GridView1.DataSource = Nothing
End Sub
อาจจะไม่เปะๆนะครับ
ผมไม่ถนัด vb
***** แต่อย่างที่บอก
จะไม่สามารถ select ออกมาแก้ข้อมูลได้ ยกเว้น select ตาม orderID ซึ่งมันผิดเพราะตอนคิดรวมก็ต้องเอา orderID มารวมกันยุแล้วครับ
นอกจากจะมีตารางมาเก็บออกรอบ
ขอให้ได้ขอให้โดนครับ
   
|
 |
 |
 |
 |
Date :
2015-12-04 14:38:29 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันติด Error อ่ะค่ะ Object reference not set to an instance of an object.
Line 151: Dim _OrderEmp As OrderEmp
Line 152: Dim str As String = ""
Line 153: For Each oe As OrderEmp In lst
Line 154: If oe.Emp = _emp Then
Line 155: _OrderEmp = oe
|
ประวัติการแก้ไข 2015-12-05 07:41:14 2015-12-05 07:42:33 2015-12-05 07:43:10 2015-12-05 08:21:52 2015-12-05 08:29:41
 |
 |
 |
 |
Date :
2015-12-05 07:25:24 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เพิ่ม class. ยัง
|
 |
 |
 |
 |
Date :
2015-12-05 09:54:51 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เพิ่มไว้ในหน้า VB แล้วค่ะ
Code (VB.NET)
Public Class OrderEmp
Public Emp As String
Public OrderID As String
End Class
Dim lst As List(Of OrderEmp)
Function checkOrderEmp(_emp As String) As OrderEmp
Dim _OrderEmp As OrderEmp = Nothing
Dim str As String = ""
For Each oe As OrderEmp In lst
If oe.Emp = _emp Then
_OrderEmp = oe
str = _emp
Exit For
End If
Next
If lst.Count < 0 Or str = "" Then
lst.Add(New OrderEmp())
lst(lst.Count - 1).OrderID = ORID()
lst(lst.Count - 1).Emp = _emp
_OrderEmp = lst(lst.Count - 1)
End If
Return _OrderEmp
End Function
Protected Sub btnsubmit_Click(ByVal sender As Object, e As ImageClickEventArgs) Handles btnsubmit.Click
Dim dt As DataTable = DirectCast(ViewState("Order"), DataTable)
Dim SqlCon As New SqlConnection(con)
Dim da As New SqlDataAdapter
Dim orKeyID As String
Dim i As Integer
cmd = New SqlCommand()
Dim strSQL As String
Dim _OrderEmp As OrderEmp
For i = 0 To dt.Rows.Count - 1
With dt.Rows(i)
With cmd
_OrderEmp = checkOrderEmp(CType(dt.Rows(i)("empID"), String))
strSQL = "INSERT INTO Orders(orderID,empID,orderDate,createdDate,createdBy)"
strSQL &= "VALUES('" & _OrderEmp.OrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & lblempid.Text & "')"
SqlCon.Open()
.CommandType = CommandType.Text
.CommandText = strSQL
.Connection = SqlCon
.ExecuteNonQuery()
orKeyID = orKey()
strSQL = "INSERT INTO OrderDetails(orKey,orderID,storefood_rkey,optionDetails,qty,unitPrice,amount)"
strSQL &= "VALUES('" & orKeyID & "','" & _OrderEmp.OrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("foodID"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("option"), String) & "',"
strSQL &= "'" & CType(dt.Rows(i)("qty"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("price"), Decimal) & "',"
strSQL &= "'" & CType(dt.Rows(i)("amount"), Decimal) & "')"
.CommandText = strSQL
.ExecuteNonQuery()
End With
End With
SqlCon.Close()
Next
dt.Clear()
GridView1.DataSource = dt
GridView1.DataSource = Nothing
End Sub
|
 |
 |
 |
 |
Date :
2015-12-05 12:16:38 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนนี้ติดปัญหาคือถ้ามีข้อมูลที่เป็น empID เดียวกันสั่งอาหาร มากกว่า 1 รายการมันจะขึ้นเออเร่อ Violation of PRIMARY KEY constraint 'PK_Orders'. Cannot insert duplicate key in object 'dbo.Orders'. The duplicate key value is (OR1500002).
The statement has been terminated. ค่ะ
Code (VB.NET)
Public Class OrderEmp
Public Emp As String
Public OrderID As String
End Class
Dim lst As New List(Of OrderEmp)
Function checkOrderEmp(_emp As String) As OrderEmp
Dim _OrderEmp As OrderEmp = Nothing
Dim str As String = ""
For Each oe As OrderEmp In lst
If oe.Emp = _emp Then
_OrderEmp = oe
str = _emp
Exit For
End If
Next
If lst.Count < 0 Or str = "" Then
lst.Add(New OrderEmp())
lst(lst.Count - 1).OrderID = ORID()
lst(lst.Count - 1).Emp = _emp
_OrderEmp = lst(lst.Count - 1)
End If
Return _OrderEmp
End Function
Protected Sub btnsubmit_Click(ByVal sender As Object, e As ImageClickEventArgs) Handles btnsubmit.Click
Dim dt As DataTable = DirectCast(ViewState("Order"), DataTable)
Dim SqlCon As New SqlConnection(con)
Dim da As New SqlDataAdapter
Dim orKeyID As String
Dim i As Integer
cmd = New SqlCommand()
Dim strSQL As String
Dim _OrderEmp As OrderEmp
For i = 0 To dt.Rows.Count - 1
With dt.Rows(i)
With cmd
_OrderEmp = checkOrderEmp(CType(dt.Rows(i)("empID"), String))
strSQL = "INSERT INTO Orders(orderID,empID,orderDate,createdDate,createdBy)"
strSQL &= "VALUES('" & _OrderEmp.OrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & lblempid.Text & "')"
SqlCon.Open()
.CommandType = CommandType.Text
.CommandText = strSQL
.Connection = SqlCon
.ExecuteNonQuery()
orKeyID = orKey()
strSQL = "INSERT INTO OrderDetails(orKey,orderID,storefood_rkey,optionDetails,qty,unitPrice,amount)"
strSQL &= "VALUES('" & orKeyID & "','" & _OrderEmp.OrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("foodID"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("option"), String) & "',"
strSQL &= "'" & CType(dt.Rows(i)("qty"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("price"), Decimal) & "',"
strSQL &= "'" & CType(dt.Rows(i)("amount"), Decimal) & "')"
.CommandText = strSQL
.ExecuteNonQuery()
End With
End With
SqlCon.Close()
Next
dt.Clear()
GridView1.DataSource = dt
GridView1.DataSource = Nothing
End Sub
|
ประวัติการแก้ไข 2015-12-10 08:11:59 2015-12-10 08:12:23
 |
 |
 |
 |
Date :
2015-12-10 08:08:04 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Public Class OrderEmp
Public Emp As String
Public OrderID As String
End Class
Dim lst As New List(Of OrderEmp)
Protected Sub btnsubmit_Click(ByVal sender As Object, e As ImageClickEventArgs) Handles btnsubmit.Click
Dim dt As DataTable = DirectCast(ViewState("Order"), DataTable)
Dim SqlCon As New SqlConnection(con)
Dim da As New SqlDataAdapter
Dim orKeyID As String
Dim i As Integer
cmd = New SqlCommand()
Dim strSQL As String
Dim _OrderEmp As OrderEmp
For i = 0 To dt.Rows.Count - 1
With dt.Rows(i)
With cmd
For Each oe As OrderEmp In lst
If oe.Emp = _emp Then
_OrderEmp = oe
str = _emp
Exit For
End If
Next
If lst.Count < 0 Or str = "" Then
lst.Add(New OrderEmp())
lst(lst.Count - 1).OrderID = ORID()
lst(lst.Count - 1).Emp = _emp
_OrderEmp = lst(lst.Count - 1)
strSQL = "INSERT INTO Orders(orderID,empID,orderDate,createdDate,createdBy)"
strSQL &= "VALUES('" & _OrderEmp.OrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & lblempid.Text & "')"
SqlCon.Open()
.CommandType = CommandType.Text
.CommandText = strSQL
.Connection = SqlCon
.ExecuteNonQuery()
End If
orKeyID = orKey()
strSQL = "INSERT INTO OrderDetails(orKey,orderID,storefood_rkey,optionDetails,qty,unitPrice,amount)"
strSQL &= "VALUES('" & orKeyID & "','" & _OrderEmp.OrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("foodID"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("option"), String) & "',"
strSQL &= "'" & CType(dt.Rows(i)("qty"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("price"), Decimal) & "',"
strSQL &= "'" & CType(dt.Rows(i)("amount"), Decimal) & "')"
.CommandText = strSQL
.ExecuteNonQuery()
End With
End With
SqlCon.Close()
Next
dt.Clear()
GridView1.DataSource = dt
GridView1.DataSource = Nothing
End Sub
ลองแบบนี้ดูครับ
|
 |
 |
 |
 |
Date :
2015-12-10 09:09:30 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนนี้ insert ข้อมูลลง DB ได้แล้วค่ะ แต่ถ้า empID เดียวกันสั่ง 2 รายการตรง OrderID มันก็รันเป็นคนละออเดอร์อ่ะค่ะ
|
 |
 |
 |
 |
Date :
2015-12-10 11:37:02 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอแก้โค้ดใหม่นะครับ
ลืมๆๆๆๆ
Code (VB.NET)
Public Class OrderEmp
Public Emp As String
Public OrderID As String
End Class
Dim lst As New List(Of OrderEmp)
Protected Sub btnsubmit_Click(ByVal sender As Object, e As ImageClickEventArgs) Handles btnsubmit.Click
Dim dt As DataTable = DirectCast(ViewState("Order"), DataTable)
Dim SqlCon As New SqlConnection(con)
Dim da As New SqlDataAdapter
Dim orKeyID As String
Dim i As Integer
cmd = New SqlCommand()
Dim strSQL As String
Dim _OrderEmp As OrderEmp
For i = 0 To dt.Rows.Count - 1
With dt.Rows(i)
With cmd
For Each oe As OrderEmp In lst
If oe.Emp = CType(dt.Rows(i)("empID"), String) Then
_OrderEmp = oe
str = _emp
Exit For
End If
Next
If lst.Count < 0 Or str = "" Then
lst.Add(New OrderEmp())
lst(lst.Count - 1).OrderID = ORID()
lst(lst.Count - 1).Emp = _emp
_OrderEmp = lst(lst.Count - 1)
strSQL = "INSERT INTO Orders(orderID,empID,orderDate,createdDate,createdBy)"
strSQL &= "VALUES('" & _OrderEmp.OrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & lblempid.Text & "')"
SqlCon.Open()
.CommandType = CommandType.Text
.CommandText = strSQL
.Connection = SqlCon
.ExecuteNonQuery()
End If
orKeyID = orKey()
strSQL = "INSERT INTO OrderDetails(orKey,orderID,storefood_rkey,optionDetails,qty,unitPrice,amount)"
strSQL &= "VALUES('" & orKeyID & "','" & _OrderEmp.OrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("foodID"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("option"), String) & "',"
strSQL &= "'" & CType(dt.Rows(i)("qty"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("price"), Decimal) & "',"
strSQL &= "'" & CType(dt.Rows(i)("amount"), Decimal) & "')"
.CommandText = strSQL
.ExecuteNonQuery()
End With
End With
SqlCon.Close()
Next
dt.Clear()
GridView1.DataSource = dt
GridView1.DataSource = Nothing
End Sub
ประมาณนี้ครับ
มันจะเชคให้อยู่แล้ว
|
 |
 |
 |
 |
Date :
2015-12-10 15:49:29 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
empID เดียวกันสั่งสองรายการก็ยังลงเป็น 2 OrderID อ่ะค่ะ
Code (VB.NET)
Protected Sub btnsubmit_Click(ByVal sender As Object, e As ImageClickEventArgs) Handles btnsubmit.Click
Dim dt As DataTable = DirectCast(ViewState("Order"), DataTable)
Dim SqlCon As New SqlConnection(con)
Dim da As New SqlDataAdapter
Dim orKeyID As String
Dim i As Integer
cmd = New SqlCommand()
Dim strSQL As String
Dim str As String = ""
Dim _emp As String = ""
Dim _OrderEmp As OrderEmp = Nothing
For i = 0 To dt.Rows.Count - 1
With dt.Rows(i)
With cmd
For Each oe As OrderEmp In lst
If oe.Emp = CType(dt.Rows(i)("empID"), String) Then
_OrderEmp = oe
str = _emp
Exit For
End If
Next
If lst.Count < 0 Or str = "" Then
lst.Add(New OrderEmp())
lst(lst.Count - 1).OrderID = ORID()
lst(lst.Count - 1).Emp = _emp
_OrderEmp = lst(lst.Count - 1)
strSQL = "INSERT INTO Orders(orderID,empID,orderDate,createdDate,createdBy)"
strSQL &= "VALUES('" & _OrderEmp.OrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "',"
strSQL &= "'" & DateTime.Now.Year.ToString() & "-" & DateTime.Now.Month.ToString() & "-" & DateTime.Now.Day.ToString() & "',"
strSQL &= "'" & DateTime.Now.Year.ToString() & "-" & DateTime.Now.Month.ToString() & "-" & DateTime.Now.Day.ToString() & "',"
strSQL &= "'" & lblempid.Text & "')"
SqlCon.Open()
.CommandType = CommandType.Text
.CommandText = strSQL
.Connection = SqlCon
.ExecuteNonQuery()
End If
orKeyID = orKey()
strSQL = "INSERT INTO OrderDetails(orKey,orderID,storefood_rkey,optionDetails,qty,unitPrice,amount)"
strSQL &= "VALUES('" & orKeyID & "','" & _OrderEmp.OrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("foodID"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("option"), String) & "',"
strSQL &= "'" & CType(dt.Rows(i)("qty"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("price"), Decimal) & "',"
strSQL &= "'" & CType(dt.Rows(i)("amount"), Decimal) & "')"
.CommandText = strSQL
.ExecuteNonQuery()
End With
End With
strSQL = "UPDATE StoreFood SET QTY = QTY - '" & CType(dt.Rows(i)("qty"), Integer) & "' WHERE rkey ='" & CType(dt.Rows(i)("foodID"), Integer) & "'"
With cmd
.CommandText = strSQL
.ExecuteNonQuery()
End With
SqlCon.Close()
Next
dt.Clear()
GridView1.DataSource = dt
GridView1.DataSource = Nothing
End Sub
|
ประวัติการแก้ไข 2015-12-11 08:45:03
 |
 |
 |
 |
Date :
2015-12-11 08:41:51 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเชคเป็น step ๆๆๆ ครับว่าช่วงไหนให้ empID ยังไงก็จะรู้เองครับว่าติดตรงไหน
|
 |
 |
 |
 |
Date :
2015-12-11 09:07:58 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
For Each oe As OrderEmp In lst
If oe.Emp = CType(dt.Rows(i)("empID"), String) Then //ลองเช็ค CType(dt.Rows(i)("empID") แล้วมันให้ empID ตามรายการที่สั่งค่ะ ไม่เข้าใจว่าตรงนี้มันทำงานยังไงช่วยอธิบายหน่อยนะคะ
_OrderEmp = oe
str = _emp
Exit For
End If
Next
If lst.Count < 0 Or str = "" Then
lst.Add(New OrderEmp())
lst(lst.Count - 1).OrderID = ORID()
lst(lst.Count - 1).Emp = _emp
_OrderEmp = lst(lst.Count - 1)
|
 |
 |
 |
 |
Date :
2015-12-11 09:53:31 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
For Each oe As OrderEmp In lst
If oe.Emp = CType(dt.Rows(i)("empID"), String) Then ' เชคข้อมูลใน lst
_OrderEmp = oe
str = _emp
Exit For
'ถ้ามีข้อมูลใน lst แล้วให้เรียกใช้ _OrderEmp = oe แล้ว Exit For
End If
Next
'กรณี เชคด้านบนแล้วไม่เจอ *** ยังไม่มีรายชื่อ ใน lst ให้ Add(New OrderEmp())
If lst.Count < 0 Or str = "" Then
lst.Add(New OrderEmp())
lst(lst.Count - 1).OrderID = ORID()
lst(lst.Count - 1).Emp = CType(dt.Rows(i)("empID"), String) 'เจอตัวปัญหาแล้วครับตรงนี้ๆๆๆ str  
_OrderEmp = lst(lst.Count - 1)
strSQL = "INSERT INTO Orders(orderID,empID,orderDate,createdDate,createdBy)"
strSQL &= "VALUES('" & _OrderEmp.OrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "',"
strSQL &= "'" & DateTime.Now.Year.ToString() & "-" & DateTime.Now.Month.ToString() & "-" & DateTime.Now.Day.ToString() & "',"
strSQL &= "'" & DateTime.Now.Year.ToString() & "-" & DateTime.Now.Month.ToString() & "-" & DateTime.Now.Day.ToString() & "',"
strSQL &= "'" & lblempid.Text & "')"
SqlCon.Open()
.CommandType = CommandType.Text
.CommandText = strSQL
.Connection = SqlCon
.ExecuteNonQuery()
End If
|
 |
 |
 |
 |
Date :
2015-12-11 10:04:16 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
For i = 0 To dt.Rows.Count - 1
_OrderEmp = Nothing 'เพิ่มตัวนี้เข้าไปด้วย
With dt.Rows(i)
With cmd
For Each oe As OrderEmp In lst
If oe.Emp = CType(dt.Rows(i)("empID"), String) Then
_OrderEmp = oe
Exit For
End If
Next
If lst.Count < 0 Or IsNothing(_OrderEmp) Then
lst.Add(New OrderEmp())
lst(lst.Count - 1).OrderID = ORID()
lst(lst.Count - 1).Emp = CType(dt.Rows(i)("empID"), String)
_OrderEmp = lst(lst.Count - 1)
strSQL = "INSERT INTO Orders(orderID,empID,orderDate,createdDate,createdBy)"
strSQL &= "VALUES('" & _OrderEmp.OrderID & "',"
strSQL &= "'" & _OrderEmp.Emp & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & "',"
strSQL &= "'" & lblempid.Text & "')"
SqlCon.Open()
.CommandType = CommandType.Text
.CommandText = strSQL
.Connection = SqlCon
.ExecuteNonQuery()
End If
ประมาณนี้ครับ
***ลองเช็คดูอีกรอบ
หลักการคือ
วน loop เชื่อดูว่ามี empID รึไม่ ถ้ามีก็เอาค่ามาใช้งาน ถ้าไม่มีให้ Add เพิ่ม แค่นั้นเองครับ
|
 |
 |
 |
 |
Date :
2015-12-11 14:03:04 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 53 เขียนโดย : lamaka.tor เมื่อวันที่ 2015-12-11 14:03:04
รายละเอียดของการตอบ ::
ตอนนี้ empID เดียวกันลงเป็น OrderID เดียวกันแล้วค่ะ
ลองเทสแบบสั่ง empID เดียวกันสองรายการลง DB ปกติค่ะ แต่พอลองสั่ง 2 รายการโดยที่ empID ต่างกันขึ้นเออเร่อ
The connection was not closed. The connection's current state is open. ข้อมูลลง DB แค่ empID เดียว คือ empID ที่สั่งเป็นคนแรก
Code (VB.NET)
Protected Sub btnsubmit_Click(ByVal sender As Object, e As ImageClickEventArgs) Handles btnsubmit.Click
Dim dt As DataTable = DirectCast(ViewState("Order"), DataTable)
Dim SqlCon As New SqlConnection(con)
Dim da As New SqlDataAdapter
Dim orKeyID As String
Dim i As Integer
Dim _dt As New DataTable
cmd = New SqlCommand()
Dim strSQL As String
Dim _emp As String = ""
Dim _OrderEmp As OrderEmp
Try
For i = 0 To dt.Rows.Count - 1
_OrderEmp = Nothing
With dt.Rows(i)
With cmd
For Each oe As OrderEmp In lst
If oe.Emp = CType(dt.Rows(i)("empID"), String) Then ' เชคข้อมูลใน lst
_OrderEmp = oe
Exit For
'ถ้ามีข้อมูลใน lst แล้วให้เรียกใช้ _OrderEmp = oe แล้ว Exit For
End If
Next
'กรณี เชคด้านบนแล้วไม่เจอ *** ยังไม่มีรายชื่อ ใน lst ให้ Add(New OrderEmp())
If lst.Count < 0 Or IsNothing(_OrderEmp) Then
lst.Add(New OrderEmp())
lst(lst.Count - 1).OrderID = ORID()
lst(lst.Count - 1).Emp = CType(dt.Rows(i)("empID"), String)
_OrderEmp = lst(lst.Count - 1)
MsgBox(CType(dt.Rows(i)("empID"), String))
strSQL = "INSERT INTO Orders(orderID,empID,orderDate,createdDate,createdBy)"
strSQL &= "VALUES('" & _OrderEmp.OrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("empID"), String) & "',"
strSQL &= "'" & DateTime.Now.Year.ToString() & "-" & DateTime.Now.Month.ToString() & "-" & DateTime.Now.Day.ToString() & "',"
strSQL &= "'" & DateTime.Now.Year.ToString() & "-" & DateTime.Now.Month.ToString() & "-" & DateTime.Now.Day.ToString() & "',"
strSQL &= "'" & lblempid.Text & "')"
SqlCon.Open()
.CommandType = CommandType.Text
.CommandText = strSQL
.Connection = SqlCon
.ExecuteNonQuery()
End If
orKeyID = orKey()
strSQL = "INSERT INTO OrderDetails(orKey,orderID,storefood_rkey,optionDetails,qty,unitPrice,amount)"
strSQL &= "VALUES('" & orKeyID & "','" & _OrderEmp.OrderID & "',"
strSQL &= "'" & CType(dt.Rows(i)("foodID"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("option"), String) & "',"
strSQL &= "'" & CType(dt.Rows(i)("qty"), Integer) & "',"
strSQL &= "'" & CType(dt.Rows(i)("price"), Decimal) & "',"
strSQL &= "'" & CType(dt.Rows(i)("amount"), Decimal) & "')"
.CommandText = strSQL
.ExecuteNonQuery()
End With
End With
Next
Catch ex As Exception
MsgBox("เกิดข้อผิดพลาด" + ex.Message) 'แจ้ง Error
Finally
If SqlCon.State = ConnectionState.Open Then SqlCon.Close()
End Try
End Sub
|
ประวัติการแก้ไข 2015-12-12 11:52:58 2015-12-12 11:57:57 2015-12-12 19:37:34 2015-12-12 21:23:52
 |
 |
 |
 |
Date :
2015-12-12 11:28:37 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองดู
ว่า method ไหน ปิดมันป่าวครับ
|
 |
 |
 |
 |
Date :
2015-12-14 08:36:39 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ดูยังไงว่า method ไหนปิดมันอ่ะคะ
|
 |
 |
 |
 |
Date :
2015-12-14 10:20:50 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ทราบว่าผู้ถามเข้าใจระบบดีแล้วหรือยังว่าหลักการมันทำงานอย่างไง (หรือผมเองหว่า อ่านๆไปเริ่มงง)
ขอถามนะครับ
1.พนักงานสามารถ key แทนกันได้แต่ต้องระบุรหัสพนักงานก่อนใช่หรือไม่(แต่ละรายการอาหาร ไม่ใช่ Order หลัก)
-ถ้าใช้ให้แยกเก็บรหัสพนักงานมาที่ Detail ด้วย
2.หน้าจอเมื่อมีการบันทึกเรียบร้อยแล้ว ได้มีล้างค่าหน้าจอหรือยังเพื่อที่จะรับ Order ใหม่
-ถ้ายังไม่มีก็เขียนเพิ่มเข้าไป
ไม่งั้นก็จะวนอยู่อย่างนี้ตลอดไปครับ หาคำตอบไม่เจอ โปรแกรมเมอร์ไม่ใช้แค่เขียนเป็นต้องวิเคราะห์เป็นด้วย
ถ้าผมตอบแรงไปก็ขอโทษด้วยครับ
|
 |
 |
 |
 |
Date :
2015-12-14 10:37:25 |
By :
บัญดิษฐ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
งงอยู่ตั้งหลายวัน แค่ย้าย _OrderEmp = Nothing ลงมาใน command เองค่ะ
ขอบคุณสำหรับคำแนะนำนะคะ TOR_CHEMISTRY 
Code (VB.NET)
For i = 0 To dt.Rows.Count - 1
With dt.Rows(i)
With cmd
_OrderEmp = Nothing
For Each oe As OrderEmp In lst
If oe.Emp = CType(dt.Rows(i)("empID"), String) Then ' เชคข้อมูลใน lst
_OrderEmp = oe
Exit For
'ถ้ามีข้อมูลใน lst แล้วให้เรียกใช้ _OrderEmp = oe แล้ว Exit For
End If
Next
|
 |
 |
 |
 |
Date :
2015-12-14 12:47:08 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เชคเจอแล้วก็ลองแก้ดูครับ
|
 |
 |
 |
 |
Date :
2015-12-14 13:22:51 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
รบกวนอีกนิดนะค่ะ ตอนนี้ใช้ function นี้ในการ insert total ลงตาราง orders แต่มันลงเป็น 0.00 อ่ะค่ะ
Code (VB.NET)
Public Function Sumamount(orderID As Integer ) As Double
Dim _Sum As Double = 0
Dim cmd As New SqlCommand
With cmd
.Connection = SqlCon
.CommandText = "SELECT Sum(amount) As _Sumamount FROM orderDetails Where orderID = " & orderID
SqlCon.ConnectionString = con
SqlCon.Open()
If IsDBNull(.ExecuteScalar()) <> True Then
_Sum= .ExecuteScalar() ' อาจจะต้อง convert เป็น Double นะครับ
End If
End With
SqlCon.Close()
Return _Sum
End Function
|
 |
 |
 |
 |
Date :
2015-12-15 07:33:04 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แก้ได้แล้วค่ะ
|
ประวัติการแก้ไข 2015-12-15 12:24:48 2015-12-15 13:01:09
 |
 |
 |
 |
Date :
2015-12-15 11:49:33 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
   
ง่ายใช่ป่ะละครับ
ลองเปิดข้อมูลเก่ามาแก้ orderDetails ได้ใช่ไมีครับ
ถ้าแบบนี้ น่าจะแก้ได้เฉพาะ orderID เดียวกันนะครับ
ซึ่งน่าจะผิดหลักครับ
ตัวอย่าง
OR1500001 และ OR1500002 อยู่ใน บิลเดียวกัน
OR1500003 แยกออกมา.
ครั้งต่อไปถ้าเราจะเรียกมาแก้ไขเราจะ Select ก็จะได้แค่ OR1500001,OR1500002,OR1500003
ประมาณนี้หรือป่าวครับ
55555
|
 |
 |
 |
 |
Date :
2015-12-15 13:30:17 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มีข้อสงสัยอีกแล้วค่ะ คือว่าตรงเครดิตและคงเหลือมีวิธีเขียนโค้ดยังไงบ้างคะให้มันคำนวณแบบ 100-35 คงเหลือเท่ากับ 65 พอสั่งรายการที่สอง เครดิตจะเป็น 65-ราคารวม = คงเหลือ ประมาณนี้อ่ะค่ะ

|
ประวัติการแก้ไข 2015-12-15 15:47:58 2015-12-15 15:48:44
 |
 |
 |
 |
Date :
2015-12-15 15:46:11 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
1. for loop ลบเอาแบบบ้านๆครับ
2. select (เครดิต-ราคารวม) As คงเหลือ แบบบ้านๆครับ ตอน select มาใช้งานครับ
|
 |
 |
 |
 |
Date :
2015-12-15 16:05:51 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ต้องเขียน for ประมาณไหนอ่ะคะ ถ้าต้องมีเครดิตทั้งของคนสั่งกับของคนสั่งแทน โดยที่เครดิต select มาจาก DB มาเก็บไว้ใน Label ลองเขียนแล้วคงเหลือเหลือ 0 ตลอดเลย
|
 |
 |
 |
 |
Date :
2015-12-16 09:08:47 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โดยที่เครดิต select มาจาก DB มาเก็บไว้ใน Label ลองเขียนแล้วคงเหลือเหลือ 0 ตลอดเลย
แสดงว่ามีค่าเดียวรึครับ
นึกว่าทั้งหมด 555
แล้วทำไมไม่ลบแบบบ้านๆละครับ
ลองเอาหน้านั้นมาดูหน่อยสิครับมันเป็นยังไงกันแน่นิ 555
|
 |
 |
 |
 |
Date :
2015-12-16 09:39:08 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โค้ดประมาณนี้ค่ะ
Code (VB.NET)
Dim checkname As String = ""
Dim checkempID As String = ""
Dim check As String = ""
Dim checkempGrade As String = ""
Dim checkremain As String
Dim price As String
Dim amount As String
Protected Sub btnadd_Click(sender As Object, e As ImageClickEventArgs) Handles btnadd.Click
Dim dt As DataTable = DirectCast(ViewState("Order"), DataTable)
Dim dr As DataRow = dt.NewRow
btnsubmit.Visible = True 'เมื่อกดปุ่มรูปรถเข็นปุ่ม Submit จะแสดงขึ้นมา
If ddlstore2.SelectedItem.Value = "0" Or ddlmenu.SelectedItem.Value = "0" Or txtquantity.Text = String.Empty Or txtunitPrice.Text = String.Empty Then
messagetxt()
ElseIf txtstock.Text <= "0" And txtstock.Text <> String.Empty Then
messageusedup()
ElseIf chkReplace.Checked And txtempID.Text = String.Empty Then
messagetxtempID()
Else
price = Decimal.Parse(txtunitPrice.Text.Trim)
amount = Decimal.Parse((txtquantity.Text) * (txtunitPrice.Text))
'เช็คการเลือก Option เพิ่มเติม
For Each li As ListItem In chkOption.Items
If li.Selected Then
If check = "" Then
check = (li.Text)
Else
check += ("," + li.Text)
End If
End If
Next
If chkNewoption.Checked = True Then
check = txtoption.Text
End If
For Each li As ListItem In chkOption.Items
If li.Selected AndAlso chkNewoption.Checked = True Then
If check = "" Then
check = (txtoption.Text)
Else
check += ("," + li.Text)
End If
End If
Next
'เช็คว่ามีการสั่งซื้อแทนหรือไม่
If chkReplace.Checked = True Then
checkempID = txtempID.Text.Trim
checkname = lblnamefor.Text.Trim + " " + lbllastnamefor.Text.Trim
checkempGrade = lblempgradefor.Text.Trim
checkremain = lblremainfor.Text.Trim 'เครดิตของคนสั่งซื้อแทนที่ดึงมาจากDB โดยยังไม่ได้คำนวณ
Else '
checkempID = lblempid.Text.Trim
checkname = lblempname.Text.Trim + " " + lblemplastname.Text.Trim
checkempGrade = lblempgrade.Text.Trim
checkremain = lblremain.Text.Trim 'เครดิตของคนสั่งซื้อแทนที่ดึงมาจากDB โดยยังไม่ได้คำนวณ
End If
dr("empID") = checkempID
dr("name") = checkname
dr("foodID") = ddlmenu.SelectedItem.Value
dr("foodName") = ddlmenu.SelectedItem.Text.Trim
dr("store") = ddlstore2.SelectedItem.Text.Trim
dr("option") = check
dr("QTY") = Integer.Parse(txtquantity.Text.Trim)
dr("price") = FormatNumber(price, 2)
dr("amount") = FormatNumber(amount, 2)
dr("empGrade") = checkempGrade
'dr("remain") =
dt.Rows.Add(dr)
ViewState("Order") = dt
Call BindGrid()
'การคำนวณผลรวมของการสั่งซื้อสินค้าทั้งหมด
GridView1.FooterRow.Cells(7).Text = String.Format("{0:0}", dt.Compute("sum(QTY)", ""))
GridView1.FooterRow.Cells(9).Text = String.Format("{0:C}", dt.Compute("sum(amount)", ""))
Call ResetAll()
End If
End Sub

|
ประวัติการแก้ไข 2015-12-16 12:18:57 2015-12-16 12:31:12 2015-12-16 12:34:22 2015-12-16 13:10:04 2015-12-16 13:22:22
 |
 |
 |
 |
Date :
2015-12-16 10:15:57 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โดยที่เครดิต select มาจาก DB มาเก็บไว้ใน Label ลองเขียนแล้วคงเหลือเหลือ 0 ตลอดเลย
Label แบบไหนครับ
แต่ช่างเถอะครับ
จุดไหนเรอะครับที่บอกว่าไม่ได้
|
 |
 |
 |
 |
Date :
2015-12-16 14:33:03 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เขียนออกมาแล้วมันได้แบบนี้อ่ะค่ะ คนละ empID ก็คิดรวมกันหมด

Code (VB.NET)
Dim i, j As Integer
'เช็คว่ามีการสั่งซื้อแทนหรือไม่
If chkReplace.Checked = True Then
checkempID = txtempID.Text.Trim
checkname = lblnamefor.Text.Trim + " " + lbllastnamefor.Text.Trim
checkempGrade = lblempgradefor.Text.Trim
checkremain = lblremainfor.Text.Trim - amount 'เครดิตของคนฝากสั่ง
For i = 0 To dt.Rows.Count - 1
Dim Newremain As Integer
Newremain = checkremain - amount
checkremain = Newremain
Next
Else
checkempID = lblempid.Text.Trim
checkname = lblempname.Text.Trim + " " + lblemplastname.Text.Trim
checkempGrade = lblempgrade.Text.Trim
checkremain = lblremain.Text.Trim - amount 'เครดิตของคนสั่ง
For j = 0 To dt.Rows.Count - 1
Dim _Newremain As Integer
_Newremain = checkremain - amount
checkremain = _Newremain
Next
End If
dr("empID") = checkempID
dr("name") = checkname
dr("foodID") = ddlmenu.SelectedItem.Value
dr("foodName") = ddlmenu.SelectedItem.Text.Trim
dr("store") = ddlstore2.SelectedItem.Text.Trim
dr("option") = check
dr("QTY") = Integer.Parse(txtquantity.Text.Trim)
dr("price") = FormatNumber(price, 2)
dr("amount") = FormatNumber(amount, 2)
dr("empGrade") = checkempGrade
dr("remain") = checkremain
|
ประวัติการแก้ไข 2015-12-16 15:25:28
 |
 |
 |
 |
Date :
2015-12-16 15:24:07 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จะไปยากอะไรครับ
Code (VB.NET)
Public Class OrderEmp
Public Emp As String
Public OrderID As String
Public Remain As Integer
End Class
เวลาจะลบก็ for loop เอาเหมือน ตอนเช็ค OrderID ก็สิ้นเรื่องนิครับ
ไม่เจอ ก็ คิดเป็น 100 พอเจอ ก็ Remain - ออก เรื่อยๆ แบบบ้านๆอ่าครับ
|
 |
 |
 |
 |
Date :
2015-12-16 17:58:19 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเขียนแล้วมันลบให้แค่แถวแรกอ่ะค่ะ พอแถวสองมันก็ไม่ยอมลบให้ต่อช่วยแนะนำทีค่ะ
|
ประวัติการแก้ไข 2015-12-16 19:35:20
 |
 |
 |
 |
Date :
2015-12-16 19:22:43 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เขียนไงครับขอดูหน่อย
|
 |
 |
 |
 |
Date :
2015-12-17 08:26:04 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เขียนแบบนี้อ่ะค่ะ
Code (VB.NET)
For i = 0 To dt.Rows.Count - 1
_remain = Nothing
For Each rm As remain In Relst
If rm.Emp = checkempID Then
_remain = rm
Exit For
End If
Next
Next
'เช็คว่ามีการสั่งซื้อแทนหรือไม่
If chkReplace.Checked = True Then
checkempID = txtempID.Text.Trim
checkname = lblnamefor.Text.Trim + " " + lbllastnamefor.Text.Trim
checkempGrade = lblempgradefor.Text.Trim
checkremain = lblremainfor.Text.Trim
If Relst.Count < 0 Or IsNothing(_remain) Then
Relst.Add(New remain())
Relst(Relst.Count - 1).OrderID = ORID()
Relst(Relst.Count - 1).Emp = checkempID
Relst(Relst.Count - 1).Remain = checkremain - amount
_remain = Relst(Relst.Count - 1)
End If
Else
checkempID = lblempid.Text.Trim
checkname = lblempname.Text.Trim + " " + lblemplastname.Text.Trim
checkempGrade = lblempgrade.Text.Trim
checkremain = lblremain.Text.Trim
If Relst.Count < 0 Or IsNothing(_remain) Then
Relst.Add(New remain())
Relst(Relst.Count - 1).OrderID = ORID()
Relst(Relst.Count - 1).Emp = checkempID
Relst(Relst.Count - 1).Remain = checkremain - amount
_remain = Relst(Relst.Count - 1)
End If
End If
dr("empID") = checkempID
dr("name") = checkname
dr("foodID") = ddlmenu.SelectedItem.Value
dr("foodName") = ddlmenu.SelectedItem.Text.Trim
dr("store") = ddlstore2.SelectedItem.Text.Trim
dr("option") = check
dr("QTY") = Integer.Parse(txtquantity.Text.Trim)
dr("price") = FormatNumber(price, 2)
dr("amount") = FormatNumber(amount, 2)
dr("empGrade") = checkempGrade
dr("remain") = _remain.Remain
|
 |
 |
 |
 |
Date :
2015-12-17 09:18:27 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Relst(Relst.Count - 1).Remain = checkremain - amount
checkremain คือ???มีค่า ???
ต้องเป็น
Relst(Relst.Count - 1).Remain = 100- amount
ผมว่าทางที่ดีลอง
คิดมือก่อนดีกว่าไม๊ครับ
เหมือนยิ่งเขียนยิ่งไปโผล่ดาวอังคารเลยนะนั่น
ความจริงปัญหาของโจทย์นี้แก้ไม่ยากครับแต่เท่าที่เห็น 80% ของการแก้ปัญหาไม่ได้มาจากท่าน heaunn เลย
แต่ไม่ได้บอกว่าไม่ใส่ใจปัญหานะครับ
แค่จะบอกว่ามีวิธีการมองปัญหาแบบผิดๆครับคือมองแคบเกินแล้วจะไม่เห็นมุมกว้าง
พอไม่เห็นมุมกว้าง ถ้าเราเขียนโค้ดต่อไปก็จะเกิดปัญหาจุกจิกเรื่องเดิมๆที่เคยแก้ไปแล้วเรื่อยๆครับ
บ้านๆคือมันจะวนในอ่างครับ
ลองสละเวลาทำตามที่แนะนำดูครับ
1. หยุดการเขียนไว้ก่อน
2. ทำความเข้าใจกับโจทย์ เอาให้มั่นใจเลยครับว่าเข้าใจจริงๆ
3. ลองมานั่งเทียนเขียนค่าแล้วคำนวณเล่นๆก่อน(อย่าเพิ่งลงที่โค๊ด)
4. มโนให้แม่นๆก่อนที่จะเอามาลงโค้ด
นี่คือวิธีแก้ปัญหาครับ
ไม่งั้นจะเกิดอาการวนในอ่าง
ที่แน่
Code (VB.NET)
_remain = Nothing
For Each rm As remain In Relst
If rm.Emp = checkempID Then
_remain = rm
Exit For
End If
Next
Next
'เช็คว่ามีการสั่งซื้อแทนหรือไม่
If chkReplace.Checked = True Then
checkempID = txtempID.Text.Trim
checkname = lblnamefor.Text.Trim + " " + lbllastnamefor.Text.Trim
checkempGrade = lblempgradefor.Text.Trim
checkremain = lblremainfor.Text.Trim
If Relst.Count < 0 Or IsNothing(_remain) Then
Relst.Add(New remain())
Relst(Relst.Count - 1).OrderID = ORID()
Relst(Relst.Count - 1).Emp = checkempID
Relst(Relst.Count - 1).Remain = checkremain - amount
_remain = Relst(Relst.Count - 1)
End If
Else
checkempID = lblempid.Text.Trim
checkname = lblempname.Text.Trim + " " + lblemplastname.Text.Trim
checkempGrade = lblempgrade.Text.Trim
checkremain = lblremain.Text.Trim
If Relst.Count < 0 Or IsNothing(_remain) Then
Relst.Add(New remain())
Relst(Relst.Count - 1).OrderID = ORID()
Relst(Relst.Count - 1).Emp = checkempID
Relst(Relst.Count - 1).Remain = checkremain - amount
_remain = Relst(Relst.Count - 1)
End If
End If
เขียนแบบนี้ไม่ถูกครับ
ลองคิดรูปแบบใหม่ดูครับ
|
 |
 |
 |
 |
Date :
2015-12-17 09:45:27 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาเป็นว่าระบบเป็นอย่างไง
แบบนี้หรือเปล่า
ยกตัวอย่างนะ นาย ก รับ Order แต่ ณ เวลาหนึ่ง นาย ก ไปขี้ นาย ข มารับแทน นาย ข ก็ต้องได้รับประโยชน์ด้วย ใช่หรือไม่
-ถ้าใช้ก็ไม่ต้องสน Order หลัก ทำเป็นสรุปเป็นรายงานเอา เวลาต้องการดูว่าแต่ละวันใครรับ Order อะไร (แบบง่าย)
รายงานไม่จำเป็นเฉพาะใน datagrid เท่านั้นอาจจะออกไป report อย่างอื่น crystal report หรือ report service
แต่ถ้าต้องการใช้ grid อยู่ ก็ loop เก็บค่า add สดเอา
-Order หลักเอาไวเก็บเงินกับลูกค้าเท่านั้น ยอดรวมก็เอามาทั้งนาย ก และ ข นั้นละ คงไม่มีใครแยกบิลของนาย ก และ ข ไปเก็บเงินนะ
-Order หลักเอาไว้สรุปรายรับ แต่ละวัน เดือน ปี
-Order Detail เอาไปสรุปของแต่ละคนว่าใครรับอะไร แต่ละวัน เดือน ปี
มันควรจะเป็นนี้นะครับผมว่า(ผมคิดเอาเอง)
ถ้ายังไม่ได้ ให้ไปถามพี่คนออกแบบฐานข้อมูลบอกว่าต้องการอะไรให้เขามาอธิบายในนี้ก็ได้
หรือส่งโปรแกรมมาให้ดูเลย อ่านดูแล้วสงสารคนตอบ 5555++++ คุณ TOR นั้นล่ะ นักวิทย์ เขียนโปรแกรม
ผมไม่ได้เก่งอะไรนะครับผม แต่ดูๆแล้วคิดว่าคงจะไม่ไปไหนแน่ๆ วนอยู่ที่เดิม
ถ้าต้องการทำระบบร้านอาหารจริงๆ ลองค้นหาดูใน google ดูนะครับตัวอย่างมีเพียบ(ของชาวบ้าน)
|
 |
 |
 |
 |
Date :
2015-12-17 11:53:24 |
By :
บัญดิษฐ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จะลองพยายามคิดให้กว้างกว่านี้ค่ะ ขอบคุณสำหรับคำแนะนำนะคะ 
|
 |
 |
 |
 |
Date :
2015-12-17 13:27:03 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนนี้มันคิดรวมกันหมดทุกไอดีไม่ได้แยกลบราคาของแต่ละคนอ่ะค่ะ
Code (VB.NET)
For i = 0 To dt.Rows.Count - 1
'Dim _NewRemain As Integer
_remain = Nothing
For Each rm As remain In Relst
If rm.Emp = checkempID Then
_remain = rm
Exit For
End If
Next
Next
If Relst.Count < 0 Or IsNothing(_remain) Then
Relst.Add(New remain())
Relst(Relst.Count - 1).OrderID = ORID()
Relst(Relst.Count - 1).Emp = checkempID
Relst(Relst.Count - 1).Remain = checkremain - amount ' checkremain คือตัวแปรที่เก็บค่า remain ของพนักงานแต่ละคน
_remain = Relst(Relst.Count - 1)
For i = 0 To dt.Rows.Count - 1
Dim NewRemain As Integer
Dim _amount As Decimal = amount
Dim _NewRemain As Integer
NewRemain = Relst(Relst.Count - 1).Remain
_NewRemain = NewRemain - _amount
_remain.Remain = _NewRemain
MsgBox(NewRemain)
Next
MsgBox(checkremain)
End If
|
ประวัติการแก้ไข 2015-12-18 09:38:33
 |
 |
 |
 |
Date :
2015-12-18 09:13:29 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Relst(Relst.Count - 1).Remain = checkremain - amount ' checkremain คือตัวแปรที่เก็บค่า remain ของพนักงานแต่ละคน
มันเพิ่มมาไม่ใชคนเดิม ยอดต้องเต็มนะครับ
ลอง alert มาดูครับว่ามันยอดเต็มหรือเป็นยอดที่ลบแล้ว
ยอดที่ลบแล้ว แสดงว่าผิด
ตัดโค้ดนี้ออกครับ
Code (VB.NET)
For i = 0 To dt.Rows.Count - 1
Dim NewRemain As Integer
Dim _amount As Decimal = amount
Dim _NewRemain As Integer
NewRemain = Relst(Relst.Count - 1).Remain
_NewRemain = NewRemain - _amount
_remain.Remain = _NewRemain
MsgBox(NewRemain)
Next
มันลบซ้ำซาก
เปลี่ยนมาใช้ Else
ทำเหมือนตอนที่เพิ่ม OrderID เมื่อครั้งกระโน้นอ่าครับ
อย่าเพิ่งออกดาวอังคารไป
|
 |
 |
 |
 |
Date :
2015-12-18 09:54:10 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ค้อนเซบ เป็นแบบนี้ ครับ
1.เช็คว่า Emp ใน Relst รึไม่
2.ไม่มี ให้เพิ่ม แล้ว _remain = Relst(Relst.Count - 1)
ถ้ามี _remain = rm
3. เอา _remain ไปใช้งานครับ
ลองแก้ใหม่ดูๆๆๆ
|
 |
 |
 |
 |
Date :
2015-12-18 10:10:48 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อะเลิร์ท checkremain ได้ยอด remain เต็มของแต่ละคนมาค่ะ แล้วถ้าคนแรกจะสั่งเพิ่มต้องเอายอดคงเหลือมาใช้ลบกับราคาอาหารรายการต่อไปต่อต้องทำยังไงคะ ตอนนี้อยากอพยพไปอยู่ดาวอังคารแล้วค่ะ ตรงนี้ไม่ไปไหนสักทีเซ็งตัวเองมากเลย 55555555555
|
 |
 |
 |
 |
Date :
2015-12-18 13:28:53 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตาม No. 85 เลยครับ
|
 |
 |
 |
 |
Date :
2015-12-18 13:47:24 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ่านที่ คห. 85 ก็ยังไม่เข้าใจว่าถ้า empID เดียวกันสั่งรายการที่สองจะเอาค่าคงเหลือของรายการก่อนมาลบได้ยังไง คิดตรงนี้ไม่ได้อ่ะคะ
Code (VB.NET)
For i = 0 To dt.Rows.Count - 1
_remain = Nothing
For Each rm As remain In Relst
If rm.Emp = checkempID Then
_remain = rm
Exit For
End If
Next
Next
If Relst.Count < 0 Or IsNothing(_remain) Then
Relst.Add(New remain())
Relst(Relst.Count - 1).Emp = checkempID
Relst(Relst.Count - 1).Amount = amount
Relst(Relst.Count - 1).Remain = checkremain - amount
_remain = Relst(Relst.Count - 1)
Else
End If
|
 |
 |
 |
 |
Date :
2015-12-18 14:51:03 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
For i = 0 To dt.Rows.Count - 1
_remain = Nothing
For Each rm As remain In Relst
If rm.Emp = checkempID Then
_remain = rm 'คนเก่า
Exit For
End If
Next
Next
If Relst.Count < 0 Or IsNothing(_remain) Then
Relst.Add(New remain())
Relst(Relst.Count - 1).Emp = checkempID
Relst(Relst.Count - 1).Amount = amount 'มีไว้ทำอะไรรึ
Relst(Relst.Count - 1).Remain = checkremain
_remain = Relst(Relst.Count - 1) 'คนใหม่
Else
End If
'เวลา ลบ ก็
_remain.Remain = _remain.Remain - amount
ว่ากันว่าไม่ควรกินปลาเลยหลังจากที่หาได้
แต่ควรแบกกลับมาบ้านเพื่อให้ครอบครัวได้อิ่มด้วย
   
|
 |
 |
 |
 |
Date :
2015-12-18 15:15:43 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
_remain.Remain = _remain.Remain - amount
ส่วนนี้ควรเอาไปวางตรงไหนเหรอคะ? เพราะลองใช้แล้วมันก็เอาเครดิตเต็มๆ ที่ดึงจาก DB มาลบทุกรายการสั่งเลยอะค่ะ
|
 |
 |
 |
 |
Date :
2015-12-21 08:08:02 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
วางไว้แบบนั้นครับ
แต่ให้เช็คให้ชัวว่ามัน
เป็น _remain ที่เราต้องการรึป่าว
_remain = rm 'คนเก่า
_remain = Relst(Relst.Count - 1) 'คนใหม่
Relst(Relst.Count - 1).Remain = checkremain 'checkremain ได้เท่าไหร่รึ
|
 |
 |
 |
 |
Date :
2015-12-21 08:52:44 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
For i = 0 To dt.Rows.Count - 1
_remain = Nothing
For Each rm As remain In Relst
If rm.Emp = checkempID Then
_remain = rm
Exit For
End If
Next
Next
If Relst.Count < 0 Or IsNothing(_remain) Then
Relst.Add(New remain())
Relst(Relst.Count - 1).Emp = checkempID
'Relst(Relst.Count - 1).Amount = amount
Relst(Relst.Count - 1).Remain = checkremain
_remain = Relst(Relst.Count - 1)
Else
End If
_remain.Remain = _remain.Remain - amount
'For i = 0 To dt.Rows.Count - 1
' NewRemain = Relst(Relst.Count - 1).Remain
' _NewRemain = NewRemain - _amount
' _remain.Remain = _NewRemain
' MsgBox(NewRemain)
'Next
MsgBox(checkremain)
MsgBox(_remain.Remain)
ตอนนี้มันเป็นแบบนี้ค่ะ ใน DB คนที่1 มีเครดิต 100 สั่งรายการแรก 35 บาท จะเหลือ 65 พอสั่งรายการสอง 30 บาทมันก็ดึงเอา 100 มาลบกับ 30 เป็น 70 อ่ะค่ะ คนที่2 มี 150 สั่งรายการแรก 35 บาทก็เหลือ 115 พอสั่งรายการสองมันก็ดึงเอา 150 มาลบใหม่ตลอดเลย
|
 |
 |
 |
 |
Date :
2015-12-21 13:35:08 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ยาก ๆๆๆ
บ้านๆเลยคือเชคดูว่ามันมาจาก
_remain = Relst(Relst.Count - 1)
หรือ
_remain = rm
ถ้ามาจาก
_remain = Relst(Relst.Count - 1)
ลองตรวจอีกว่า
If Relst.Count < 0 Or IsNothing(_remain) Then
แล้วจะร้องอ๋อออออออ เอง ครับ
|
 |
 |
 |
 |
Date :
2015-12-21 14:26:17 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
If Relst.Count < 0 Or IsNothing(_remain) Then 'ที่บอกให้เช็คตรงนี้คืออะไรเหรอคะ?
|
 |
 |
 |
 |
Date :
2015-12-24 09:12:13 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
If Relst.Count < 0 Or IsNothing(_remain) Then
เกรงว่าจะใส่ _remain = Nothing หลังจากใช้งานแล้ว
แบบนี้มันจะ IsNothing อยู่ตลอดไงละครับ
ลองเชคดู
|
 |
 |
 |
 |
Date :
2015-12-25 08:50:33 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
For i = 0 To dt.Rows.Count - 1
'_remain = Nothing
For Each rm As remain In Relst ' เชคข้อมูลใน Relst
If rm.Emp = checkempID Then
_remain = rm 'คนเก่า
'ถ้ามีข้อมูลใน Relst แล้วให้เรียกใช้ _remain = rm แล้ว Exit For
Exit For
End If
Next
Next
'กรณี เชคด้านบนแล้วไม่เจอ *** ยังไม่มีรายชื่อ ใน Relst ให้ Relst.Add(New remain())
If Relst.Count < 0 Or IsNothing(_remain) Then
Relst.Add(New remain())
Relst(Relst.Count - 1).Emp = checkempID
Relst(Relst.Count - 1).Remain = checkremain
_remain = Relst(Relst.Count - 1) 'คนใหม่
End If
_remain.Remain = checkremain - amount
MsgBox(checkempID)
MsgBox(checkremain)
ลองเอา _remain = Nothing ออกแล้วก็ยังเหมือนเดิม ช่วยหน่อยค่ะตันมาหลายวันแล้ว
|
 |
 |
 |
 |
Date :
2015-12-25 10:12:59 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
If Relst.Count < 0 Or IsNothing(_remain) Then
ออกมาเป็นอะไรครับ
กรณีที่มี checkempID แล้ว
|
 |
 |
 |
 |
Date :
2015-12-25 10:43:36 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
ประวัติการแก้ไข 2015-12-25 14:39:53
 |
 |
 |
 |
Date :
2015-12-25 12:51:46 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันไม่ทำตรงนี้อ่ะค่ะ
Code (VB.NET)
For Each rm As remain In Relst ' เชคข้อมูลใน Relst
If rm.Emp = checkempID Then
_remain = rm 'คนเก่า
MsgBox(checkempID)
'ถ้ามีข้อมูลใน Relst แล้วให้เรียกใช้ _remain = rm แล้ว Exit For
Exit For
End If
Next
|
ประวัติการแก้ไข 2015-12-26 10:33:55
 |
 |
 |
 |
Date :
2015-12-26 10:01:32 |
By :
heaunn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันไม่ทำตรงนี้อ่ะค่ะ คือยังไงรึครับ
|
 |
 |
 |
 |
Date :
2015-12-28 08:19:11 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเปลี่ยนจากอ้างอิงจาก remain ให้เป็น Relst[item] ครับ
Dim item As Integer = 0
For i As Integer = 0 To Relst.Count - 1
If Relst(i) = checkempID Then
item = i
Exit For
End If
Next
'ไม่มีก็เพิ่ม
item = Relst.Count - 1
Relst(item).Remain = ???
|
 |
 |
 |
 |
Date :
2016-01-04 09:04:18 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|