cpopham Posted June 8, 2004 Posted June 8, 2004 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" 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 Quote ____________________________________________ http://www.pophamcafe.com I am starting a developers section, more tutorials than anything.
Administrators PlausiblyDamp Posted June 8, 2004 Administrators Posted June 8, 2004 When you get this error what is the value of dsAGPOs and dsAGPOs.Tables("AGPOInfo") in the debugger? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
cpopham Posted June 8, 2004 Author Posted June 8, 2004 It says: <NewDataSet /> That is using debug.writeline(dsAGPOs.GetXML right before I declare the myRow variable. Quote ____________________________________________ http://www.pophamcafe.com I am starting a developers section, more tutorials than anything.
Administrators PlausiblyDamp Posted June 8, 2004 Administrators Posted June 8, 2004 Looks like the AGPOInfo table hasn't been created. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
cpopham Posted June 8, 2004 Author Posted June 8, 2004 At the module layer I use: Private dsAGPOs as dataset to declare the dataset. I want it to remain until the program is terminated because my users will continue to add datarows to the dataset. I then have this if statement which checks a boolean value to see if this is the first run through or not and if is the first run through it is suppose to create the 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 Shouldn't that create the dataset? If not what do I need to change to create the dataset? Chester Quote ____________________________________________ http://www.pophamcafe.com I am starting a developers section, more tutorials than anything.
Administrators PlausiblyDamp Posted June 8, 2004 Administrators Posted June 8, 2004 Looks like you are creating a table called AGPO but refering to one called AGPOInfo for starters. Also if you step through the code in the debugger does it call into the above routine correctly? Also where is blnCreated declared / accessed? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
cpopham Posted June 8, 2004 Author Posted June 8, 2004 Referring to the wrong datatable was the problem. I must have looked at that code 50 times and did not catch that problem! The boolean variable is created at the module level of the datatier. But my datatable keeps getting remade. I am trying to figure out how to keep this from happening. Maybe turn this function to to seperate functions one to create the dataset and another to update the datatable using a counter in the main form? Chester Quote ____________________________________________ http://www.pophamcafe.com I am starting a developers section, more tutorials than anything.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.