Results 1 to 5 of 5

Thread: [RESOLVED] Dataset - datatable

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    92

    Resolved [RESOLVED] Dataset - datatable

    Hi, I have a dataset and inside it a dataTable. How can I retrieve the data of the datatable?
    My code is this:
    objDataAdapter.SelectCommand = New SqlCommand()
    objDataAdapter.SelectCommand.Connection = objConnection
    objDataAdapter.SelectCommand.CommandText = _
    "SELECT distinct Title FROM Outlookbar "
    objDataAdapter.SelectCommand.CommandType = CommandType.Text
    objConnection.Open()

    ' Fill the DataSet object with data...
    objDataAdapter.Fill(objDataSet, "Outlookbar")
    ............
    I think that untill here I have created a Dataset with a dataTable that has the name Outlookbar. In my example the sql query must return 4 rows(4 data from the table of the database).
    So, how can I store its one of these rows (of the dataTable) in a variable for later use?

  2. #2
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681

    Re: Dataset - datatable

    Look at objDataSet.Tables(0).Rows - you can loop through the 4 rows and read values as needed.

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

    Re: Dataset - datatable

    What VBCrazyCoder said is absolutely correct but, given that you've created the DataTable by name, I would tend to access it by name too, i.e. objDataSet.Tables("Outlookbar"). Neither is technically more correct than the other but using a name to access a named object feels more intuitive to me, plus it reinforces the realtionship between the table you filled and the table you're accessing, which may not otherwise be immediately obvious. Using the name is also easier when you have multiple tables, so you don't have to care which one is first.
    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

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    92

    Re: Dataset - datatable

    Thank you for answering. I managed to find it :
    lblArchives.Text = objDataSet.Tables("Outlookbar").Rows(0).Item("Title")

    AS you can see I am using the instance of the DataSet (objDataSet),then the Tables("Outlookbar") which is the collection name of the DataTable and the row and the Item to get the right row and the right column from the DataTable.
    Thanks for answering to me

  5. #5
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681

    Re: [RESOLVED] Dataset - datatable

    I guess the only thing to keep in mind would be that string comparisons are less efficient than ordinal based lookups - so if you had a large amount of data you would probably want to use ordinals (like for accessing rows, etc.) - you can always use an enum or something to get the name / value pairs to still be able to go by a name but actually use the value - if that makes sense.

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