Hi everyone,
as my title says.. Im trying to add the entire data of the gridview to a SQLdatabase.
what i'm doing is looping through the DGView and adding it 1 at a time in a db.. Im not sure if this is the elegant way, but my knowledge in vb is just at noob level
my code so far.
For the life of me, I cannot figure out why i'm getting a Object reference not set to an instance of an object.
Please help. I wasted hours trying to get pass this wall
as my title says.. Im trying to add the entire data of the gridview to a SQLdatabase.
what i'm doing is looping through the DGView and adding it 1 at a time in a db.. Im not sure if this is the elegant way, but my knowledge in vb is just at noob level
my code so far.
VB.NET:
Private Sub btnLoadData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadData.Click
Dim ConnectionString As String = _
"DataSource=.\SQLEXPRESS;AttachDbFilename=C:\EmoGirlSexyMe\2012\Programming\CSVtoDB\db1.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
Dim i As Integer = 0
Dim str, Name, Items, SerialNumber, ContactDetails As String
Dim strInsert As String = _
"Insert Into csvTable (Name,Items,SerialNumber,ContactDetails)" & _
" values (@CN,@CD,@PN,@D1)"
Dim con1 As New SqlConnection(ConnectionString)
Dim cmdInsert As New SqlCommand
con1.Open()
For i = 0 To DataGridView1.RowCount - 1
Name = DataGridView1.Rows(i).Cells(0).Value.ToString()
Items = DataGridView1.Rows(i).Cells(1).Value.ToString()
SerialNumber = DataGridView1.Rows(i).Cells(2).Value.ToString()
ContactDetails = DataGridView1.Rows(i).Cells(3).Value.ToString()
cmdInsert = New SqlCommand(strInsert, con1)
cmdInsert.Parameters.AddWithValue("@CN", Name)
cmdInsert.Parameters.AddWithValue("@CD", Items)
cmdInsert.Parameters.AddWithValue("@PN", SerialNumber)
cmdInsert.Parameters.AddWithValue("@D1", ContactDetails)
cmdInsert.ExecuteNonQuery()
Next
con1.Close()
End Sub
For the life of me, I cannot figure out why i'm getting a Object reference not set to an instance of an object.
Please help. I wasted hours trying to get pass this wall