0

i am working on vb.net windows form ,I have a data grid view,,i am trying to save data grid values

I am saving data to Two Tables..my Table Names:

1->CompanyMaster_tbl
2->DepartmentMaster_tbl

I given code in save button like this:

Dim CompanyMaster_tbl As DataTable = Nothing
        Dim DepartmentMaster_tbl As DataTable = Nothing

        For Each CompanyMaster_row As DataRow In CompanyMaster_tbl.Rows
            For i As Integer = 0 To gv.RowCount - 2

                If gv.Rows(i).Cells("cmpny").Value <> "" Then
                    sqlInsertT1 &= "Insert Into CompanyMaster_tbl(CompanyName) Values ('" & gv.Rows(i).Cells("cmpny").Value & "');"
                    Exetransaction(sqlInsertT1)
                End If
                Ccid = RecordID("Cid", "CompanyMaster_tbl", "CompanyName", gv.Rows(i).Cells("cmpny").Value)
            Next

            For Each DepartmentMaster_row As DataRow In DepartmentMaster_tbl.Select(Ccid)
                For j As Integer = 0 To gv.RowCount - 2
                    sqlInsertT2 &= "Insert Into DepartmentMaster_tbl(dtname,dtphone,dtEmail,Cid) Values ('" & gv.Rows(j).Cells("Dpmnt").Value & "','" & gv.Rows(j).Cells("dtphon").Value & "','" & gv.Rows(j).Cells("mail").Value & "'," & Ccid & ");"
                    Exetransaction(sqlInsertT2)
                Next
            Next
        Next

while coming to this line am getting error:

For Each CompanyMaster_row As DataRow In CompanyMaster_tbl.Rows

Object reference not set to an instance of an object why i am getting this error? what is wrong with my code

7
  • Dim CompanyMaster_tbl = Nothing is the problem... Commented Jan 2, 2014 at 13:18
  • instead of that what i have to give? Commented Jan 2, 2014 at 13:18
  • in such case your CompanyMaster_tbl is null and that's why when you do .Rows it's throwing null ref exception. You might want to check CompanyMaster_tbl Commented Jan 2, 2014 at 13:23
  • what i want to check in CompanyMaster_tbl Commented Jan 2, 2014 at 13:25
  • Well given what you are doing I'd have expected your table to be pointing at a table in a data set pointed at a connection. Certainly making it Nothing and then trying to use it isn't going to work. Commented Jan 2, 2014 at 13:29

1 Answer 1

1

I don't work in VB.NET (rather in C#) but your code make no sense.

You saying ...

Dim CompanyMaster_tbl 
As DataTable = Nothing <-- (here CompanyMaster_tbl set to null)


        For Each CompanyMaster_row As DataRow 
       In CompanyMaster_tbl.Rows <-- you are trying to referencing it.

So, it will definitely throw you null reference exception.

Instead of saying ...

For Each CompanyMaster_row As DataRow In CompanyMaster_tbl.Rows

Why not loop through rows in your gridview and then populate the datatable

For Each DataGridViewRow gr In gv.Rows

      If gr.Cells("cmpny").Value <> "" Then
            sqlInsertT1 &= "Insert Into CompanyMaster_tbl(CompanyName) 
            Values ('" & gr.Cells("cmpny").Value & "');"
            Exetransaction(sqlInsertT1)
        End If
        Ccid = RecordID("Cid", "CompanyMaster_tbl", "CompanyName", 
        gr.Cells("cmpny").Value)
    Next
Sign up to request clarification or add additional context in comments.

6 Comments

it is oke sir,,,so how i can save data from gridview to mutilpe tables
The answer already answers this question. Loop through the GridView and insert rows from there, not an uninstantiated DataTable. Further than that, I think you may benefit to understand the differences between your source and destination. Go back to basics a little more.
@user3106114, as said in my answer, loop through the grid and then just follow your code .. the same way you were inserting data before. see my edit.
sir but this line showing error "For Each gridviewrow gr In gv.Rows" syntax error
it's DataGridViewRow and not gridviewrow. so change that line to For Each DataGridViewRow gr In gv.Rows. Please help yourself with further syntax errors.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.