Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
NullReferenceException Was Unhandled Error on line with code " newRow = dt.NewRow() " below
VB
Try
    If State = gModule.FormState.adStateAddMode Then
      'add a row
        Dim newRow As DataRow
        newRow = dt.NewRow()
        newRow("company") = txtbname.Text
        newRow("bid") = txtbid.Text
        newRow("biztype") = IIf(txtOccupation.Text = "", System.DBNull.Value, txtOccupation.Text)



Update 1:

VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim dt As DataTable = cnGetData.Tables("tbl_permits")
    If txtbname.Text = "" Or txtbname.Text = "" Then
        MsgBox("Please fill up Company Name Information.", MsgBoxStyle.Critical)
        Me.txtbname.Focus()
        Exit Sub
    End If
    Try
    If State = gModule.FormState.adStateAddMode Then
        ' add a row
        Dim nRow As DataRow nRow = dt.NewRow()



and I have also set the: Dim cnGetData As New DataSet()
Posted
Updated 22-May-11 19:38pm
v3

the datatable "dt" is not initialized. i.e. it's nothing
check where the datatable is being created and initialized properly.

Thanks,
Hemant

Edit:
Now check your dataset where are you filling values in your dataset. you've created the dataset
Dim cnGetData As New DataSet()


but are you filling it with proper data and is it being filled with the table tbl_permits. The top most source of data here is Dataset and then there are tables if you've not filled the dataset properly.

Debug your code and watch if you have any table in cnGetData dataset

--Hemant
 
Share this answer
 
v2
Comments
Venkatesh Mookkan 23-May-11 1:18am    
Good call!
joseph k 23-May-11 1:34am    
Ok, Thanks for your response: check the code below as i have initialised dt to get data from a table:
<pre lang="vb">
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dt As DataTable = cnGetData.Tables("tbl_permits")

If txtbname.Text = "" Or txtbname.Text = "" Then
MsgBox("Please fill up Company Name Information.", MsgBoxStyle.Critical)
Me.txtbname.Focus()

Exit Sub
End If

Try
If State = gModule.FormState.adStateAddMode Then
' add a row
Dim nRow As DataRow

nRow = dt.NewRow()</pre>

and I have also set the: Dim cnGetData As New DataSet()
Hemant__Sharma 23-May-11 1:48am    
Check the updated solution
Hemant__Sharma 23-May-11 1:48am    
Thanks @Venkatesh.
In the above case cnGetData.Tables("tbl_permits") is null.

Place a breakpoint on that line and check it cnGetData.Tables("tbl_permits") has any value in it.
 
Share this answer
 
Comments
joseph k 23-May-11 2:20am    
Thanks, this was a very good lead to my problem, i solved it.
Venkatesh Mookkan 23-May-11 2:23am    
You are welcome.
Thanks very much for your lead i got the problem, Actually i had not connected to the MySQL DB, i got it working.
 
Share this answer
 
Comments
Hemant__Sharma 23-May-11 2:37am    
Welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900