Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Trying to add new row to dataset

Author  Topic 

bubberz
Constraint Violating Yak Guru

289 Posts

Posted - 2005-11-29 : 15:22:58
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

bubberz
Constraint Violating Yak Guru

289 Posts

Posted - 2005-11-29 : 15:37:41
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
Go to Top of Page
   

- Advertisement -