CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2008
    Posts
    10

    [RESOLVED] Object Not set to Instance of an Object in adding a data row.

    hello everyone,

    in the following code, I am trying to call a sub routine which adds information to a row in a database by passing the values from a class function/Structure.

    However, when I get passed assigning the two ID columns for the pm K and Foreign K the system reads that I do not have an instance set to an object on.

    I have provided example coded below:

    Code:
    Private Sub btnNewFamilySubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewFamilySubmit.Click
    
    
            Call ValidateEmptybox()
    
            CreateFamily = New Family.FamilyInformation  'Class Declaration.
            ValidCreateFamily = New Family.FamilyDetails 'Public Structure declaration.
    
            'Assign the values to the structure FamilyDetails in the Family NameSpace.
    
            CreateFamily.FamilyName = txtFamilyName.Text.ToString
            CreateFamily.HomeAddress = txtFamilyStreetAddress.Text.ToString
            CreateFamily.HomeCity = txtFamilyCity.Text.ToString
            CreateFamily.HomeState = cbFamilyHomeState.Text.ToString
            CreateFamily.PostalCode = txtFamilyPostalCode.Text.ToString
            CreateFamily.HomePhone = txtFamilyHomePhone.Text.ToString
    
            'Assign the function in the Family NameSpace to the FamilyInformation Class.
    
            ValidCreateFamily = CreateFamily.ValidFamilyInformation
    
            
            'Pass the Values back from the Family NameSpace function 
            'after validation and send them to SubmitValidFamily to submit
            'the information to the database.
    
            Call SubmitValidFamily(ValidCreateFamily)
    
    
    
    
        End Sub
    
        Private Sub SubmitValidFamily(ByVal ValidFamilyDetail As Family.FamilyDetails)
    
    
            'Create a New instance of a row in the tblFamily of dsNewFamily DataSet
            'Create a new instance of the FamilyDetails Structure in the Family NameSpace.
            Dim drCurrentFamily As dsNewFamily.tblFamilyRow
            ValidFamilyDetail = New Family.FamilyDetails
    
            Try
                'Create a new row and ready it to fill the columns.
                drCurrentFamily = DsNewFamily1.tblFamily.NewtblFamilyRow
    
                With drCurrentFamily
                    'Assign a new GUID
                    .Fam_ID = Guid.NewGuid
    
                    'Check the combobox and select the ID of the Church Selected in the list.
                    'Then Assign it to the foreign key, Ch_ID.
                    Dim aIndex As Integer
    
                    With cbChurchList
                        For aIndex = 0 To .Items.Count - 1
                            drCurrentFamily.Ch_ID = CType(cbChurchList.SelectedValue, Guid)
                            Exit For
                        Next
    
                        If aIndex >= .Items.Count Then
                            .SelectedIndex = -1
                        End If
    
                    End With
    
                    'Pass the information from the Public Structure FamilyDetails.
                    'This is were a Null Reference Error appears.  Saying an Object Reference
                    'is not set to an instance of an object.
                    .Fam_FamilyName = ValidFamilyDetail.FamilyName.ToString
                    .Fam_HomeAddress = ValidFamilyDetail.HomeAddress.ToString
                    .Fam_HomeCity = ValidFamilyDetail.HomeCity.ToString
                    .Fam_HomeState = ValidFamilyDetail.HomeState.ToString
                    .Fam_HomeZipCode = ValidFamilyDetail.HomePostalCode.ToString
                    .Fam_HomePhone = ValidFamilyDetail.HomePhone.ToString
                    .Fam_CreatedOn = CDate((Date.Today.Month & "/" & Date.Today.Day & "/" & Date.Today.Year))
                    .Fam_FamilyStatus = CInt("1")
    
                End With
    
                DsNewFamily1.tblFamily.Fam_IDColumn.AllowDBNull = True
    
                DsNewFamily1.tblFamily.AddtblFamilyRow(drCurrentFamily)
    
          
    
    
            Catch ex As System.ArgumentException
                DsNewFamily1.tblFamily(mintCurrent).CancelEdit()
    
            Catch ex As System.Data.ConstraintException
                MessageBox.Show("ID must be unique. Addition cancelled", _
                           "Error", MessageBoxButtons.OK)
    
            Catch ex As Exception
                MsgBox("Can't load Submit Information: Internal System Error." & vbCrLf & ex.Message)
    
            End Try
    
        End Sub
    Last edited by Pullayniece; September 7th, 2008 at 02:27 PM. Reason: Edited proper code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Object Not set to Instance of an Object in adding a data row.

    Please use CODE TAGS so we can read your mess!
    Code:
    ' Like this
         so it saves indents
    I don't see you using Rows.Add anywhere
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Sep 2008
    Posts
    10

    Re: Object Not set to Instance of an Object in adding a data row.

    Updated with proper code tags, sorry thought they were usual <> format not []. I have not used adds.row because I am using the addtblnamerow method.

    Any other clues would be appreciated.

  4. #4
    Join Date
    Sep 2008
    Posts
    10

    Re: Object Not set to Instance of an Object in adding a data row.

    Hey all, thought I'd update this post with what I've found so far to be the problem. I isolated the issue to the last line of the submit_click event "Call SubmitValidFamily(ValidCreateFamily) The ValidCreateFamily parameter at this point contains all the appropriate details to be added to the database. However these values seem to fail to pass through to the SubmitValidFamily Sub Procedure, thus referencing a null exception error. I was wondering what lines I might be missing or mistyped to fix this error.

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Object Not set to Instance of an Object in adding a data row.

    Post the solution, to help the next person, plus we'll look at it again
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Sep 2008
    Posts
    10

    Re: Object Not set to Instance of an Object in adding a data row.

    There is no solution yet, I just know the problem is happening between calling the SubmitValidFamily Sub routine passing those values to the ValidFamilyDetail object.

  7. #7
    Join Date
    Sep 2008
    Posts
    10

    Re: Object Not set to Instance of an Object in adding a data row.

    Just recently found the solution to the issue. It appears that I was creating a second instance of the FamilyDetails Object in the SubmitValidFamily Sub Routine. After commenting this out the code seems to transfer values fine.

    The line was with the code: ValidFamilyDetail = New Family.FamilyDetails
    in the SubmitValidFamily Sub Routine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured