 |
|
บันทึกข้อมูลจาก Datagrid ลง DB กดบันทึกเเล้วขึ้น Debug ตรง .ExecutNoncury() คับ ช่วยที่คับ |
|
 |
|
|
 |
 |
|
Code (VB.NET)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Using con As New SqlConnection("data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322")
con.Open()
Dim tr As SqlTransaction = con.BeginTransaction
For i As Integer = 0 To DataGridView1.RowCount - 1
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)
With cm
.Parameters.AddWithValue("@a", dtp.Value.ToString("MM/dd/yyyy"))
.Parameters.AddWithValue("@b", cb1.SelectedValue)
.Parameters.AddWithValue("@c", DataGridView1.Rows(i).Cells(0))
.Parameters.AddWithValue("@d", DataGridView1.Rows(i).Cells(1))
.Parameters.AddWithValue("@e", DataGridView1.Rows(i).Cells(2))
.Parameters.AddWithValue("@f", DataGridView1.Rows(i).Cells(3))
.Parameters.AddWithValue("@g", txt6.Text)
.Parameters.AddWithValue("@h", DataGridView1.Rows(i).Cells(4))
.Parameters.AddWithValue("@i", ty.Text)
.Parameters.AddWithValue("@j", tz.Text)
.ExecuteNonQuery()
End With
Next
tr.Commit()
MessageBox.Show("บันทึกการแก้ไขข้อมูลเรียบร้อยแล้ว", "Database", MessageBoxButtons.OK)
DataGridView1.Rows.Clear()
End Using
End Sub
บันทึกข้อมูลจาก Datagrid ลง DB กดบันทึกเเล้วขึ้น Debug ตรง .ExecutNoncury () คับ ช่วยที่คับ
Tag : .NET, VB.NET
|
|
 |
 |
 |
 |
Date :
2011-06-14 16:58:21 |
By :
crazy-Ugly |
View :
3037 |
Reply :
61 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Error ว่าอะไรครับ 
|
 |
 |
 |
 |
