Below is the code for the Page_load and button click event.
When the button is clicked, I get:
Object reference not set to an instance of an object.
..which happens @:
Line 80: rowNew = dsContacts.Contacts.NewContactsRow
Dim adptContacts As SqlDataAdapter
Dim dsContacts As dsContacts
Public dsContactTypes As dsContactTypes
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
adptContacts = Cache("adptContacts")
dsContacts = Cache("dsContacts")
dsContactTypes = Cache("dsContactTypes")
drpContactTypes.DataSource = dsContactTypes
drpContactTypes.DataTextField = "ContactType"
drpContactTypes.DataValueField = "ContactTypeID"
drpContactTypes.DataBind()
drpStates.DataBind()
End If
End Sub
Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
'Create new row for dataset
Dim rowNew As dsContacts.ContactsRow
rowNew = dsContacts.Contacts.NewContactsRow
'Add data to the row
rowNew.ContactID = GetNewID(dsContacts.Tables("Contacts"))
rowNew.FirstName = txtFirstName.Text
rowNew.LastName = txtLastName.Text
rowNew.Address = txtAddress.Text
rowNew.City = txtCity.Text
rowNew.StateOrProvince = drpStates.SelectedItem.Text
rowNew.PostalCode = txtZip.Text
rowNew.HomePhone = txtHomePhone.Text
rowNew.WorkPhone = txtWorkPhone.Text
rowNew.Notes = txtNotes.Text
rowNew.ContactID = drpContactTypes.SelectedItem.Value
'Add row to dataset
dsContacts.Contacts.AddContactsRow(rowNew)
Try
adptContacts.Update(dsContacts)
litStatus.Text = rowNew.FirstName & " " & rowNew.LastName & " successfully added!"
Call ClearTextBoxes()
Catch ex As Exception
'If unique ID not unique
litStatus.Text = "DB error occured " & ex.Message
End Try
End Sub
When the button is clicked, I get:
Object reference not set to an instance of an object.
..which happens @:
Line 80: rowNew = dsContacts.Contacts.NewContactsRow
Dim adptContacts As SqlDataAdapter
Dim dsContacts As dsContacts
Public dsContactTypes As dsContactTypes
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
adptContacts = Cache("adptContacts")
dsContacts = Cache("dsContacts")
dsContactTypes = Cache("dsContactTypes")
drpContactTypes.DataSource = dsContactTypes
drpContactTypes.DataTextField = "ContactType"
drpContactTypes.DataValueField = "ContactTypeID"
drpContactTypes.DataBind()
drpStates.DataBind()
End If
End Sub
Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
'Create new row for dataset
Dim rowNew As dsContacts.ContactsRow
rowNew = dsContacts.Contacts.NewContactsRow
'Add data to the row
rowNew.ContactID = GetNewID(dsContacts.Tables("Contacts"))
rowNew.FirstName = txtFirstName.Text
rowNew.LastName = txtLastName.Text
rowNew.Address = txtAddress.Text
rowNew.City = txtCity.Text
rowNew.StateOrProvince = drpStates.SelectedItem.Text
rowNew.PostalCode = txtZip.Text
rowNew.HomePhone = txtHomePhone.Text
rowNew.WorkPhone = txtWorkPhone.Text
rowNew.Notes = txtNotes.Text
rowNew.ContactID = drpContactTypes.SelectedItem.Value
'Add row to dataset
dsContacts.Contacts.AddContactsRow(rowNew)
Try
adptContacts.Update(dsContacts)
litStatus.Text = rowNew.FirstName & " " & rowNew.LastName & " successfully added!"
Call ClearTextBoxes()
Catch ex As Exception
'If unique ID not unique
litStatus.Text = "DB error occured " & ex.Message
End Try
End Sub