I have a text box and a datagridview in a form . I can insert both text box and datagridview content to table . is it possible to insert text box value into equal number of datagridview input ?
Dim thisConnection As New SqlConnection(" ")
'Create Command object
Dim nonqueryCommand As SqlCommand = thisConnection.CreateCommand()
Try
' Open Connection
thisConnection.Open()
Console.WriteLine("Connection Opened")
' Create INSERT statement with named parameters
nonqueryCommand.CommandText = _
"INSERT INTO Table4 (Col1, Col2,col3,col4) VALUES (@Col1, @Col2,@col3,@col4)"
nonqueryCommand.Prepare()
' Data to be inserted
For Each row As DataGridViewRow In DataGridView1.Rows
If Not row.IsNewRow Then
nonqueryCommand.Parameters.Add("@Col1", SqlDbType.DateTime);
nonqueryCommand.Parameters.Add("@Col2", SqlDbType.VarChar, 50).Value = DBNull.Value
nonqueryCommand.Parameters.Add("@Col3", SqlDbType.VarChar, 50).Value = DBNull.Value
nonqueryCommand.Parameters.Add("@Col4", SqlDbType.VarChar, 50).Value = DBNull.Value
nonqueryCommand.Parameters("@Col1").Value = DateTime.Parse(textbox1.text);
nonqueryCommand.Parameters("@Col2").Value = row.Cells(1).Value.ToString()
nonqueryCommand.Parameters("@Col3").Value = row.Cells(2).Value.ToString()
nonqueryCommand.Parameters("@Col4").Value = row.Cells(3).Value.ToString()
nonqueryCommand.ExecuteNonQuery()
End If
Next
Catch ex As SqlException
' Display error
Console.WriteLine("Error: " & ex.ToString())
Finally
' Close Connection
thisConnection.Close()
Console.WriteLine("Connection Closed")
End Try