We help IT Professionals succeed at work.

Windows App Dataset and Datarow errors

cpopham asked
Last Modified: 2010-04-24
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?
Watch Question


no one has any ideas?
Top Expert 2004

Shouldn't  you use

Dim ds As new DataSet
Top Expert 2004

Stupid remark, forget it


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.

This one is on us!
(Get your first solution completely free - no credit card required)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.