hello everyone,
i have a datagridview filled with a dataset and i want to store all the records it has to new table in database. i added a new column,("pos") to a dataset that fills the datagridview through a query so that it ranks records based on a field (total).my problem is how to loop through all records to store them coz the error i get is (Object reference not set to an instance of an object ).can someone help me pliz, av tried a couple of weeks with no success,,, or is there a better way i could do this.
here is my code

 Dim comm As SqlCommand = New SqlCommand()
        Dim conn As SqlConnection = New SqlConnection
        conn = New SqlConnection(cs1)
        comm.Connection = conn
        conn.Open()

        Dim sname, adm, class1, yr, term, examType, eng, kisw, math, bio, chem, phy, hist, cre, geo, agr, biz, total, pos, grade As String
        For i As Integer = 0 To DataGridView1.Rows.Count - 1

            sname = Me.DataGridView1.Rows(i).Cells("sname").Value.ToString()
            adm = Me.DataGridView1.Rows(i).Cells("adm").Value.ToString()
            class1 = Me.DataGridView1.Rows(i).Cells("class").Value.ToString
            examType = Me.DataGridView1.Rows(i).Cells("examType").Value.ToString
            yr = Me.DataGridView1.Rows(i).Cells("yr").Value.ToString
            math = Me.DataGridView1.Rows(i).Cells("math").Value.ToString
            eng = Me.DataGridView1.Rows(i).Cells("eng").Value.ToString
            kisw = Me.DataGridView1.Rows(i).Cells("kisw").Value.ToString
            bio = Me.DataGridView1.Rows(i).Cells("bio").Value.ToString
            phy = Me.DataGridView1.Rows(i).Cells("phy").Value.ToString
            chem = Me.DataGridView1.Rows(i).Cells("chem").Value.ToString()
            hist = Me.DataGridView1.Rows(i).Cells("hist").Value.ToString()
            cre = Me.DataGridView1.Rows(i).Cells("cre").Value.ToString()
            geo = Me.DataGridView1.Rows(i).Cells("geo").Value.ToString()
            biz = Me.DataGridView1.Rows(i).Cells("biz").Value.ToString()
            total = Me.DataGridView1.Rows(i).Cells("total").Value.ToString()
            agr = Me.DataGridView1.Rows(i).Cells("agr").Value.ToString()
            term = Me.DataGridView1.Rows(i).Cells("term").Value.ToString()
            pos = Me.DataGridView1.Rows(i).Cells("pos").Value.ToString()
            grade = Me.DataGridView1.Rows(i).Cells("grade").Value.ToString()

            comm.CommandText = "insert into tblExamResults(sname,adm,class,examType,yr,math,eng,kisw,bio,chem,phy,hist,cre,geo,biz,total,agr,term,pos,grade) values('" & sname & "','" & adm & "','" & class1 & "','" & examType & "','" & yr & "','" & math & "','" & eng & "','" & kisw & "','" & bio & "','" & chem & "','" & phy & "','" & hist & "','" & cre & "','" & geo & "','" & biz & "','" & total & "','" & agr & "','" & term & "','" & pos & "','" & grade & "')"

            comm.ExecuteNonQuery()
        Next

        con.Close()

It is most likely "ToString".

Delete ".ToString()" and ".ToString" from each line.

example:

Change from:

grade = Me.DataGridView1.Rows(i).Cells("grade").Value.ToString()

To:

grade = Me.DataGridView1.Rows(i).Cells("grade").Value

I'm not sure what your columns represent, but it appears to not be normalized. To learn more about dateabase normalization, search for "database normalization".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.