troubleshooting Question

Windows App Dataset and Datarow errors

Avatar of cpopham
cpopham asked on
Visual Basic.NET
5 Comments1 Solution558 ViewsLast Modified:
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(strDate, strPO, strSpend, strVend, strBuy)

        Me.dgAGPOs.DataSource = ds
        Me.dgAGPOs.DataMember = "AGPOInfo"
    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
            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


            Return dsAGPOs

        Catch ex As Exception

        End Try

    End Function

Any Ideas?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros