cpopham
asked on
Windows App Dataset and Datarow errors
I am writing an application where my user makes a selection with a combo box and then it pulls up the appropriate information for the selection. If the user approves of the selection he clicks a button which adds the information to another dataset.
Everything works fine except when the button is clicked to add the information to the dataset.
I get the error Object reference not set to an instance of an object.
Here should be the appropriate code:
This is the button to add the information to the dataset. The DataTier is a separate class for all of my data manipulation.
Private Sub btnAddPO_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnAddPO.Click
Dim ds As DataSet
Dim strDate As Date = Today
Dim strPO As String = mstrSelectedPO
Dim strSpend As String = Me.lblSpend.Text
Dim strVend As String = Me.lblVendor.Text
Dim strBuy As String = Me.lblBuyer.Text
mobjDataTier = New DataTier
ds = mobjDataTier.AddAGPO(strDa te, strPO, strSpend, strVend, strBuy)
Me.dgAGPOs.DataSource = ds
Me.dgAGPOs.DataMember = "AGPOInfo"
Me.dgAGPOs.Refresh()
End Sub
This is the code in my data class which is giving me the error:
Public Function AddAGPO(ByVal strDate As String, _
ByVal strPONum As String, _
ByVal strSpend As String, _
ByVal strVend As String, _
ByVal strBuyer As String) _
As DataSet
Try
If blnCreated = False Then
dsAGPOs = New DataSet
With dsAGPOs.Tables.Add("AGPO")
.Columns.Add("ToAG", GetType(String))
.Columns.Add("PONum", GetType(String))
.Columns.Add("Vendor", GetType(String))
.Columns.Add("Buyer", GetType(String))
End With
blnCreated = True
End If
Dim myRow As DataRow
myRow = dsAGPOs.Tables("AGPOInfo") .NewRow ‘This is the row that is causing the error.
myRow.Item(0) = strDate
myRow.Item(1) = strPONum
myRow.Item(2) = strSpend
myRow.Item(3) = strVend
myRow.Item(4) = strBuyer
dsAGPOs.Tables("AGPOInfo") .Rows.Add( myRow)
Return dsAGPOs
Catch ex As Exception
MessageBox.Show(ex.Message )
End Try
End Function
Any Ideas?
Chester
Everything works fine except when the button is clicked to add the information to the dataset.
I get the error Object reference not set to an instance of an object.
Here should be the appropriate code:
This is the button to add the information to the dataset. The DataTier is a separate class for all of my data manipulation.
Private Sub btnAddPO_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnAddPO.Click
Dim ds As DataSet
Dim strDate As Date = Today
Dim strPO As String = mstrSelectedPO
Dim strSpend As String = Me.lblSpend.Text
Dim strVend As String = Me.lblVendor.Text
Dim strBuy As String = Me.lblBuyer.Text
mobjDataTier = New DataTier
ds = mobjDataTier.AddAGPO(strDa
Me.dgAGPOs.DataSource = ds
Me.dgAGPOs.DataMember = "AGPOInfo"
Me.dgAGPOs.Refresh()
End Sub
This is the code in my data class which is giving me the error:
Public Function AddAGPO(ByVal strDate As String, _
ByVal strPONum As String, _
ByVal strSpend As String, _
ByVal strVend As String, _
ByVal strBuyer As String) _
As DataSet
Try
If blnCreated = False Then
dsAGPOs = New DataSet
With dsAGPOs.Tables.Add("AGPO")
.Columns.Add("ToAG", GetType(String))
.Columns.Add("PONum", GetType(String))
.Columns.Add("Vendor", GetType(String))
.Columns.Add("Buyer", GetType(String))
End With
blnCreated = True
End If
Dim myRow As DataRow
myRow = dsAGPOs.Tables("AGPOInfo")
myRow.Item(0) = strDate
myRow.Item(1) = strPONum
myRow.Item(2) = strSpend
myRow.Item(3) = strVend
myRow.Item(4) = strBuyer
dsAGPOs.Tables("AGPOInfo")
Return dsAGPOs
Catch ex As Exception
MessageBox.Show(ex.Message
End Try
End Function
Any Ideas?
Chester
Shouldn't you use
Dim ds As new DataSet
Dim ds As new DataSet
Stupid remark, forget it
ASKER
Okay the problem was that I had the datatable name declared as one thing (AGPOInfo) but I was referrign to a datatable named "AGPO" when Iw as adding my datacolumns.
Chester
Chester
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER