Results 1 to 12 of 12

Thread: Adding Rows to Data Table

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Adding Rows to Data Table

    I have an application that I'm currently writing that takes the selected date from a calendar control and populates a datatable with bookings for that date from an access database. The problem I have is that there are not always bookings made so sometimes the datatable contains no rows, if I then try to add a row I get the following error.

    System.NullReferenceException: Object reference not set to an instance of an object.

    This I assume is because the datatable has a row count of 0. the question is how do I get around this problem, is there a way to add new rows to an empty data Table ?

    The code for adding the row is as follows:

    VB Code:
    1. Dim objDataRow As DataRow
    2.  
    3.         objDataRow=objDataSet.Tables("Bookings").NewRow
    4.         objDataRow.Item("Aircraft")=aircraft
    5.         objDataRow.Item("Date")=myDate
    6.         objDataRow.Item("Name")=txtName.Text
    7.         objDataRow.Item("Duration")=txtDuration.Text
    8.  
    9.     objDataSet.Tables("Bookings").Rows.Add(objDataRow)
    10.     objBookingDA.Update(objDataSet,"Bookings")

  2. #2
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: Adding Rows to Data Table

    I search into the existing Forums and found one. See this Forum and you'll find an answer!

    http://www.vbforums.com/showthread.p...rows+datatable

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: Adding Rows to Data Table

    Thanks, I know the mechanics of adding a datarow into an existing table, but when I try this with what is effectively an empty table it throws the error message.
    The question is can I add a row using the code in the original post to an empty data table and if not (which I suspect is the case) how do I get around the problem when a date is selected that currently has no records ?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Adding Rows to Data Table

    I'm going to make a bit of an educated guess and say that the problem is not that your DataTable contains no rows but that it contains no columns. If your query returns no rows then it may be that the table schema is not filled either. Check whether that is the case and, if so, you can call FillSchema just like you would call Fill to get the correct columns in the table:
    VB Code:
    1. Dim myTable As New DataTable
    2.  
    3. If myDataAdapter.Fill(myTable) = 0 Then
    4.     myDataAdapter.FillSchema(myTable)
    5. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Adding Rows to Data Table

    Quote Originally Posted by jmcilhinney
    I'm going to make a bit of an educated guess and say that the problem is not that your DataTable contains no rows but that it contains no columns. If your query returns no rows then it may be that the table schema is not filled either. Check whether that is the case and, if so, you can call FillSchema just like you would call Fill to get the correct columns in the table:
    VB Code:
    1. Dim myTable As New DataTable
    2.  
    3. If myDataAdapter.Fill(myTable) = 0 Then
    4.     myDataAdapter.FillSchema(myTable)
    5. End If
    John If you do a select query w/ the tables that has no rows/data it STILL get the schema of it.

    Tinbeard: Could you post the code on how you fill the dataset/datatable? Maybe you didn't fill the right datatable in which you use in adding a new row.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Adding Rows to Data Table

    Bummer. Obviously my guesses need to go back to school.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Junior Member
    Join Date
    Sep 2005
    Posts
    18

    Re: Adding Rows to Data Table

    Im my opinion, you should see the variant "myDate", which control give its value to "mydate". The error may be cause by that control.
    Good luck!

  8. #8
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    Re: Adding Rows to Data Table

    Here's a snippet of code I used to manually create a table in a dataset in one of my apps. It manually adds the table, then the columns. Maybe something similar can be done with rows.

    ds is a DataSet
    ds.Tables.Add(New System.Data.DataTable)
    ds.Tables(0).Columns.Add("Date")
    ds.Tables(0).Columns.Add("PhoneNumber")
    ds.Tables(0).Columns.Add("Name")

  9. #9
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: Adding Rows to Data Table

    Try this
    VB Code:
    1. Dim NewRow As System.Data.DataRow =ds.Tables(0).NewRow()
    2. NewRow(0) = Date.Now
    3. NewRow(1) = "35178945"
    4. NewRow(2) = "name"
    5. ds.Tables(0).Rows.Add(NewRow)

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: Adding Rows to Data Table

    This is the code I've used to initially fill the dataset / datatable

    VB Code:
    1. Sub BindData()
    2.     Dim strSelect as String
    3.         strSelect="SELECT * FROM booking WHERE Date=" & myDate
    4.         objBookingDA= New OleDBDataAdapter(strSelect,objConnection)
    5.         objBookingCB= New OleDBCommandBuilder(objBookingDA)
    6.         objBookingDA.FillSchema(objDataSet, schemaType.Source,"booking")
    7.         objBookingDA.Fill(objDataSet,"Bookings")
    8.         dlstBookings.DataSource=objDataSet.Tables("Bookings")
    9.         dlstBookings.DataBind
    10. End Sub


    If I remove the myDate variable as suggested I still get the error raised at the line.

    VB Code:
    1. objDataRow=objDataSet.Tables("Bookings").NewRow

    (see original posting for location of this line)

  11. #11
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Adding Rows to Data Table

    Is this a web apps? You don't need to use the fillschema method becuase fill method already get the schema of the table.
    VB Code:
    1. Sub BindData()
    2.     Dim strSelect as String
    3.         strSelect="SELECT * FROM booking WHERE Date=" & myDate
    4.         objBookingDA= New OleDBDataAdapter(strSelect,objConnection)
    5.         objBookingCB= New OleDBCommandBuilder(objBookingDA)
    6.         objBookingDA.Fill(objDataSet,"Bookings")
    7.         dlstBookings.DataSource=objDataSet.Tables("Bookings")
    8.         dlstBookings.DataBind
    9. End Sub

    Question: When do you call the sub binddata? After you add a rows or before?
    I guess you add first a row to the datatable before calling the sub binddata.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: Adding Rows to Data Table

    The BindData() is called from the calendar selectionChanged so when someone clicks on a date the datalist is displayed showing all bookings for that date.

    Only after this is the facility for adding a booking made visible (I want to make sure they select a date before making a booking).

    The plan is that when they have made a booking BindData will be called again to re-populate the datalist with the new data, so the user can see that their booking has been added.

    It is a web app, should this be moved to asp.net forum ?

    Apologies if I've posted in the wrong forum, so used to having problems with windows app's it's second nature to post here
    Last edited by Tinbeard; Sep 29th, 2005 at 08:47 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width