Click here to Skip to main content
15,913,773 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is wrong with this code. I am trying to loop through all datagridview rows and save all rows to table.

VB
Dim con As New SqlConnection
        Dim cmd As New SqlCommand
        Dim val1 = TextBox1.Text
        Dim val2 = TextBox2.Text
        con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
        con.Open()
        cmd.Connection = con

        For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
            cmd.CommandText = "Insert into table2(aa,bb) Values(@firstname,@lastname)"
            cmd.Parameters.AddWithValue("@firstname", DataGridView1.Rows(i).Cells(0).Value)
            cmd.Parameters.AddWithValue("@lastname", DataGridView1.Rows(i).Cells(1).Value)
            cmd.ExecuteNonQuery()
        Next
Posted

1 solution

try this code it will work for you I guess


VB
Dim con As New SqlConnection
        
        Dim val1 = TextBox1.Text
        Dim val2 = TextBox2.Text
        con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
        con.Open()
        

        For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
          Dim cmd As New SqlCommand()
cmd.Connection = con
            cmd.CommandText = "Insert into table2(aa,bb) Values(@firstname,@lastname)"
            cmd.Parameters.AddWithValue("@firstname", DataGridView1.Rows(i).Cells(0).Value)
            cmd.Parameters.AddWithValue("@lastname", DataGridView1.Rows(i).Cells(1).Value)
            cmd.ExecuteNonQuery()
        Next


Hope this helps....

Best of Luck.............
 
Share this answer
 
Comments
wiswalld 28-Apr-12 10:39am    
I get the error

The variable name '@firstname' has already been declared. Variable names must be unique within a query batch or stored procedure.
wiswalld 28-Apr-12 10:48am    
Sorry the error is

The parameterized query '(@firstname nvarchar(4000),@lastname nvarchar(4000))Insert into ' expects the parameter '@firstname', which was not supplied.
Mantu Singh 28-Apr-12 10:55am    
Is you data grid empty....?
if not you are passing null value to parameter
I noted a line above in u r ques

try this

cmd.CommandText = "Insert into table2(aa,bb) Values(@firstname,@lastname)"
cmd.Parameters.AddWithValue("@firstname",val1)
cmd.Parameters.AddWithValue("@lastname",val2)
wiswalld 28-Apr-12 13:50pm    
There are values

I use this to populate a datagridview from an excel sheet
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim DtSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\Temp\Sex.xls';Extended Properties=Excel 8.0;")
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
MyCommand.TableMappings.Add("Table", "Net-informations.com")
DtSet = New System.Data.DataSet
MyCommand.Fill(DtSet)
DataGridView1.DataSource = DtSet.Tables(0)
MyConnection.Close()

Then I am trying to save to sql table using

Dim con As New SqlConnection
con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
con.Open()


For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
Dim cmd As New SqlCommand()
cmd.Connection = con
cmd.CommandText = "Insert into table2(aa,bb) Values(@firstname,@lastname)"
cmd.Parameters.AddWithValue("@firstname", DataGridView1.Rows(i).Cells(0).Value)
cmd.Parameters.AddWithValue("@lastname", DataGridView1.Rows(i).Cells(1).Value)
cmd.ExecuteNonQuery()
Next
[no name] 2-Jun-15 1:11am    
Are you using a local database?

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