Date :
2011-06-14 18:05:55 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัวอย่างการใช้ SqlParameter ครับ
Code (VB.NET)
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
strSQL = "INSERT INTO files (Name,FilesName,FilesType) " & _
" VALUES " & _
" (@sName,@sFilesName,@sFilesType)"
objConn.ConnectionString = strConnString
objConn.Open()
Dim objCmd As New SqlCommand(strSQL, objConn)
objCmd.Parameters.Add("@sName", SqlDbType.VarChar).Value = Me.txtName.Text
objCmd.Parameters.Add("@sFilesName", SqlDbType.Binary).Value = imbByte
objCmd.Parameters.Add("@sFilesType", SqlDbType.VarChar).Value = strMIME
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
.Parameters.AddWithValue("@c", DataGridView1.Rows(i).Cells(0))
เป็นแบบนี้
Revised
.Parameters.AddWithValue("@c", DataGridView1.Rows(i).Cells(0).Value)
เพิ่มเติม
1. ถ้าจะให้ดีควรใช้ BindingSource ช่วย จะทำให้การเขียนโค้ดไม่ยึดติด Index ของ Column
2. ถ้าจะใช้ SqlTransaction ให้ครบหลักสูตร ต้องมีการเรียกใช้ Method Commit และ Rollback (เรียกเมื่อ Error)
Code (VB.NET)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Using con As New SqlConnection("data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322")
con.Open()
Dim bs As BindingSource = DataGridView1.DataSource ' จะ Error ถ้าไม่ได้ใช้ BindingSource เป็น DataSource ในตอนแรก
Dim tr As SqlTransaction = con.BeginTransaction
Try
For i As Integer = 0 To DataGridView1.RowCount - 1
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)
With cm
.Parameters.AddWithValue("@a", dtp.Value.ToString("MM/dd/yyyy"))
.Parameters.AddWithValue("@b", cb1.SelectedValue)
.Parameters.AddWithValue("@c", bs.List(i)("proid"))
.Parameters.AddWithValue("@d", bs.List(i)("proname"))
.Parameters.AddWithValue("@e", bs.List(i)("box"))
.Parameters.AddWithValue("@f", bs.List(i)("trancost"))
.Parameters.AddWithValue("@g", txt6.Text)
.Parameters.AddWithValue("@h", bs.List(i)("amount"))
.Parameters.AddWithValue("@i", ty.Text)
.Parameters.AddWithValue("@j", tz.Text)
.ExecuteNonQuery()
End With
Next
tr.Commit()
MessageBox.Show("บันทึกการแก้ไขข้อมูลเรียบร้อยแล้ว", "Database", MessageBoxButtons.OK)
DataGridView1.Rows.Clear()
Catch ex As Exception
tr.Rollback() ' ถ้าจะใช้ SqlTransaction ก็ควรจะมี tr.Rollback() เพื่อให้ทำงานได้เต็มรูปแบบ
MessageBox.Show(ex.Message)
Finally
tr.Dispose
End Try
End Using
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)
Private Sub butsup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butsup.Click
Using con As New SqlConnection("data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322")
con.Open()
Dim bs As BindingSource = DataGridView1.DataSource 'จะ Error ถ้าไม่ได้ใช้ BindingSource เป็น DataSource ในตอนแรก
Dim tr As SqlTransaction = con.BeginTransaction
Try
For i As Integer = 0 To DataGridView1.RowCount - 1
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)
With cm
.Parameters.AddWithValue("@a", dtp.Value.ToString("MM/dd/yyyy"))
.Parameters.AddWithValue("@b", cb1.SelectedValue)
.Parameters.AddWithValue("@c", bs.List(i)("proid"))
.Parameters.AddWithValue("@d", bs.List(i)("proname"))
.Parameters.AddWithValue("@e", bs.List(i)("box"))
.Parameters.AddWithValue("@f", bs.List(i)("trancost"))
.Parameters.AddWithValue("@g", txt6.Text)
.Parameters.AddWithValue("@h", bs.List(i)("amount"))
.Parameters.AddWithValue("@i", ty.Text)
.Parameters.AddWithValue("@j", tz.Text)
.ExecuteNonQuery()
End With
Next
tr.Commit()
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว", "Database", MessageBoxButtons.OK)
DataGridView1.Rows.Clear()
'หากโปรแกรม Error ให้เเสดงข้อมความของ Debug โดยใช้ Catch . . . as Exception
Catch ex As Exception
tr.Rollback()
MessageBox.Show(ex.Message)
Finally
tr.Dispose()
End Try
End Using
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)
Using con As New SqlConnection("data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322")
con.Open()
Dim bs As BindingSource = DataGridView1.DataSource 'จะ Error ถ้าไม่ได้ใช้ BindingSource เป็น DataSource ในตอนแรก
Dim tr As SqlTransaction = con.BeginTransaction
Try
For i As Integer = 0 To DataGridView1.RowCount - 1
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)
With cm
.Parameters.AddWithValue("@a", dtp.Value.ToString("MM/dd/yyyy"))
.Parameters.AddWithValue("@b", cb1.SelectedValue)
.Parameters.AddWithValue("@c", bs.List(i)("proid"))
.Parameters.AddWithValue("@d", bs.List(i)("proname"))
.Parameters.AddWithValue("@e", bs.List(i)("box"))
.Parameters.AddWithValue("@f", bs.List(i)("trancost"))
.Parameters.AddWithValue("@g", txt6.Text)
.Parameters.AddWithValue("@h", bs.List(i)("amount"))
.Parameters.AddWithValue("@i", ty.Text)
.Parameters.AddWithValue("@j", tz.Text)
.ExecuteNonQuery()
End With
Next
tr.Commit()
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว", "Database", MessageBoxButtons.OK)
DataGridView1.Rows.Clear()
'หากโปรแกรม Error ให้เเสดงข้อมความของ Debug โดยใช้ Catch . . . as Exception
Catch ex As Exception
tr.Rollback()
MessageBox.Show(ex.Message)
Finally
tr.Dispose()
End Try
End Using
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)
Private Sub butsup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butsup.Click
Using con As New SqlConnection("data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322")
con.Open()
Dim bs As BindingSource = DataGridView1.DataSource 'จะ Error ถ้าไม่ได้ใช้ BindingSource เป็น DataSource ในตอนแรก
Dim tr As SqlTransaction = con.BeginTransaction
Try
For i As Integer = 0 To bs.Count - 1
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)
With cm
.Parameters.AddWithValue("@a", dtp.Value.ToString("MM/dd/yyyy"))
.Parameters.AddWithValue("@b", cb1.SelectedValue)
.Parameters.AddWithValue("@c", bs.List(i)("proid"))
.Parameters.AddWithValue("@d", bs.List(i)("proname"))
.Parameters.AddWithValue("@e", bs.List(i)("box"))
.Parameters.AddWithValue("@f", bs.List(i)("trancost"))
.Parameters.AddWithValue("@g", txt6.Text)
.Parameters.AddWithValue("@h", bs.List(i)("amount"))
.Parameters.AddWithValue("@i", ty.Text)
.Parameters.AddWithValue("@j", tz.Text)
.ExecuteNonQuery()
End With
Next
tr.Commit()
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว", "Database", MessageBoxButtons.OK)
DataGridView1.Rows.Clear()
'หากโปรแกรม Error ให้เเสดงข้อมความของ Debug โดยใช้ Catch . . . as Exception
Catch ex As Exception
tr.Rollback()
MessageBox.Show(ex.Message)
Finally
tr.Dispose()
End Try
End Using
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)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("DataSource: " & DataGridView1.DataSource.GetType.ToString)
Using con As New SqlConnection("data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322")
con.Open()
Dim bs As BindingSource = DataGridView1.DataSource ' จะ Error ถ้าไม่ได้ใช้ BindingSource เป็น DataSource ในตอนแรก
Dim tr As SqlTransaction = con.BeginTransaction
Try
For i As Integer = 0 To DataGridView1.RowCount - 1
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)
With cm
.Parameters.AddWithValue("@a", dtp.Value.ToString("MM/dd/yyyy"))
.Parameters.AddWithValue("@b", cb1.SelectedValue)
.Parameters.AddWithValue("@c", bs.List(i)("proid"))
.Parameters.AddWithValue("@d", bs.List(i)("proname"))
.Parameters.AddWithValue("@e", bs.List(i)("box"))
.Parameters.AddWithValue("@f", bs.List(i)("trancost"))
.Parameters.AddWithValue("@g", txt6.Text)
.Parameters.AddWithValue("@h", bs.List(i)("amount"))
.Parameters.AddWithValue("@i", ty.Text)
.Parameters.AddWithValue("@j", tz.Text)
.ExecuteNonQuery()
End With
Next
tr.Commit()
MessageBox.Show("บันทึกการแก้ไขข้อมูลเรียบร้อยแล้ว", "Database", MessageBoxButtons.OK)
DataGridView1.Rows.Clear()
Catch ex As Exception
tr.Rollback() ' ถ้าจะใช้ SqlTransaction ก็ควรจะมี tr.Rollback() เพื่อให้ทำงานได้เต็มรูปแบบ
MessageBox.Show(ex.Message)
Finally
tr.Dispose
End Try
End Using
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)
Private Sub butre_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butre.Click
If MessageBox.Show("คุณต้องการเรียกข้อมูลใช่หรือไม่?", "Database", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
'****การดึงข้อมูลมาโชว์ใน DataGrid****'
DataGridView1.Show()
lv.Hide()
Using con As New SqlConnection("data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322")
con.Open()
Dim sql As String = "select * from tblsum where keydate ='" & dtp.Value.ToString("MM/dd/yyyy") & "'and tranid ='" & cb1.SelectedValue & "'"
Dim cmd As SqlCommand = New SqlCommand(sql, con)
Dim dr As SqlDataReader = cmd.ExecuteReader
Dim dt = New DataTable
If dr.HasRows Then
Dim r As Integer = 0
While dr.Read
With DataGridView1
r = .RowCount - 1
.Rows.Add()
.Rows(r).Cells(0).Value = dr.Item(2)
.Rows(r).Cells(1).Value = dr.Item(3)
.Rows(r).Cells(2).Value = dr.Item(4)
End With
End While
End If
End Using
End If
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)
f MessageBox.Show("คุณต้องการเรียกข้อมูลใช่หรือไม่?", "Database", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
'****การดึงข้อมูลมาโชว์ใน DataGrid****'
DataGridView1.Show()
lv.Hide()
Using con As New SqlConnection("data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322")
con.Open()
Dim sql As String = "select * from tblsum where keydate ='" & dtp.Value.ToString("MM/dd/yyyy") & "'and tranid ='" & cb1.SelectedValue & "'"
Dim cmd As SqlCommand = New SqlCommand(sql, con)
Dim dr As SqlDataReader = cmd.ExecuteReader
Dim dt = New DataTable
If dr.HasRows Then
Dim r As Integer = 0
While dr.Read
With DataGridView1
r = .RowCount - 1
.Rows.Add()
.Rows(r).Cells(0).Value = dr.Item(2)
.Rows(r).Cells(1).Value = dr.Item(3)
.Rows(r).Cells(2).Value = dr.Item(4)
End With
End While
End If
End Using
End If
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)
If MessageBox.Show("คุณต้องการเรียกข้อมูลใช่หรือไม่?", "Database", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
'****การดึงข้อมูลมาโชว์ใน DataGrid****'
DataGridView1.Show()
lv.Hide()
Using con As New SqlConnection("data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322")
con.Open()
Dim sql As String = "select * from tblsum where keydate ='" & dtp.Value.ToString("MM/dd/yyyy") & "'and tranid ='" & cb1.SelectedValue & "'"
Dim cmd As SqlCommand = New SqlCommand(sql, con)
Dim dr As SqlDataReader = cmd.ExecuteReader
Dim dt = New DataTable
If dr.HasRows Then
Dim r As Integer = 0
While dr.Read
With DataGridView1
r = .RowCount - 1
.Rows.Add()
.Rows(r).Cells(0).Value = dr.Item(2)
.Rows(r).Cells(1).Value = dr.Item(3)
.Rows(r).Cells(2).Value = dr.Item(4)
End With
End While
End If
End Using
End If
DataGridView1.Focus()
จากโค๊ดด้านบนนี้ ผมต้องเขียนยังไงคับ ช่วยที่คับพี่ ตรงนี้ได้ก็เสร็จครับโปรแกรมนี้
|
 |
 |
 |
 |
Date :
2011-06-20 10:54:01 |
By :
crazy-Ugly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โค๊ดบันทึกคับ จะต้องเขียนยังไงคับ ไม่ทราบจิงๆคับ
Code (VB.NET)
Private Sub butsup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butsup.Click
Using con As New SqlConnection("data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322")
con.Open()
'Dim bs As BindingSource = DataGridView1.DataSource 'จะ Error ถ้าไม่ได้ใช้ BindingSource เป็น DataSource ในตอนแรก
Dim tr As SqlTransaction = con.BeginTransaction
'Try
For i As Integer = 0 To DataGridView1.RowCount - 1
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)
With cm
.Parameters.AddWithValue("@a", dtp.Value.ToString("MM/dd/yyyy"))
.Parameters.AddWithValue("@b", cb1.SelectedValue)
.Parameters.AddWithValue("@c", DataGridView1.Rows(i).Cells(0).Value)
.Parameters.AddWithValue("@d", DataGridView1.Rows(i).Cells(1).Value)
.Parameters.AddWithValue("@e", DataGridView1.Rows(i).Cells(2).Value)
.Parameters.AddWithValue("@f", DataGridView1.Rows(i).Cells(3).Value)
.Parameters.AddWithValue("@g", txt6.Text)
.Parameters.AddWithValue("@h", DataGridView1.Rows(i).Cells(4).Value)
.Parameters.AddWithValue("@i", ty.Text)
.Parameters.AddWithValue("@j", tz.Text)
.ExecuteNonQuery()
End With
Next
tr.Commit()
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว", "Database", MessageBoxButtons.OK)
DataGridView1.Rows.Clear()
'' ''หากโปรแกรม Error ให้เเสดงข้อมความของ Debug โดยใช้ Catch . . . as Exception
'Catch ex As Exception
' tr.Rollback()
' MessageBox.Show(ex.Message)
'Finally
' tr.Dispose()
'End Try
End Using
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)
With cm
.Parameters.AddWithValue("@a", dtp.Value.ToString("MM/dd/yyyy"))
.Parameters.AddWithValue("@b", cb1.SelectedValue)
.Parameters.AddWithValue("@c", DataGridView1.Rows(i).Cells(0).Value)
.Parameters.AddWithValue("@d", DataGridView1.Rows(i).Cells(1).Value)
.Parameters.AddWithValue("@e", DataGridView1.Rows(i).Cells(2).Value)
.Parameters.AddWithValue("@f", DataGridView1.Rows(i).Cells(3).Value)
.Parameters.AddWithValue("@g", txt6.Text)
.Parameters.AddWithValue("@h", DataGridView1.Rows(i).Cells(4).Value)
.Parameters.AddWithValue("@i", ty.Text)
.Parameters.AddWithValue("@j", tz.Text)
.ExecuteNonQuery()
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)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Using con As New SqlConnection("data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322")
con.Open()
Dim tr As SqlTransaction = con.BeginTransaction
For i As Integer = 0 To DataGridView1.RowCount - 1
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)
With cm
.Parameters.AddWithValue("@a", dtp.Value.ToString("MM/dd/yyyy"))
.Parameters.AddWithValue("@b", cb1.SelectedValue)
.Parameters.AddWithValue("@c", DataGridView1.Rows(i).Cells(0))
.Parameters.AddWithValue("@d", DataGridView1.Rows(i).Cells(1))
.Parameters.AddWithValue("@e", DataGridView1.Rows(i).Cells(2))
.Parameters.AddWithValue("@f", DataGridView1.Rows(i).Cells(3))
.Parameters.AddWithValue("@g", txt6.Text)
.Parameters.AddWithValue("@h", DataGridView1.Rows(i).Cells(4))
.Parameters.AddWithValue("@i", ty.Text)
.Parameters.AddWithValue("@j", tz.Text)
.ExecuteNonQuery()
End With
Next
tr.Commit()
MessageBox.Show("บันทึกการแก้ไขข้อมูลเรียบร้อยแล้ว", "Database", MessageBoxButtons.OK)
DataGridView1.Rows.Clear()
End Using
End Sub
|
 |
 |
 |
 |
