Results 1 to 8 of 8

Thread: Reading all rows in a column of a dataset

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Scotland, UK
    Posts
    321

    Reading all rows in a column of a dataset

    I have loaded the contents of an XML file into a dataset, and then into a datagrid.

    But I am stuck with this (probably simple thing),

    How do you read data from a specific field / column, or find out how many records / rows exist, in preferably the dataset. But if not possible, in the datagrid.

    Thanks in advance for any help you can offer.

    Code:
            Dim d As New DataSet()
    
            Try
                d.ReadXml("C:\test.xml")
                Me.DataGrid1.DataSource = d
    
            Catch exp As Exception
                MessageBox.Show(exp.Message.ToString, "Exception was caught", MessageBoxButtons.OK)
            End Try
    
            ds = d.Copy
            DataGrid1.DataSource = ds
            DataGrid1.Refresh()
            d.Dispose()

  2. #2
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    To find how many rows there are: ds.tables("mytable").rows.count
    To read from a particular column: ds.tables("mytable").rows(r)(c)
    where r is the row 0 based, and c is the column 0 based.
    Learn, this is the Keyword...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Scotland, UK
    Posts
    321
    thank you

  4. #4
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    Learn, this is the Keyword...

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Scotland, UK
    Posts
    321
    When using it, I am getting the error:

    An unhandled exception of type 'System.InvalidCastException' occurred in myClient.exe

    Code:
           Dim a As Integer
            a = Me.ListBox1.SelectedIndex
    
            Me.lblFullName = ds.Tables("person").Rows(a)(0)     << ERROR at this line
            Me.lblFirstName = ds.Tables("person").Rows(a)("firstName")
            Me.lblDOB = ds.Tables("person").Rows(a)("birth")
            Me.lblFax = ds.Tables("person").Rows(a)("fax")
            Me.lblMobile = ds.Tables("person").Rows(a)("mobile")
            Me.lblMSN = ds.Tables("person").Rows(a)("msn")
            Me.lblSurname = ds.Tables("person").Rows(a)("surname")
            Me.lblTelNo = ds.Tables("person").Rows(a)("tel")
            Me.lblWebsite = ds.Tables("person").Rows(a)("website")
            Me.lblWork = ds.Tables("person").Rows(a)("work")
            Me.txtNotes = ds.Tables("person").Rows(a)("notes")
    integer a, appears to be a valid number, that should exist, so anyone any ideas?

  6. #6
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    When no items are selected, listbox.selectedindex equals -1.Try to see if a=-1
    Learn, this is the Keyword...

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Scotland, UK
    Posts
    321
    solved it, forgot the .text

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This will returns number of rows in database .
    VB Code:
    1. Public Function Nu_of_Rows(ByVal TableStr As String) As Integer
    2. Dim SqlStr As String = "Select * FROM " & TableStr
    3. Dim ds As New DataSet()
    4. Dim adp As New OleDbDataAdapter(SqlStr, MyConnection)
    5. adp.Fill(ds, TableStr)
    6. Return ds.Tables(TableStr).Rows.Count
    7. End Function

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