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

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