0

i am working on vb.net windows form application ,,i have a data grid

enter image description here

I have two tables 1->CompanyMaster_tbl in this having Two fields . Cid and CompanyName,

Cid is the primary key of this table

2->DepartmentMaster_tbl in this having 4 fields. dtid,dtname,dtphon,dtmail,Cid.

dtid is the primary key,and Cid is the foreign key

while clicking save button i want save the data in both table. in one company i want to save multiple departments. my Cid and dtid are autoincriment,,i mean Identity specication i set as true..in save button i given code like this,,

 Dim sqlInsertT1 As String = ""
 Dim sqlInsertT2 As String = ""
 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

but this logic is not enough for saving this data,,
in save button if i am editing something in grid view ,,i want to update that value in particular table
in the below line i am getting error like this:Object reference not set to an instance of an object.

For Each CompanyMaster_row As DataRow In CompanyMaster_tbl.Rows

so how i can resolve this issue? i try after filling my company master table,,but then also i am getting same error

4
  • Initially i would suggest you to use transaction. After insert you can select scope_identity in order to get the just inserted id. Commented Dec 31, 2013 at 13:46
  • What's the relationship between the datatables and the datagridview? Are they bound? If not, how is the datagridview populated? Commented Dec 31, 2013 at 13:57
  • in load event i written some coding for populating the data grid view,, Commented Dec 31, 2013 at 14:05
  • i am taking each value then i am passing to datagridview, like this: Dim row0 As String() = {cmpname, dtname, dtphon, dtmail} gv.Rows.Add(row0) Commented Dec 31, 2013 at 14:07

1 Answer 1

2

In your posted code, you have assigned a value of Nothing to CompanyMaster_tbl just before you try to access it. That's why you're getting the exception.

Dim CompanyMaster_tbl As DataTable = Nothing

For Each CompanyMaster_row As DataRow In CompanyMaster_tbl.Rows

The exception is thrown when you try to access the Rows property of the null object.

Sign up to request clarification or add additional context in comments.

3 Comments

instead of nothing i given my table name ..then also i am getting error
fill/get/populate CompanyMaster_tbl before you try to iterate it
i filled CompanyMaster_tbl manually,,then i try to iterate ..but getting same error

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.