Empty table in a dataset

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pmelanso@uwaterloo.ca

    #1

    Empty table in a dataset

    Hello,
    What would be the statement to see if a table in a dataset has no rows
    ( i.e. is empty)??? What I need this for is if an sql statement doesn't
    return anything... I need to know when that happends.

    Pam

  • Chris

    #2
    Re: Empty table in a dataset

    pmelanso@uwater loo.ca wrote:[color=blue]
    > Hello,
    > What would be the statement to see if a table in a dataset has no rows
    > ( i.e. is empty)??? What I need this for is if an sql statement doesn't
    > return anything... I need to know when that happends.
    >
    > Pam
    >[/color]

    if not DataSet.Tables( 0) is nothing then
    DataSet.Tables( 0).Rows.Count
    end if

    Comment

    • Marina

      #3
      Re: Empty table in a dataset

      Actually, I would expect that code to throw an index out of bounds
      exception.

      It should be something like:

      If myDS.Tables.Cou nt = 0 Then
      ' there are no tables
      End If

      "Chris" <no@spam.com> wrote in message
      news:%23Q09DpU6 FHA.2816@tk2msf tngp13.phx.gbl. ..[color=blue]
      > pmelanso@uwater loo.ca wrote:[color=green]
      >> Hello,
      >> What would be the statement to see if a table in a dataset has no rows
      >> ( i.e. is empty)??? What I need this for is if an sql statement doesn't
      >> return anything... I need to know when that happends.
      >>
      >> Pam
      >>[/color]
      >
      > if not DataSet.Tables( 0) is nothing then
      > DataSet.Tables( 0).Rows.Count
      > end if[/color]


      Comment

      • Chris

        #4
        Re: Empty table in a dataset

        Marina wrote:[color=blue]
        > Actually, I would expect that code to throw an index out of bounds
        > exception.
        >
        > It should be something like:
        >
        > If myDS.Tables.Cou nt = 0 Then
        > ' there are no tables
        > End If
        >
        > "Chris" <no@spam.com> wrote in message
        > news:%23Q09DpU6 FHA.2816@tk2msf tngp13.phx.gbl. ..
        >[color=green]
        >>pmelanso@uwat erloo.ca wrote:
        >>[color=darkred]
        >>>Hello,
        >>>What would be the statement to see if a table in a dataset has no rows
        >>>( i.e. is empty)??? What I need this for is if an sql statement doesn't
        >>>return anything... I need to know when that happends.
        >>>
        >>>Pam
        >>>[/color]
        >>
        >>if not DataSet.Tables( 0) is nothing then
        >>DataSet.Table s(0).Rows.Count
        >>end if[/color]
        >
        >
        >[/color]

        You are right... I was thinking but didn't type along the lines of:

        if not MyDS.Tables("Ta bleName") is nothing then
        ....
        end if

        because if the ds has two tables in it your way isn't valid for his needs

        Comment

        • datatable row count sample

          #5
          RE: Empty table in a dataset

          'note the test can be done in one line of code like in the commented out line
          'below
          'If mydataset.table s("TableName"). Rows.Count < 1 Then MsgBox("Zero datarows
          exist", , "No Data")

          'declare objects outside of the block so you can dispose of them in
          finally
          Dim tbl As DataTable
          Dim ds As DataSet
          Dim iRows As Integer
          Try
          'set objects inside of try to trap exceptions
          ds = New DataSet("ds")
          tbl = New DataTable("t")
          tbl.Columns.Add ("ID")
          ds.Tables.Add(t bl)
          'returns 0(zero) if there are no datarows in the table referenced
          iRows = ds.Tables("t"). Rows.Count

          MsgBox(iRows, , "Number of datarows: iRows")
          'test the value of iRow
          If iRows > 0 Then
          'No datarows present
          Else
          'at least one datarow present
          End If

          Catch ex As Exception
          MsgBox(ex.Messa ge.ToString, , "System Exception")
          Finally
          tbl.Dispose() : ds.Dispose()
          End Try

          "pmelanso@uwate rloo.ca" wrote:
          [color=blue]
          > Hello,
          > What would be the statement to see if a table in a dataset has no rows
          > ( i.e. is empty)??? What I need this for is if an sql statement doesn't
          > return anything... I need to know when that happends.
          >
          > Pam
          >
          >[/color]

          Comment

          • datatable row count sample

            #6
            RE: datatable row count sample.

            'note the test can be done in one line of code like in the commented out line
            'below
            'If mydataset.table s("TableName"). Rows.Count < 1 Then MsgBox("Zero datarows
            exist", , "No Data").

            'declare objects outside of the block so you can dispose of them in
            finally
            Dim tbl As DataTable
            Dim ds As DataSet
            Dim iRows As Integer
            Try
            'set objects inside of try to trap exceptions
            ds = New DataSet("ds")
            tbl = New DataTable("t")
            tbl.Columns.Add ("ID")
            ds.Tables.Add(t bl)
            'returns 0(zero) if there are no datarows in the table referenced
            iRows = ds.Tables("t"). Rows.Count

            MsgBox(iRows, , "Number of datarows: iRows")
            'test the value of iRow
            If iRows > 0 Then
            'No datarows present
            Else
            'at least one datarow present
            End If

            Catch ex As Exception
            MsgBox(ex.Messa ge.ToString, , "System Exception")
            Finally
            tbl.Dispose() : ds.Dispose()
            End Try

            "pmelanso@uwate rloo.ca" wrote:
            [color=blue]
            > Hello,
            > What would be the statement to see if a table in a dataset has no rows
            > ( i.e. is empty)??? What I need this for is if an sql statement doesn't
            > return anything... I need to know when that happends.
            >
            > Pam
            >
            >[/color]

            Comment

            Working...