Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to add row to dataset

Status
Not open for further replies.

lanm

Programmer
Jul 7, 2005
244
US
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
 
Fixed with:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

adptContacts = Cache("adptContacts")
dsContacts = Cache("dsContacts")
dsContactTypes = Cache("dsContactTypes")

If Not IsPostBack Then
drpContactTypes.DataSource = dsContactTypes
drpContactTypes.DataTextField = "ContactType"
drpContactTypes.DataValueField = "ContactTypeID"
drpContactTypes.DataBind()
drpStates.DataBind()
End If
End Sub
 
Where are you declaring the dataset? Is it in the code behind or though the dataadapter generate dataset wizard?
 
The dataset is created in cache from a previous / startup page.

I declare the var:
Dim dsContacts As dsContacts
..above the Page_Load event



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top