Date :
2011-06-22 08:39:24 |
By :
watcharop |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 48 เขียนโดย : watcharop เมื่อวันที่ 2011-06-22 08:39:24
รายละเอียดของการตอบ ::
ตามที่ท่านหางอึ่งกล่าวแหละครับ วันที่นี้ตัวปราบเซียนเลย ทดลองประมาณนี้ครับ
Code (VB.NET)
With cm
.Parameters.AddWithValue("@a", "'2011-06-22 00:00:00'")
.Parameters.AddWithValue("@b", "B")
.Parameters.AddWithValue("@c", "B")
.Parameters.AddWithValue("@d", "B")
.Parameters.AddWithValue("@e", "B")
.Parameters.AddWithValue("@f", "B")
.Parameters.AddWithValue("@g", "B")
.Parameters.AddWithValue("@h", "B")
.Parameters.AddWithValue("@i", "B")
.Parameters.AddWithValue("@j", "B")
.ExecuteNonQuery()
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)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Using con As New SqlConnection("data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322")
con.Open()
Dim tr As SqlTransaction = con.BeginTransaction
' ส่วนของการสร้าง Parameters ไว้ก่อน For Loop
Dim cm As New SqlCommand("insert into tblbbk1 (keydate,tranid,proid,bill,typeid,zone) values (@a,@b,@proid,@g,@i,@j)", con, tr)
With cm
.Parameters.AddWithValue("@a", dtp.Value.ToString("MM/dd/yyyy"))
.Parameters.AddWithValue("@b", cb1.SelectedValue)
.Parameters.Add("@proid", SqlDbType.NVarChar,2) ' แนะนำให้ Add Parameters ด้วยวิธีนี้ การ AddWithValue อาจทำให้ระบบตีความหมายของชนิดตัวแปรไม่ตรงกับที่ต้องการ
.Parameters.AddWithValue("@g", txt6.Text)
.Parameters.AddWithValue("@i", ty.Text)
.Parameters.AddWithValue("@j", tz.Text)
End With
For i As Integer = 0 To DataGridView1.RowCount - 1
If Not DataGridView1.Rows(i).IsNewRow Then
' Parameters ใดที่ไม่มีการแก้ไขข้อมูล ก็ไม่จำเป็นต้องใส่ใน For Loop
cm.Parameters("@proid").Value = DataGridView1.Rows(i).Cells(0).Value
cm.ExecuteNonQuery()
End If
Next
tr.Commit()
MessageBox.Show("บันทึกการแก้ไขข้อมูลเรียบร้อยแล้ว", "Database", MessageBoxButtons.OK)
DataGridView1.Rows.Clear()
End Using
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)
Private Sub butsup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butsup.Click
Using con As New SqlConnection("data source=tiger;initial catalog=transport;uid=sa;pwd=47222420322")
con.Open()
Dim tr As SqlTransaction = con.BeginTransaction
' ส่วนของการสร้าง Parameters ไว้ก่อน For Loop
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)
With cm
.Parameters.AddWithValue("@a", dtp.Value.ToString("MM/dd/yyyy"))
.Parameters.AddWithValue("@b", cb1.SelectedValue)
.Parameters.Add("@c", SqlDbType.NVarChar, 2) ' แนะนำให้ Add Parameters ด้วยวิธีนี้ การ AddWithValue อาจทำให้ระบบตีความหมายของชนิดตัวแปรไม่ตรงกับที่ต้องการ
.Parameters.Add("@d", SqlDbType.NVarChar, 50)
.Parameters.Add("@e", SqlDbType.Int)
.Parameters.Add("@f", SqlDbType.NVarChar, 20)
.Parameters.AddWithValue("@g", txt6.Text)
.Parameters.Add("@h", SqlDbType.Float)
.Parameters.AddWithValue("@i", ty.Text)
.Parameters.AddWithValue("@j", tz.Text)
End With
Try
For i As Integer = 0 To DataGridView1.RowCount - 1
If Not DataGridView1.Rows(i).IsNewRow Then
' Parameters ใดที่ไม่มีการแก้ไขข้อมูล ก็ไม่จำเป็นต้องใส่ใน For Loop
cm.Parameters("@c").Value = DataGridView1.Rows(i).Cells(0).Value
cm.Parameters("@d").Value = DataGridView1.Rows(i).Cells(1).Value
cm.Parameters("@e").Value = DataGridView1.Rows(i).Cells(2).Value
cm.Parameters("@f").Value = DataGridView1.Rows(i).Cells(3).Value
cm.Parameters("@h").Value = DataGridView1.Rows(i).Cells(4).Value
cm.ExecuteNonQuery()
End If
Next
tr.Commit()
If MessageBox.Show("คุณต้องการบันทึกข้อมูลใช่หรือไม่?", "Database", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
MessageBox.Show("บันทึกข้อมูลเรียบร้อยเเล้ว", "Database", MessageBoxButtons.OK, MessageBoxIcon.Information)
DataGridView1.Rows.Clear()
DataGridView1.Hide()
lv.Show()
butsup.Enabled = False
butre.Enabled = False
End If
Catch ex As Exception
tr.Rollback() ' ถ้าจะใช้ SqlTransaction ก็ควรจะมี tr.Rollback() เพื่อให้ทำงานได้เต็มรูปแบบ
MessageBox.Show(ex.Message)
Finally
tr.Dispose()
End Try
End Using
butre.Enabled = True
butadd.Enabled = True
butprint.Enabled = True
buts.Enabled = True
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 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|