Click to See Complete Forum and Search --> : [RESOLVED] Object Not set to Instance of an Object in adding a data row.


Pullayniece
September 6th, 2008, 11:26 PM
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:



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

dglienna
September 7th, 2008, 11:43 AM
Please use CODE TAGS so we can read your mess!

' Like this
so it saves indents

I don't see you using Rows.Add anywhere

Pullayniece
September 7th, 2008, 02:48 PM
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.

Pullayniece
September 7th, 2008, 11:09 PM
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.

dglienna
September 8th, 2008, 12:42 AM
Post the solution, to help the next person, plus we'll look at it again

Pullayniece
September 8th, 2008, 11:59 AM
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.

Pullayniece
September 8th, 2008, 12:32 PM
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.