Insertar Datos a Datagridview
Hola.. Me gustaría saber si me puedes ayudar con esto.. Tengo un datagridview (no enlazado a ningún Dataset) al cual le agrego información.. Y necesito que al darle click en un botón (guardar) me guarde esos datos en una tabla en Sql.. Me podrías decir como puedo hacer esto.. Tengo algo de código.. Pero me muestra error:
Dim conx As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection("Data Source=11.11.11.11;Initial Catalog=BD240;Persist Security Info=True;User ID=sitauser;Password=kkkk")
conx.Open()
Dim comsql As SqlClient.SqlCommand
'Dim i As Integer = 0
comsql = conx.CreateCommand()
For i As Integer = 0 To DataGridView2.Rows.Count - 1
comsql = "INSERT INTO TABCOMP (NUM_REF,FEC_CREA,COMP,MONTO,AUTORIZA) VALUES ('" + DataGridView2.Rows(i).Cells(0).Value.ToString() + "','" + DataGridView2.Rows(i).Cells(1).Value.ToString() + "','" + DataGridView2.Rows(i).Cells(2).Value.ToString() + "','" + DataGridView2.Rows(i).Cells(3).Value.ToString() + "','" + DataGridView2.Rows(i).Cells(4).Value.ToString() + "')"
comsql.ExecuteNonQuery()
Next
conx.Close()
El error me lo muestra en donde esta en negrita y me dice es: Value of type 'String' cannot be converted to 'System. Data.SqlClient.SqlCommand'.
Tal vez sesa algo simple.. Jeje.. Pero de antemano agradezco que me puedas orientar.. Saludos..
Dim conx As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection("Data Source=11.11.11.11;Initial Catalog=BD240;Persist Security Info=True;User ID=sitauser;Password=kkkk")
conx.Open()
Dim comsql As SqlClient.SqlCommand
'Dim i As Integer = 0
comsql = conx.CreateCommand()
For i As Integer = 0 To DataGridView2.Rows.Count - 1
comsql = "INSERT INTO TABCOMP (NUM_REF,FEC_CREA,COMP,MONTO,AUTORIZA) VALUES ('" + DataGridView2.Rows(i).Cells(0).Value.ToString() + "','" + DataGridView2.Rows(i).Cells(1).Value.ToString() + "','" + DataGridView2.Rows(i).Cells(2).Value.ToString() + "','" + DataGridView2.Rows(i).Cells(3).Value.ToString() + "','" + DataGridView2.Rows(i).Cells(4).Value.ToString() + "')"
comsql.ExecuteNonQuery()
Next
conx.Close()
El error me lo muestra en donde esta en negrita y me dice es: Value of type 'String' cannot be converted to 'System. Data.SqlClient.SqlCommand'.
Tal vez sesa algo simple.. Jeje.. Pero de antemano agradezco que me puedas orientar.. Saludos..
1 Respuesta
Respuesta de Roberto Alvarado