Object reference not set to an instance of an object

emaduddeen

Well-known member
Joined
May 5, 2010
Messages
171
Location
Lowell, MA & Occasionally Indonesia
Programming Experience
Beginner
Hi Everyone,

Can you help me get past this error?

I get the error with this code. It doesn't like objDataRowSingleParent :
VB.NET:
        ' Set up a new blank data row if the user chose to Insert.
        '---------------------------------------------------------
        If booleanAddNew Then
            objDataRowSingleParent = objDataSetParentDetails.Tables("Parent Details").NewRow()
        End If

In the Public Class section of the form I have this:
VB.NET:
Dim objDataRowSingleParent As DataRow

I thought this would be enough to do it but I'm not sure what else I'm missing.

Thanks.

Truly,
Emad
 
Learn to use the debugger: Place breakpoints in your code; when you debug, the code execution will be stopped at the places you created the breakpoints and you can mousover the different variables to see their values. You should be able to spot the variable that has not been instantiated.
To place breakpoints, simply click in the left margin of the line of code you want to stop execution at.
 
Hi Paszt,

I did the debugger and it shows objDataRowSingleParent is = to "nothing" but I did a "Dim" statement in the "Public Class". I think it's just a simple step I'm missing. Could you tell me what else other than the "Dim" statement is needed?

Thanks.

Truly,
Emad
 
check if objDataSetParentDetails isnot nothing then if it's not then check if objDataSetParentDetails.Tables("Parent Details") is not nothing
 
you are looking at the type not at the data, put a break point into that code and once you reach it open the imemediate window (by running the bebugger then going to debug > window > immediate window) then write in the immediate window:
? if objDataSetParentDetails is nothing , if it results in false then write if objDataSetParentDetails.Tables("Parent Details") is nothing and what it results

if any of them results in false then you need to make sure they reference some data
 
Assalam alaikum wr. wb. ahmad,

There will not be any data referenced at that point because I am trying to create a new blank datarow so it can be added later.

Here is the code for the entire sub routine so you know what I'm trying to do.

Maybe there is a simpler way to get a new row of data into the dataset so it can be added to the database?

VB.NET:
    Public Sub SaveToDatabase()

        ' Set up a new blank data row if the user chose to Insert.
        '---------------------------------------------------------
        If booleanAddNew Then
            objDataRowSingleParent = objDataSetParentDetails.Tables("Parent Details").NewRow()
        End If

        ' Start the editing in the datarow.
        '----------------------------------
        objDataRowSingleParent.BeginEdit()

        ' Fill the data row with values from TextBoxes in the details group.
        '-------------------------------------------------------------------
        For Each objSingleControl In GroupBoxParentDetailsInfo.Controls

            If TypeOf objSingleControl Is EditBox Then

                ' Each text box control starts with "EditBox" followed by the column name.
                ' This line will get the column name from the text box so data can be taken
                ' from the data row.
                '--------------------------------------------------------------------------
                strName = Mid(objSingleControl.Name, 8)

                Try
                    ' Grab a textbox's data and place it into a DataRow column.
                    '----------------------------------------------------------
                    objDataRowSingleParent(strName) = objSingleControl.text

                Catch exException As Exception
                    MessageBox.Show("Data row column not the same as control name: " & _
                                    objSingleControl.Name)
                End Try

            End If
        Next

        ' Fill the data row with values from Payment Plan RadioButtons.
        '--------------------------------------------------------------

        If RadioButtonPlanA.Checked = True Then
            objDataRowSingleParent("PaymentPlan") = "A"
        End If

        If RadioButtonPlanB.Checked = True Then
            objDataRowSingleParent("PaymentPlan") = "B"
        End If

        If RadioButtonPlanC.Checked = True Then
            objDataRowSingleParent("PaymentPlan") = "C"
        End If

        If RadioButtonPlanD.Checked = True Then
            objDataRowSingleParent("PaymentPlan") = "D"
        End If

        ' Finish the editing of the data row.
        '------------------------------------
        objDataRowSingleParent.EndEdit()

        Try

            ' Add a new blank row into the Data Set if the user chose to Insert.
            '-------------------------------------------------------------------
            If booleanAddNew Then
                objDataSetParentDetails.Tables("Parent Details").Rows.Add(objDataRowSingleParent)
            End If

            ' Create an instance of the command builder.
            '-------------------------------------------
            objCommandBuilderParentDetails = New SqlCommandBuilder(objDataAdapterParentDetails)

            If booleanAddNew Then

                ' Have the command builder create an Insert SQL command.
                '-------------------------------------------------------
                objDataAdapterParentDetails.InsertCommand = objCommandBuilderParentDetails.GetInsertCommand
            Else

                ' Have the command builder create an update SQL command.
                '-------------------------------------------------------
                objDataAdapterParentDetails.UpdateCommand = objCommandBuilderParentDetails.GetUpdateCommand
            End If

            ' Perform the update SQL command; then close the connection.
            '-----------------------------------------------------------
            objDataAdapterParentDetails.Update(objDataSetParentDetails, "Parent Details")
            objDataSetParentDetails.Tables("Parent Details").AcceptChanges()

            If booleanAddNew Then
                ' Re-load the Datset View because there is a new row of data.
                '------------------------------------------------------------
                PopulateDataGridWithParentNames()

                ' Close the connection opened by the insert command.
                '---------------------------------------------------
                objDataAdapterParentDetails.InsertCommand.Connection.Close()
            Else
                ' Close the connection opened by the update command.
                '---------------------------------------------------
                objDataAdapterParentDetails.UpdateCommand.Connection.Close()
            End If

            ' Reset this since it's not needed now.
            '--------------------------------------
            booleanAddNew = False

        Catch saveException As Exception

            Throw saveException ' to whoever called this sub routine.

        End Try

    End Sub

Wasalamu,
Emad
 
You have said it there are no data reference so the objects will have a nothing value which then mean you can't use it to call a method objDataSetParentDetails and objDataSetParentDetails.Tables("Parent Details") must not evaluate to nothing
 
Example:

VB.NET:
Dim ConnectionText As String = "Data Source=MYPC; Initial Catalog=MyDB; Integrated Security=True"
        Dim cmd As New SqlCommand("Select * from MyTable")'Has Column Name
Dim da As New SqlDataAdapter(cmd, ConnectionText )
dim dt as new datatable()
da.fill(dt)
dim dr as datarow=dt.newrow()
dr("Name")=textbox1.text
dr.AcceptChanges()
 
Is this the best way to create the data row object.

Hi,

Can you tell me if this is the best way to create the data row object? The code works but just a little curious.

VB.NET:
        ' Set up a new blank data row if the user chose to Insert.
        '---------------------------------------------------------
        If booleanAddNew Then
            objDataAdapterParentDetails.SelectCommand.Parameters("@ParentID").Value = 0
            objDataAdapterParentDetails.Fill(objDataSetParentDetails, "Parent Details")
            objDataRowParentDetails = objDataSetParentDetails.Tables("Parent Details").NewRow()
        End If
 
Back
Top