 |
|
[.NET]
บันทึกข้อมูลจาก Datagrid ลง DB กดบันทึกเเล้วขึ้น Debug ตรง .ExecutNoncury() คับ ช่วยที่คับ |
|
 |
|
|
 |
 |
|
Code (VB.NET)
01. Private Sub Button1_Click( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles Button1.Click
02. Using con As New SqlConnection( "data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322" )
03. con.Open()
04. Dim tr As SqlTransaction = con.BeginTransaction
05. For i As Integer = 0 To DataGridView1.RowCount - 1
06. Dim cm As New SqlCommand( "insert into tblbbk1 (keydate,tranid,proid,proname,box,trancost,bill,amount,typeid,zone) values (@a,@b,@c,@d,@e,@f,@g,@h,@i,@j)" , con, tr)
07. With cm
08. .Parameters.AddWithValue( "@a" , dtp.Value.ToString( "MM/dd/yyyy" ))
09. .Parameters.AddWithValue( "@b" , cb1.SelectedValue)
10. .Parameters.AddWithValue( "@c" , DataGridView1.Rows(i).Cells(0))
11. .Parameters.AddWithValue( "@d" , DataGridView1.Rows(i).Cells(1))
12. .Parameters.AddWithValue( "@e" , DataGridView1.Rows(i).Cells(2))
13. .Parameters.AddWithValue( "@f" , DataGridView1.Rows(i).Cells(3))
14. .Parameters.AddWithValue( "@g" , txt6.Text)
15. .Parameters.AddWithValue( "@h" , DataGridView1.Rows(i).Cells(4))
16. .Parameters.AddWithValue( "@i" , ty.Text)
17. .Parameters.AddWithValue( "@j" , tz.Text)
18. .ExecuteNonQuery()
19. End With
20. Next
21. tr.Commit()
22. MessageBox.Show( "บันทึกการแก้ไขข้อมูลเรียบร้อยแล้ว" , "Database" , MessageBoxButtons.OK)
23. DataGridView1.Rows.Clear()
24. End Using
25. End Sub
บันทึกข้อมูลจาก Datagrid ลง DB กดบันทึกเเล้วขึ้น Debug ตรง .ExecutNoncury () คับ ช่วยที่คับ
Tag : .NET, VB.NET
|
|
 |
 |
 |
 |
Date :
2011-06-14 16:58:21 |
By :
crazy-Ugly |
View :
3025 |
Reply :
61 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Error ว่าอะไรครับ 
|
 |
 |
 |
 |
Date :
2011-06-14 18:05:55 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัวอย่างการใช้ SqlParameter ครับ
Code (VB.NET)
01. strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
02.
03. strSQL = "INSERT INTO files (Name,FilesName,FilesType) " & _
04. " VALUES " & _
05. " (@sName,@sFilesName,@sFilesType)"
06. objConn.ConnectionString = strConnString
07. objConn.Open()
08.
09. Dim objCmd As New SqlCommand(strSQL, objConn)
10. objCmd.Parameters.Add( "@sName" , SqlDbType.VarChar).Value = Me .txtName.Text
11. objCmd.Parameters.Add( "@sFilesName" , SqlDbType.Binary).Value = imbByte
12. objCmd.Parameters.Add( "@sFilesType" , SqlDbType.VarChar).Value = strMIME
13. objCmd.ExecuteNonQuery()
Go to : ASP.NET SQL Server BLOB Binary Data and Parameterized Query
Go to : ASP.NET System.Data.SqlClient - Parameter Query (SqlParameter)
|
 |
 |
 |
 |
Date :
2011-06-14 20:53:30 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

นี่คับ มัน error ตรงนี้คับ ขึ้นข้อความแบบนี้คับ
|
 |
 |
 |
 |
Date :
2011-06-15 08:19:51 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ช่วยทีคับพี่ วิน ผมพยายามเขียนอยู่เป็นอาทิดละคับตรงบันทึก ไม่ได้จิงๆคับ เลยมาโพสขอความช่วยเหลือคับ
คือ ผม บันทึกข้อมูลที่มีอยู่ใน Datagrid เลยคับไม่ได้ผูกกับ textbox คับ ผมกระทำการใน Datagrid เลยคับ
โดยข้อมูลใน datagrid ผมดึงข้อมูลมาจาก tabal อื่นคับ ดึงเอาเฉพาะ รหัสสินค้า ชื่อสินค้า เเละจำนวนกล่อง มาคับ เพื่อจะมากรอกค่า Rate เพราะข้อมูลมันเหมือนกันแต่หาผลลัพพ์คนละส่วนงานคับ โดยใช้ Datagrid ตัวนี้เเหละคับเป็นตัวเชื่อม แต่มันบันทึกไม่ได้คับ
ข้อ Debug ดังภาพด้านบนครับ
ขอคุณครับ
|
ประวัติการแก้ไข 2011-06-15 08:53:57 2011-06-15 08:55:08
 |
 |
 |
 |
Date :
2011-06-15 08:23:00 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

Error อย่างนี้คับ
|
 |
 |
 |
 |
Date :
2011-06-15 09:24:21 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเพิ่ม .Value
Original
1. .Parameters.AddWithValue( "@c" , DataGridView1.Rows(i).Cells(0))
เป็นแบบนี้
Revised
1. .Parameters.AddWithValue( "@c" , DataGridView1.Rows(i).Cells(0).Value)
เพิ่มเติม
1. ถ้าจะให้ดีควรใช้ BindingSource ช่วย จะทำให้การเขียนโค้ดไม่ยึดติด Index ของ Column
2. ถ้าจะใช้ SqlTransaction ให้ครบหลักสูตร ต้องมีการเรียกใช้ Method Commit และ Rollback (เรียกเมื่อ Error)
Code (VB.NET)
01. Private Sub Button1_Click( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles Button1.Click
02. Using con As New SqlConnection( "data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322" )
03. con.Open()
04. Dim bs As BindingSource = DataGridView1.DataSource
05. Dim tr As SqlTransaction = con.BeginTransaction
06. Try
07.
08. For i As Integer = 0 To DataGridView1.RowCount - 1
09. Dim cm As New SqlCommand( "insert into tblbbk1 (keydate,tranid,proid,proname,box,trancost,bill,amount,typeid,zone) values (@a,@b,@c,@d,@e,@f,@g,@h,@i,@j)" , con, tr)
10. With cm
11. .Parameters.AddWithValue( "@a" , dtp.Value.ToString( "MM/dd/yyyy" ))
12. .Parameters.AddWithValue( "@b" , cb1.SelectedValue)
13. .Parameters.AddWithValue( "@c" , bs.List(i)( "proid" ))
14. .Parameters.AddWithValue( "@d" , bs.List(i)( "proname" ))
15. .Parameters.AddWithValue( "@e" , bs.List(i)( "box" ))
16. .Parameters.AddWithValue( "@f" , bs.List(i)( "trancost" ))
17. .Parameters.AddWithValue( "@g" , txt6.Text)
18. .Parameters.AddWithValue( "@h" , bs.List(i)( "amount" ))
19. .Parameters.AddWithValue( "@i" , ty.Text)
20. .Parameters.AddWithValue( "@j" , tz.Text)
21. .ExecuteNonQuery()
22. End With
23. Next
24. tr.Commit()
25. MessageBox.Show( "บันทึกการแก้ไขข้อมูลเรียบร้อยแล้ว" , "Database" , MessageBoxButtons.OK)
26. DataGridView1.Rows.Clear()
27. Catch ex As Exception
28. tr.Rollback()
29. MessageBox.Show(ex.Message)
30. Finally
31. tr.Dispose
32. End Try
33. End Using
34. End Sub
|
 |
 |
 |
 |
Date :
2011-06-15 13:15:10 |
By :
watcharop |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เคยใส่ . value ต่อท้ายเเล้วคับ Error จุดเดิมคับ แต่ข้อความที่Debug มาบอกเป็นแบบนี้คับ

|
 |
 |
 |
 |
Date :
2011-06-15 14:35:55 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ช่วยทีคับ
|
 |
 |
 |
 |
Date :
2011-06-15 14:47:02 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
01. Private Sub butsup_Click( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles butsup.Click
02. Using con As New SqlConnection( "data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322" )
03. con.Open()
04. Dim bs As BindingSource = DataGridView1.DataSource
05. Dim tr As SqlTransaction = con.BeginTransaction
06. Try
07. For i As Integer = 0 To DataGridView1.RowCount - 1
08. Dim cm As New SqlCommand( "insert into tblbbk1 (keydate,tranid,proid,proname,box,trancost,bill,amount,typeid,zone) values (@a,@b,@c,@d,@e,@f,@g,@h,@i,@j)" , con, tr)
09. With cm
10. .Parameters.AddWithValue( "@a" , dtp.Value.ToString( "MM/dd/yyyy" ))
11. .Parameters.AddWithValue( "@b" , cb1.SelectedValue)
12. .Parameters.AddWithValue( "@c" , bs.List(i)( "proid" ))
13. .Parameters.AddWithValue( "@d" , bs.List(i)( "proname" ))
14. .Parameters.AddWithValue( "@e" , bs.List(i)( "box" ))
15. .Parameters.AddWithValue( "@f" , bs.List(i)( "trancost" ))
16. .Parameters.AddWithValue( "@g" , txt6.Text)
17. .Parameters.AddWithValue( "@h" , bs.List(i)( "amount" ))
18. .Parameters.AddWithValue( "@i" , ty.Text)
19. .Parameters.AddWithValue( "@j" , tz.Text)
20. .ExecuteNonQuery()
21. End With
22. Next
23. tr.Commit()
24. MessageBox.Show( "บันทึกข้อมูลเรียบร้อยแล้ว" , "Database" , MessageBoxButtons.OK)
25. DataGridView1.Rows.Clear()
26.
27. Catch ex As Exception
28. tr.Rollback()
29. MessageBox.Show(ex.Message)
30. Finally
31. tr.Dispose()
32. End Try
33. End Using
34. End Sub
ผมลองเขียนตามที่พี่แนะนำครับ ยัง Error จุดเดิมคับ .ExecutNonQuery()
มันขึ้น Error แบบนี้คับ

|
ประวัติการแก้ไข 2011-06-15 16:42:27
 |
 |
 |
 |
Date :
2011-06-15 14:52:25 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ช่วยผมด้วยคร๊าป
|
 |
 |
 |
 |
Date :
2011-06-16 08:06:31 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลอง Debug ดูครับ Error ตรง Line ไหนครับ
|
 |
 |
 |
 |
Date :
2011-06-16 08:39:28 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
01. Using con As New SqlConnection( "data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322" )
02. con.Open()
03. Dim bs As BindingSource = DataGridView1.DataSource
04. Dim tr As SqlTransaction = con.BeginTransaction
05. Try
06. For i As Integer = 0 To DataGridView1.RowCount - 1
07. Dim cm As New SqlCommand( "insert into tblbbk1 (keydate,tranid,proid,proname,box,trancost,bill,amount,typeid,zone) values (@a,@b,@c,@d,@e,@f,@g,@h,@i,@j)" , con, tr)
08. With cm
09. .Parameters.AddWithValue( "@a" , dtp.Value.ToString( "MM/dd/yyyy" ))
10. .Parameters.AddWithValue( "@b" , cb1.SelectedValue)
11. .Parameters.AddWithValue( "@c" , bs.List(i)( "proid" ))
12. .Parameters.AddWithValue( "@d" , bs.List(i)( "proname" ))
13. .Parameters.AddWithValue( "@e" , bs.List(i)( "box" ))
14. .Parameters.AddWithValue( "@f" , bs.List(i)( "trancost" ))
15. .Parameters.AddWithValue( "@g" , txt6.Text)
16. .Parameters.AddWithValue( "@h" , bs.List(i)( "amount" ))
17. .Parameters.AddWithValue( "@i" , ty.Text)
18. .Parameters.AddWithValue( "@j" , tz.Text)
19. .ExecuteNonQuery()
20. End With
21. Next
22. tr.Commit()
23. MessageBox.Show( "บันทึกข้อมูลเรียบร้อยแล้ว" , "Database" , MessageBoxButtons.OK)
24. DataGridView1.Rows.Clear()
25.
26. Catch ex As Exception
27. tr.Rollback()
28. MessageBox.Show(ex.Message)
29. Finally
30. tr.Dispose()
31. End Try
32. End Using
33. End Sub
พี่วินครับ จากโค๊ดข้างบนนี้ เวลากด บันทึก rate เเล้วมัน show message เเบบนี้คับ

|
 |
 |
 |
 |
Date :
2011-06-16 10:13:00 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเปลี่ยน
For i As Integer = 0 To DataGridView1.RowCount - 1
เป็น
For i As Integer = 0 To bs.Count - 1
Code (VB.NET)
01. Private Sub butsup_Click( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles butsup.Click
02. Using con As New SqlConnection( "data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322" )
03. con.Open()
04. Dim bs As BindingSource = DataGridView1.DataSource
05. Dim tr As SqlTransaction = con.BeginTransaction
06. Try
07. For i As Integer = 0 To bs.Count - 1
08. Dim cm As New SqlCommand( "insert into tblbbk1 (keydate,tranid,proid,proname,box,trancost,bill,amount,typeid,zone) values (@a,@b,@c,@d,@e,@f,@g,@h,@i,@j)" , con, tr)
09. With cm
10. .Parameters.AddWithValue( "@a" , dtp.Value.ToString( "MM/dd/yyyy" ))
11. .Parameters.AddWithValue( "@b" , cb1.SelectedValue)
12. .Parameters.AddWithValue( "@c" , bs.List(i)( "proid" ))
13. .Parameters.AddWithValue( "@d" , bs.List(i)( "proname" ))
14. .Parameters.AddWithValue( "@e" , bs.List(i)( "box" ))
15. .Parameters.AddWithValue( "@f" , bs.List(i)( "trancost" ))
16. .Parameters.AddWithValue( "@g" , txt6.Text)
17. .Parameters.AddWithValue( "@h" , bs.List(i)( "amount" ))
18. .Parameters.AddWithValue( "@i" , ty.Text)
19. .Parameters.AddWithValue( "@j" , tz.Text)
20. .ExecuteNonQuery()
21. End With
22. Next
23. tr.Commit()
24. MessageBox.Show( "บันทึกข้อมูลเรียบร้อยแล้ว" , "Database" , MessageBoxButtons.OK)
25. DataGridView1.Rows.Clear()
26.
27. Catch ex As Exception
28. tr.Rollback()
29. MessageBox.Show(ex.Message)
30. Finally
31. tr.Dispose()
32. End Try
33. End Using
34. End Sub
|
 |
 |
 |
 |
Date :
2011-06-16 12:54:12 |
By :
watcharop |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เหมือนเดิมคับพี่ ขึ้น message เหมือนเดิมคับ
ขอบใจมากคับที่ช่วยผม ไม่ได้จิงๆ คับ ขอบคุณมากคับ
|
ประวัติการแก้ไข 2011-06-16 17:16:24
 |
 |
 |
 |
Date :
2011-06-16 17:09:58 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ช่วยผมทีคับ มันติดตรงไหนเหรอคับ ?
|
 |
 |
 |
 |
Date :
2011-06-17 08:36:54 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อุปสรรค์จิงๆคับ จะเสดก็ไม่เสด ช่วยผมด้วยครับพี่ๆ
|
 |
 |
 |
 |
Date :
2011-06-17 08:38:56 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมลองเปลี่ยนโดยไม่ใช้ Try คับ มัน error ตรงนี้คับ

|
ประวัติการแก้ไข 2011-06-17 08:55:47
 |
 |
 |
 |
Date :
2011-06-17 08:51:42 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จากภาพด้านบนผมลองเปลี่ยน แบบเป็นนี้คับ

|
 |
 |
 |
 |
Date :
2011-06-17 09:03:28 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนเขียนโค้ด
กำหนดอะไรให้เป็น DataSource ของ DataGridView1
|
 |
 |
 |
 |
Date :
2011-06-17 09:12:21 |
By :
watcharop |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Dim bs As BindingSource = DataGridView1.DataSource คับ
|
ประวัติการแก้ไข 2011-06-17 10:47:49
 |
 |
 |
 |
Date :
2011-06-17 10:44:30 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2011-06-17 10:49:41 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ได้หมายถึงอย่างนั้น
งั้นลองใส่ MessageBox.Show("DataSource: " & DataGridView1.DataSource.GetType.ToString)
แล้วดูว่ามี Message ว่าอย่างไร
Code (VB.NET)
01. Private Sub Button1_Click( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles Button1.Click
02. MessageBox.Show( "DataSource: " & DataGridView1.DataSource. GetType .ToString)
03. Using con As New SqlConnection( "data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322" )
04. con.Open()
05. Dim bs As BindingSource = DataGridView1.DataSource
06. Dim tr As SqlTransaction = con.BeginTransaction
07. Try
08.
09. For i As Integer = 0 To DataGridView1.RowCount - 1
10. Dim cm As New SqlCommand( "insert into tblbbk1 (keydate,tranid,proid,proname,box,trancost,bill,amount,typeid,zone) values (@a,@b,@c,@d,@e,@f,@g,@h,@i,@j)" , con, tr)
11. With cm
12. .Parameters.AddWithValue( "@a" , dtp.Value.ToString( "MM/dd/yyyy" ))
13. .Parameters.AddWithValue( "@b" , cb1.SelectedValue)
14. .Parameters.AddWithValue( "@c" , bs.List(i)( "proid" ))
15. .Parameters.AddWithValue( "@d" , bs.List(i)( "proname" ))
16. .Parameters.AddWithValue( "@e" , bs.List(i)( "box" ))
17. .Parameters.AddWithValue( "@f" , bs.List(i)( "trancost" ))
18. .Parameters.AddWithValue( "@g" , txt6.Text)
19. .Parameters.AddWithValue( "@h" , bs.List(i)( "amount" ))
20. .Parameters.AddWithValue( "@i" , ty.Text)
21. .Parameters.AddWithValue( "@j" , tz.Text)
22. .ExecuteNonQuery()
23. End With
24. Next
25. tr.Commit()
26. MessageBox.Show( "บันทึกการแก้ไขข้อมูลเรียบร้อยแล้ว" , "Database" , MessageBoxButtons.OK)
27. DataGridView1.Rows.Clear()
28. Catch ex As Exception
29. tr.Rollback()
30. MessageBox.Show(ex.Message)
31. Finally
32. tr.Dispose
33. End Try
34. End Using
35. End Sub
|
 |
 |
 |
 |
Date :
2011-06-17 10:53:42 |
By :
watcharop |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าจะให้ดี เอาโค้ด ทั้งฟอร์มมาแปะด้วยจะดีกว่า
อยากดูตอนโหลดข้อมูล
|
 |
 |
 |
 |
Date :
2011-06-17 10:57:11 |
By :
watcharop |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คับ แปป คับ
|
 |
 |
 |
 |
Date :
2011-06-17 11:38:22 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
01. Private Sub butre_Click( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles butre.Click
02. If MessageBox.Show( "คุณต้องการเรียกข้อมูลใช่หรือไม่?" , "Database" , MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
03.
04. DataGridView1.Show()
05. lv.Hide()
06. Using con As New SqlConnection( "data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322" )
07. con.Open()
08. Dim sql As String = "select * from tblsum where keydate ='" & dtp.Value.ToString( "MM/dd/yyyy" ) & "'and tranid ='" & cb1.SelectedValue & "'"
09. Dim cmd As SqlCommand = New SqlCommand(sql, con)
10. Dim dr As SqlDataReader = cmd.ExecuteReader
11. Dim dt = New DataTable
12. If dr.HasRows Then
13. Dim r As Integer = 0
14. While dr.Read
15. With DataGridView1
16. r = .RowCount - 1
17. .Rows.Add()
18. .Rows(r).Cells(0).Value = dr.Item(2)
19. .Rows(r).Cells(1).Value = dr.Item(3)
20. .Rows(r).Cells(2).Value = dr.Item(4)
21. End With
22. End While
23. End If
24. End Using
25. End If
26. DataGridView1.Focus()
อันนี้เป็นการเรียกข้อมูลคับ อิงจากวันที่ และ รหัสสินค้าคับ
|
 |
 |
 |
 |
Date :
2011-06-17 11:40:24 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
ประวัติการแก้ไข 2011-06-17 11:46:25 2011-06-17 11:48:19 2011-06-17 15:02:36
 |
 |
 |
 |
Date :
2011-06-17 11:44:01 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จากโค้ดที่ให้มา
DataGridView1 มิได้ผูกข้อมูลกับ Object ใดๆ เลย
ซึ่งเป็นวิธีที่ไม่ค่อยนิยมทำกันนัก
เพราะฉะนั้นจึงไม่สามารถใช้โค้ดที่ผมเคยให้ไปได้ (โค้ดที่เกี่ยวเนื่องกับ bs)
ต้องใช้วิธีอ่านข้อมูลจาก DataGridView1 โดยตรง
อย่างที่คุณเขียนไว้แต่แรก
ช่วงนี้ลองหาตัวอย่างการใช้ DataGridView มาศึกษาก่อนดีกว่า
ใน WebSite นี้ก็มี
จะช่วยลดการเขียนโค้ดไปได้เยอะเลยล่ะ
|
 |
 |
 |
 |
Date :
2011-06-17 12:44:10 |
By :
watcharop |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่มีหนทางเลยเหรอคับพี่ คือผมใช้ datagrid มาเป็นตัวกลางในการเรียกข้อมูลมาใช้คับ ข้อมูลเดียวกันแต่นำไปใช้คนละส่วนคับ
ส่วนที่ 1 add ข้อมูลจาก การหาค่าประกันสินค้าคับ จะเก็บข้อมูลไว้ใน tblsum
ส่วนที่ 2 (ส่วนที่ผมนำมาตั้งกระทู้อ่ะคับ) ผมดึงข้อมูลมาจาก tblsum เอาเฉพาะ รหัสสินค้า ชื่อสินค้า และ จำนวนกล่อง คับ เพื่อมาหาค่า Rate คับ
เลยไม่ได้ผูกกับวัตถุใดๆ คับ ตอนเเรกลองดึงข้อมูลมาใส่ใน Listview เเต่ไม่ Listview ไม่สามารถเเทรข้อมูลต่อท้ายข้อมูลที่ดุงมาได้คับ เลยลองมาใช้ datagrid ผลลัพพ์คือ สามารถกระทำการได้ แต่ติดตรง บีนทึกลง DB นี่แหละคับ จนปัญญาจิงๆคับ
|
ประวัติการแก้ไข 2011-06-17 15:10:51
 |
 |
 |
 |
Date :
2011-06-17 15:03:02 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ช่วยผมด้วยคับ
|
ประวัติการแก้ไข 2011-06-18 08:19:01
 |
 |
 |
 |
Date :
2011-06-18 08:17:36 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มีส่วนไหมคับที่เวลาผมดึงข้อมูลมา จะมี Record ที่ว่างอยู่สุดท้ายเสมอ อะคับ ส่วนนี้จะทำให้ Error ได้เป่าคับ

เเล้วจะสามารถทำให้เวลาดึงข้อมูลมา ไม่เอา record ที่เป็นค่า ว่างได้เป่าคับ ให้พอดีกับข้อมูลที่ดึงมาเลยคับ
โค๊ด ดึงข้อมูล
Code (VB.NET)
01. f MessageBox.Show( "คุณต้องการเรียกข้อมูลใช่หรือไม่?" , "Database" , MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
02.
03. DataGridView1.Show()
04. lv.Hide()
05. Using con As New SqlConnection( "data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322" )
06. con.Open()
07. Dim sql As String = "select * from tblsum where keydate ='" & dtp.Value.ToString( "MM/dd/yyyy" ) & "'and tranid ='" & cb1.SelectedValue & "'"
08. Dim cmd As SqlCommand = New SqlCommand(sql, con)
09. Dim dr As SqlDataReader = cmd.ExecuteReader
10. Dim dt = New DataTable
11. If dr.HasRows Then
12. Dim r As Integer = 0
13. While dr.Read
14. With DataGridView1
15. r = .RowCount - 1
16. .Rows.Add()
17. .Rows(r).Cells(0).Value = dr.Item(2)
18. .Rows(r).Cells(1).Value = dr.Item(3)
19. .Rows(r).Cells(2).Value = dr.Item(4)
20. End With
21. End While
22. End If
23. End Using
24. End If
25. DataGridView1.Focus()
|
 |
 |
 |
 |
Date :
2011-06-18 09:25:37 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2011-06-18 13:44:39 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใน DataGridViewRow จะมี Property
ชื่อ IsnewRow (ถ้าจำไม่ผิด) ก็ใช้ประโยชน์จากตัวนี้แหละ
|
 |
 |
 |
 |
Date :
2011-06-18 17:19:27 |
By :
watcharop |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอเป็น Code ได้ไหมคับพี่ คือผมดูเเล้วมันไม่มีคับ IsnewRow
|
 |
 |
 |
 |
Date :
2011-06-20 08:39:38 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมใช้ 3.5 + SQL 2005 คับ
Code (VB.NET)
01. If MessageBox.Show( "คุณต้องการเรียกข้อมูลใช่หรือไม่?" , "Database" , MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
02.
03. DataGridView1.Show()
04. lv.Hide()
05. Using con As New SqlConnection( "data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322" )
06. con.Open()
07. Dim sql As String = "select * from tblsum where keydate ='" & dtp.Value.ToString( "MM/dd/yyyy" ) & "'and tranid ='" & cb1.SelectedValue & "'"
08. Dim cmd As SqlCommand = New SqlCommand(sql, con)
09. Dim dr As SqlDataReader = cmd.ExecuteReader
10. Dim dt = New DataTable
11. If dr.HasRows Then
12. Dim r As Integer = 0
13. While dr.Read
14. With DataGridView1
15. r = .RowCount - 1
16. .Rows.Add()
17. .Rows(r).Cells(0).Value = dr.Item(2)
18. .Rows(r).Cells(1).Value = dr.Item(3)
19. .Rows(r).Cells(2).Value = dr.Item(4)
20. End With
21. End While
22. End If
23. End Using
24. End If
25. DataGridView1.Focus()
จากโค๊ดด้านบนนี้ ผมต้องเขียนยังไงคับ ช่วยที่คับพี่ ตรงนี้ได้ก็เสร็จครับโปรแกรมนี้
|
 |
 |
 |
 |
Date :
2011-06-20 10:54:01 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โค๊ดบันทึกคับ จะต้องเขียนยังไงคับ ไม่ทราบจิงๆคับ
Code (VB.NET)
01. Private Sub butsup_Click( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles butsup.Click
02. Using con As New SqlConnection( "data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322" )
03. con.Open()
04.
05. Dim tr As SqlTransaction = con.BeginTransaction
06.
07. For i As Integer = 0 To DataGridView1.RowCount - 1
08.
09. Dim cm As New SqlCommand( "insert into tblbbk1 (keydate,tranid,proid,proname,box,trancost,bill,amount,typeid,zone) values (@a,@b,@c,@d,@e,@f,@g,@h,@i,@j)" , con, tr)
10. With cm
11. .Parameters.AddWithValue( "@a" , dtp.Value.ToString( "MM/dd/yyyy" ))
12. .Parameters.AddWithValue( "@b" , cb1.SelectedValue)
13. .Parameters.AddWithValue( "@c" , DataGridView1.Rows(i).Cells(0).Value)
14. .Parameters.AddWithValue( "@d" , DataGridView1.Rows(i).Cells(1).Value)
15. .Parameters.AddWithValue( "@e" , DataGridView1.Rows(i).Cells(2).Value)
16. .Parameters.AddWithValue( "@f" , DataGridView1.Rows(i).Cells(3).Value)
17. .Parameters.AddWithValue( "@g" , txt6.Text)
18. .Parameters.AddWithValue( "@h" , DataGridView1.Rows(i).Cells(4).Value)
19. .Parameters.AddWithValue( "@i" , ty.Text)
20. .Parameters.AddWithValue( "@j" , tz.Text)
21. .ExecuteNonQuery()
22. End With
23. Next
24. tr.Commit()
25. MessageBox.Show( "บันทึกข้อมูลเรียบร้อยแล้ว" , "Database" , MessageBoxButtons.OK)
26. DataGridView1.Rows.Clear()
27.
28.
29.
30.
31.
32.
33.
34. End Using
35. End Sub
|
 |
 |
 |
 |
Date :
2011-06-21 08:39:22 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันยาก
เลยไม่เคยอยากเขียนให้ดู เหนื่อย
ผมไม่เคยใช้ DataGridView เก็บข้อมูลตรงๆ แบบนี้
เพราะมันจะวุ่นวายทั้งตอนโหลดข้อมูล และตอนบันทึกนี่แหละ
|
 |
 |
 |
 |
Date :
2011-06-21 09:00:24 |
By :
watcharop |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พอมีหนทางอื่นไหมครับพี่ที่พอจะทำได้นอกจาก ที่ผมทำ
|
 |
 |
 |
 |
Date :
2011-06-21 13:22:07 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จากกระทู้ 39 จขกท. ทดสอบส่งค่าเป็น ข้อมูลตรงๆดูได้ไหมครับ ว่าผ่านหรือไม่ โดยไม่ต้องอ้างอิงไป Gridview ได้ผลอย่างไรบอกให้ทราบด้วยครับ
|
 |
 |
 |
 |
Date :
2011-06-21 14:35:59 |
By :
kaimuk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อธิบายก่อนน่ะครับ คือผมดึงข้อมูลมาจากตารางอื่นน่ะคับ ไม่ได้ Add ข้อมูลเข้าDatagrid ทั้งหมด และไม่ได้ผูก กับ เครื่องมือใดๆ คับ
แบบว่าใช้ Datagrid เป็นตัวแสดงผลเฉยๆคับ
แบบนี้คับ

จากภาพด้านบน ผมจะคีย์ค่า Rate ลงไป (คีย์ในตาราง Datagrid เลยน่ะคับ ไม่ได้คีย์ใน Txtbox)
แล้วในช่อง Amount = จำนวนกล่อง * Rate
ผมใช้ Code นี้คับในการคำนวน
Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
Dim i As Integer
For i = 0 To DataGridView1.RowCount - 1
DataGridView1.Rows(i).Cells(4).Value = DataGridView1.Rows(i).Cells(3).Value * DataGridView1.Rows(i).Cells(2).Value
Next
End Sub
ดังภาพนี้คับ

ปัญหาคือ พอผมคีย์ค่า Rate ครบตามข้อมูลที่ดึงมา เสร็จ แล้วกดบันทึก มันข้อ Error debug คับ (กระทู้ที่ 7)
ช่วยทีคับ
|
ประวัติการแก้ไข 2011-06-21 16:47:18 2011-06-21 16:52:11 2011-06-21 16:53:05 2011-06-21 16:55:27 2011-06-21 16:57:55 2011-06-21 16:59:53
 |
 |
 |
 |
Date :
2011-06-21 16:43:46 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
01. With cm
02. .Parameters.AddWithValue( "@a" , dtp.Value.ToString( "MM/dd/yyyy" ))
03. .Parameters.AddWithValue( "@b" , cb1.SelectedValue)
04. .Parameters.AddWithValue( "@c" , DataGridView1.Rows(i).Cells(0).Value)
05. .Parameters.AddWithValue( "@d" , DataGridView1.Rows(i).Cells(1).Value)
06. .Parameters.AddWithValue( "@e" , DataGridView1.Rows(i).Cells(2).Value)
07. .Parameters.AddWithValue( "@f" , DataGridView1.Rows(i).Cells(3).Value)
08. .Parameters.AddWithValue( "@g" , txt6.Text)
09. .Parameters.AddWithValue( "@h" , DataGridView1.Rows(i).Cells(4).Value)
10. .Parameters.AddWithValue( "@i" , ty.Text)
11. .Parameters.AddWithValue( "@j" , tz.Text)
12. .ExecuteNonQuery()
13. End With
ตรง DataGridView1.Rows(i).Cells(0).Value บราๆๆ ทั้งหลายเนี้ยครับ กรณีที่เราใส่ค่าไปตรงๆเลย จะผ่านหรือไม่ จะได้พอสรุปได้ว่าเป็นกับ DataGrid หรือไม่น่ะครับ
|
 |
 |
 |
 |
Date :
2011-06-21 16:59:17 |
By :
kaimuk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังไงเหรอคับ ส่งค่าโดยตรง ผมดึงข้อมูลมาครับ ดูกระทู้ 44 คับ
อย่าว่ากันน่ะคับผมไม่รู้จิงๆ มือใหม่คับ
|
ประวัติการแก้ไข 2011-06-21 17:07:48 2011-06-21 17:08:35 2011-06-21 17:09:14
 |
 |
 |
 |
Date :
2011-06-21 17:05:56 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ช่วยผมทีครับ
|
 |
 |
 |
 |
Date :
2011-06-22 08:09:01 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำไมไม่ลอง Insert เริ่มจากจำนวน Column น้อยๆ ก่อนล่ะ
จะได้รู้ว่า Column ไหนมีปัญหา
เช่นจาก Column ทั้งหมด keydate,tranid,proid,proname,box,trancost,bill,amount,typeid,zone
ก็ลอง Insert แค่ ฟิล์ดที่เป็นตัวเลข กับเป็น Text ก่อน และก็ฟิล์ด ที่ไม่เกี่ยวข้องกับ DataGridView
จะได้มั่นใจว่า ปัญหาเกิดจากฟิล์ดวันที่ หรือฟิล์ดใน DataGridView
ปัญหาของมนุษย์หลายๆ ท่าน ก็เกิดจากการ Insert ฟิล์ดวันที่
Code (VB.NET)
01. Private Sub Button1_Click( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles Button1.Click
02. Using con As New SqlConnection( "data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322" )
03. con.Open()
04. Dim tr As SqlTransaction = con.BeginTransaction
05. For i As Integer = 0 To DataGridView1.RowCount - 1
06. Dim cm As New SqlCommand( "insert into tblbbk1 (keydate,tranid,proid,proname,box,trancost,bill,amount,typeid,zone) values (@a,@b,@c,@d,@e,@f,@g,@h,@i,@j)" , con, tr)
07. With cm
08. .Parameters.AddWithValue( "@a" , dtp.Value.ToString( "MM/dd/yyyy" ))
09. .Parameters.AddWithValue( "@b" , cb1.SelectedValue)
10. .Parameters.AddWithValue( "@c" , DataGridView1.Rows(i).Cells(0))
11. .Parameters.AddWithValue( "@d" , DataGridView1.Rows(i).Cells(1))
12. .Parameters.AddWithValue( "@e" , DataGridView1.Rows(i).Cells(2))
13. .Parameters.AddWithValue( "@f" , DataGridView1.Rows(i).Cells(3))
14. .Parameters.AddWithValue( "@g" , txt6.Text)
15. .Parameters.AddWithValue( "@h" , DataGridView1.Rows(i).Cells(4))
16. .Parameters.AddWithValue( "@i" , ty.Text)
17. .Parameters.AddWithValue( "@j" , tz.Text)
18. .ExecuteNonQuery()
19. End With
20. Next
21. tr.Commit()
22. MessageBox.Show( "บันทึกการแก้ไขข้อมูลเรียบร้อยแล้ว" , "Database" , MessageBoxButtons.OK)
23. DataGridView1.Rows.Clear()
24. End Using
25. End Sub
|
 |
 |
 |
 |
Date :
2011-06-22 08:39:24 |
By :
watcharop |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 48 เขียนโดย : watcharop เมื่อวันที่ 2011-06-22 08:39:24
รายละเอียดของการตอบ ::
ตามที่ท่านหางอึ่งกล่าวแหละครับ วันที่นี้ตัวปราบเซียนเลย ทดลองประมาณนี้ครับ
Code (VB.NET)
01. With cm
02. .Parameters.AddWithValue( "@a" , "'2011-06-22 00:00:00'" )
03. .Parameters.AddWithValue( "@b" , "B" )
04. .Parameters.AddWithValue( "@c" , "B" )
05. .Parameters.AddWithValue( "@d" , "B" )
06. .Parameters.AddWithValue( "@e" , "B" )
07. .Parameters.AddWithValue( "@f" , "B" )
08. .Parameters.AddWithValue( "@g" , "B" )
09. .Parameters.AddWithValue( "@h" , "B" )
10. .Parameters.AddWithValue( "@i" , "B" )
11. .Parameters.AddWithValue( "@j" , "B" )
12. .ExecuteNonQuery()
13. End With
ส่วนอันไหนที่มันเป็น FK ก็ทำให้มันตรง ทดลอง Run ดูว่าผ่านหรือไม่น่ะครับ
|
 |
 |
 |
 |
Date :
2011-06-22 08:49:06 |
By :
kaimuk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

เหมือนเดิมคับ
|
 |
 |
 |
 |
Date :
2011-06-22 10:40:58 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

Database คับ
|
 |
 |
 |
 |
Date :
2011-06-22 10:45:56 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
" ทำไมไม่ลอง Insert เริ่มจากจำนวน Column น้อยๆ ก่อนล่ะ
จะได้รู้ว่า Column ไหนมีปัญหา
เช่นจาก Column ทั้งหมด keydate,tranid,proid,proname,box,trancost,bill,amount,typeid,zone
ก็ลอง Insert แค่ ฟิล์ดที่เป็นตัวเลข กับเป็น Text ก่อน และก็ฟิล์ด ที่ไม่เกี่ยวข้องกับ DataGridView
จะได้มั่นใจว่า ปัญหาเกิดจากฟิล์ดวันที่ หรือฟิล์ดใน DataGridView
ปัญหาของมนุษย์หลายๆ ท่าน ก็เกิดจากการ Insert ฟิล์ดวันที่ "

แบบนี้บันทึกได้คับ ข้อมูลเข้า DB คับ
|
ประวัติการแก้ไข 2011-06-22 10:54:29 2011-06-22 10:55:05
 |
 |
 |
 |
Date :
2011-06-22 10:51:47 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ก็เป็นนิมิตหมายที่ดี
งั้นต่อไปก็ลองกับ DataGridView
ทีละ Column
เดี๋ยวก็เจอล่ะ
|
 |
 |
 |
 |
Date :
2011-06-22 11:02:51 |
By :
watcharop |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ได้เลยคับ บันทึกข้อมุลใน Datagrid ไม่ได้ซัก Column เลยคับ
และผมลองเอา Code คำนวนออก ก็บันทึกไม่ได้คับ แต่มันขึ้น debug แบบนี้คับ


|
ประวัติการแก้ไข 2011-06-22 14:14:27 2011-06-22 14:29:37 2011-06-22 14:31:58
 |
 |
 |
 |
Date :
2011-06-22 14:13:50 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เริ่มจาก บันทึกเฉพาะ proid ซึ่งมีชนิดข้อมูลเป็น nvarchar(2)
Code (VB.NET)
01. Private Sub Button1_Click( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles Button1.Click
02. Using con As New SqlConnection( "data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322" )
03. con.Open()
04. Dim tr As SqlTransaction = con.BeginTransaction
05.
06.
07. Dim cm As New SqlCommand( "insert into tblbbk1 (keydate,tranid,proid,bill,typeid,zone) values (@a,@b,@proid,@g,@i,@j)" , con, tr)
08. With cm
09. .Parameters.AddWithValue( "@a" , dtp.Value.ToString( "MM/dd/yyyy" ))
10. .Parameters.AddWithValue( "@b" , cb1.SelectedValue)
11. .Parameters.Add( "@proid" , SqlDbType.NVarChar,2)
12.
13. .Parameters.AddWithValue( "@g" , txt6.Text)
14.
15. .Parameters.AddWithValue( "@i" , ty.Text)
16. .Parameters.AddWithValue( "@j" , tz.Text)
17. End With
18.
19. For i As Integer = 0 To DataGridView1.RowCount - 1
20. If Not DataGridView1.Rows(i).IsNewRow Then
21.
22. cm.Parameters( "@proid" ).Value = DataGridView1.Rows(i).Cells(0).Value
23. cm.ExecuteNonQuery()
24. End If
25. Next
26.
27. tr.Commit()
28. MessageBox.Show( "บันทึกการแก้ไขข้อมูลเรียบร้อยแล้ว" , "Database" , MessageBoxButtons.OK)
29. DataGridView1.Rows.Clear()
30. End Using
31. End Sub
|
 |
 |
 |
 |
Date :
2011-06-22 14:44:03 |
By :
watcharop |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จาก Code กระทู้งด้านบนครับ
ได้ครับ พี่หางอึ่ง บันทึกได้ครับ
ข้อมูลใน Datagrid ส่วนของ proid บันทึกได้ครับ ลงใน DB แล้วครับ (พอมีความหวังเเล้วครับ)
|
ประวัติการแก้ไข 2011-06-22 16:47:32 2011-06-22 16:49:15 2011-06-22 16:50:07
 |
 |
 |
 |
Date :
2011-06-22 16:45:46 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
01. Private Sub butsup_Click( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles butsup.Click
02. Using con As New SqlConnection( "data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322" )
03. con.Open()
04. Dim tr As SqlTransaction = con.BeginTransaction
05.
06. Dim cm As New SqlCommand( "insert into tblbbk1 (keydate,tranid,proid,proname,box,trancost,bill,amount,typeid,zone) values (@a,@b,@c,@d,@e,@f,@g,@h,@i,@j)" , con, tr)
07. With cm
08. .Parameters.AddWithValue( "@a" , dtp.Value.ToString( "MM/dd/yyyy" ))
09. .Parameters.AddWithValue( "@b" , cb1.SelectedValue)
10. .Parameters.Add( "@c" , SqlDbType.NVarChar, 2)
11. .Parameters.Add( "@d" , SqlDbType.NVarChar, 50)
12. .Parameters.Add( "@e" , SqlDbType.Int)
13. .Parameters.Add( "@f" , SqlDbType.NVarChar, 20)
14. .Parameters.AddWithValue( "@g" , txt6.Text)
15. .Parameters.Add( "@h" , SqlDbType.Float)
16. .Parameters.AddWithValue( "@i" , ty.Text)
17. .Parameters.AddWithValue( "@j" , tz.Text)
18. End With
19. Try
20. For i As Integer = 0 To DataGridView1.RowCount - 1
21. If Not DataGridView1.Rows(i).IsNewRow Then
22.
23. cm.Parameters( "@c" ).Value = DataGridView1.Rows(i).Cells(0).Value
24. cm.Parameters( "@d" ).Value = DataGridView1.Rows(i).Cells(1).Value
25. cm.Parameters( "@e" ).Value = DataGridView1.Rows(i).Cells(2).Value
26. cm.Parameters( "@f" ).Value = DataGridView1.Rows(i).Cells(3).Value
27. cm.Parameters( "@h" ).Value = DataGridView1.Rows(i).Cells(4).Value
28. cm.ExecuteNonQuery()
29. End If
30. Next
31. tr.Commit()
32. If MessageBox.Show( "คุณต้องการบันทึกข้อมูลใช่หรือไม่?" , "Database" , MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
33. MessageBox.Show( "บันทึกข้อมูลเรียบร้อยเเล้ว" , "Database" , MessageBoxButtons.OK, MessageBoxIcon.Information)
34. DataGridView1.Rows.Clear()
35. DataGridView1.Hide()
36. lv.Show()
37. butsup.Enabled = False
38. butre.Enabled = False
39. End If
40. Catch ex As Exception
41. tr.Rollback()
42. MessageBox.Show(ex.Message)
43. Finally
44. tr.Dispose()
45. End Try
46. End Using
47. butre.Enabled = True
48. butadd.Enabled = True
49. butprint.Enabled = True
50. buts.Enabled = True
51. End Sub
ได้เเล้วครับ พี่ ขอบคุณมากครับ สุดยอดครับ ขอบคุณๆๆๆๆๆๆ มากๆ ครับ
|
 |
 |
 |
 |
Date :
2011-06-23 09:44:21 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เห็นต่อเนื่องหลายวัน ก็เอาใจช่วยอยู่ครับ
Cheer !! 
|
 |
 |
 |
 |
Date :
2011-06-24 09:40:06 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เพิ่งเห็นว่า Mr.Win บอกวิธีตั้งแต่ Post#2
ถ้าจขกท. เชื่อตั้งแต่ตอนแรก ก็เสร็จไปนานแล้ว
|
 |
 |
 |
 |
Date :
2011-06-24 10:37:27 |
By :
watcharop |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ i Love ThaiCreate.Com
|
 |
 |
 |
 |
Date :
2011-06-24 10:43:24 |
By :
คุณศุภกร |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากครับ
น้ำใจงามจิงๆครับ
|
 |
 |
 |
 |
Date :
2011-06-25 11:14:48 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|