Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to add currently shows datagridview data in another table. If i add the data, database should took the last data in datagridview. Please guys, need your assistance, Guide me.

What I have tried:

VB
Try
                    con = New SqlConnection(cs)
                    con.Open()

                    Dim sqlInsertT2 As String = ""
                    For i As Integer = 0 To DataGridView1.RowCount - 1
                        sqlInsertT2 &= " Insert Into Techsp(Jobno,Qty,Jobid,Item,AU,ItemQty,Category,TotalQty) Values ('" & TextBox1.Text & "','" & TextBox4.Text & "','" & TextBox6.Text & "','" & DataGridView1.Rows(i).Cells("Items").Value & "','" & DataGridView1.Rows(i).Cells("A/U").Value & "','" & DataGridView1.Rows(i).Cells("Item Qty").Value & "','" & DataGridView1.Rows(i).Cells("Category").Value & "',@TotalQty);"
                        cmd = New SqlCommand(sqlInsertT2)
                        cmd.Connection = con
                        Dim iresult As Integer
                        iresult = Val(TextBox4.Text) * Val(DataGridView1.Rows(i).Cells("Item Qty").Value)
                        cmd.Parameters.AddWithValue("Jobno", TextBox1.Text)
                        cmd.Parameters.AddWithValue("Qty", TextBox4.Text)
                        cmd.Parameters.AddWithValue("Jobid", Textbox6.Text)
                        cmd.Parameters.AddWithValue("Item", DataGridView1.Rows(i).Cells("Items").Value)
                        cmd.Parameters.AddWithValue("AU", DataGridView1.Rows(i).Cells("A/U").Value)
                        cmd.Parameters.AddWithValue("ItemQty", DataGridView1.Rows(i).Cells("Item Qty").Value)
                        cmd.Parameters.AddWithValue("Category", DataGridView1.Rows(i).Cells("Category").Value)
                        cmd.Parameters.AddWithValue("@TotalQty", iresult)
                    Next
                Catch ex As Exception
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Try
                cmd.ExecuteReader()
Posted
Updated 5-Apr-20 15:01pm
v2

1 solution

Try this:

Dim NewTable As Datatable = DataGridView1.DataSource.Copy
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900