Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have a datagrid connected to an SQL table, how to programmatically with a button can insert specific cell values to another SQL database table?
here is vb net code gives an error "column0 has been declared"

VB
Dim ConnString As String = "Data Source=SUPERVISOR-PC;Initial Catalog=library;Integrated Security=True"
      Dim i As New Integer

      For i = 0 To DataGridView1.RowCount - 1

          Dim sql As String = "" & "INSERT INTO [atzenta].dbo.phones (" & "name, mobile, telephone" & ") VALUES (" & "@Column" & (i) & ", @Column" & (i) & ", @Column" & (i) & " )"

          Using cn As New SqlConnection(ConnString), cmd As New SqlCommand(sql, cn)

              cmd.Parameters.Add("@column" & (i), SqlDbType.VarChar, 50).Value = DataGridView1.Rows(i).Cells(1).Value
              cmd.Parameters.Add("@column" & (i), SqlDbType.VarChar, 50).Value = DataGridView1.Rows(i).Cells(4).Value
              cmd.Parameters.Add("@column" & (i), SqlDbType.VarChar, 50).Value = DataGridView1.Rows(i).Cells(5).Value

              cn.Open()
              cmd.ExecuteNonQuery()
              cn.Close()
          End Using


      Next

  End Sub


changed SQL string to this and get cmd.executenonquery "incorect syntax Near Trans"
VB
Dim sql1 As String = "" & "INSERT INTO [atzenta].dbo.phones (" & "name, mobile, telephone" & ") VALUES (" & DataGridView1.Rows(i).Cells(1).Value & "," & DataGridView1.Rows(i).Cells(4).Value & ", " & DataGridView1.Rows(i).Cells(5).Value & ")"

and deleted the add parameter section
VB
Dim ConnString As String = "Data Source=SUPERVISOR-PC;Initial Catalog=library;Integrated Security=True"
       Dim i As New Integer

       For i = 0 To DataGridView1.RowCount - 1

           Dim sql1 As String = "" & "INSERT INTO [atzenta].dbo.phones (" & "name, mobile, telephone" & ") VALUES (" & DataGridView1.Rows(i).Cells(1).Value & "," & DataGridView1.Rows(i).Cells(4).Value & ", " & DataGridView1.Rows(i).Cells(5).Value & ")"

           Using cn As New SqlConnection(ConnString), cmd As New SqlCommand(sql1, cn)
               cn.Open()
               cmd.ExecuteNonQuery()
               cn.Close()
           End Using

Any correct SQL string syntax for this?
Posted
Updated 14-Jun-15 23:59pm
v2

1 solution

All of these parameter declarations are within the same loop.

Let me show you the values in the first loop:

VB
'First loop so i = 0
 
'This is just a string at this stage
Dim sql As String = "" & "INSERT INTO [atzenta].dbo.phones (" & "name, mobile, telephone" & ") VALUES (" & "@Column0, @Column0, @Column0 )"
 
Using cn As New SqlConnection(ConnString), cmd As New SqlCommand(sql, cn)
 
'Paremeter @column0  added
cmd.Parameters.Add("@column0", SqlDbType.VarChar, 50).Value = DataGridView1.Rows(i).Cells(1).Value
'Paremeter @column0 added - oh wait!  it has already been added - ERROR!
cmd.Parameters.Add("@column0", SqlDbType.VarChar, 50).Value = DataGridView1.Rows(i).Cells(4).Value
 cmd.Parameters.Add("@column0", SqlDbType.VarChar, 50).Value = DataGridView1.Rows(i).Cells(5).Value
 
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
End Using



I hope that helps clarify the issue.

Let me know if you have any further problems ^_^
 
Share this answer
 
v2
Comments
Member 3892343 21-Jun-15 11:00am    
DataGridView1.Rows(i).Cells(1).Value
'Paremeter @column0 added - oh wait! it has already been added - ERROR!
done this:
cmd.Parameters.Add(DataGridView1.Rows(i)+1, SqlDbType.VarChar, 50).Value = DataGridView1.Rows(i).Cells(4).Value
cmd.Parameters.Add(DataGridView1.Rows(i)+2, SqlDbType.VarChar, 50).Value = DataGridView1.Rows(i).Cells(5).Value

Thank you
Andy Lanng 21-Jun-15 11:18am    
You could do that but it's pointless.

Just name your parameters. You can use @Column1 - @Column3 or @a, @b & @c, or meaningful names like @id, @parentid, @value.

The parameters in your code stay constant so there is no need to give them names associated to the loop counter.

It will work, but other developers would look at you funny :P

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