empty dataset

Hi
How do i check to see if there are values in a dataset before displaying
at the moment i get this error
System.IndexOutOfRangeException: There is no row at position 0.

and the code it relates to is:
            PeopleGrid.Datasource = Ds.tables("People")
            address.text = DS1.Tables("Header").Rows(0).Item("title")
            pm.text = DS1.Tables("Header").Rows(0).Item("pm")
           
the dataset is generated from a query that uses an id passed from another page - so sometimes there will be records returned and sometimes there won't
thanks
lz7cjcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lz7cjcAuthor Commented:
oops - wrong section!
will get it moved
martie_11Commented:
Hello lz7cjc,

Have you tried:

if (Ds != null){
     PeopleGrid.Datasource = Ds.tables("People")
     ...
}

...hope that helps.
lz7cjcAuthor Commented:
have tried that and get
Value of type 'System.Data.DataSet' cannot be converted to 'Boolean'.
any ideas?
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

martie_11Commented:
how do you declare your dataset?

if I do:

      DataSet ds = null;
      if (ds == null)
            Response.Write("In IF");

I get output of "In IF"

Did you init your dataset to null / nothing?
martie_11Commented:
Once you get your dataset, you can also check your row count.

i.e.

if (ds.Tables["People"].Rows.Count > 0){
     ...
}
lz7cjcAuthor Commented:
i do
Dim Ds as new DataSet
Dim Ds1 as new DataSet
so i guess that is to null? when i try
DS= null
i get the following error:
 BC30822: 'null' is not declared. 'Null' constant is no longer supported; use 'System.DBNull' instead.
           
lz7cjcAuthor Commented:
Martie
I believe i am getting the error before i can try your code but thanks for the thought
martie_11Commented:
Sorry, there is no 'null' in VB.NET.

null is represented by 'Nothing' in VB.NET...try:

Dim Ds as new DataSet = Nothing
If (Ds Is Nothing){
   ...
}

What happens when you try to count the number of rows?  Did you try that?
lz7cjcAuthor Commented:
i assume you mean this
 <%if (ds.Tables["People"].Rows.Count > 0)  then %>
   
I don't seem to be able to do this in the code to manipulate whether i use a datagrid or print a message to the screen

it tells me that  Name 'ds' is not declared
martie_11Commented:
'ds' is supposed to be whatever you declared your dataset as...

Can you paste your ASPX code and your code behind (aspx.cs)?

Thanks...
lz7cjcAuthor Commented:
sorry for the delay
here you go:

lz7cjcAuthor Commented:
sorry... hold on... have got a different error on this page now
once that is sorted i shall get back to you
haven't forgotten
thanks
Nick
lz7cjcAuthor Commented:
Hi
sorry for the delay... have upped the points now i have some to spare. The code i have is:
           Dim Ds as new DataSet
            Ds = Nothing
         '   connecttodatabase.open
            DataAdapterVariable.Fill(DS, "People")

         '   connecttodatabase.close
            if (Ds is nothing) then
            header.text = "There are no notes for this project manager"

            else
            header.text = "Notes by Project Manager: " &  DS.tables("People").Rows(0).Item("pmname").ToString()
            PeopleGrid.Datasource = Ds.tables("People")

            end if
and i get the following error:
System.ArgumentNullException: Value cannot be null. Parameter name: dataSet
on line
DataAdapterVariable.Fill(DS, "People")

any ideas?
thanks

martie_11Commented:
What happens when you take out:
Ds = Nothing

?
martie_11Commented:
Also, where do you declare and instantiate DataAdapterVariable?

Can you show me that code?
lz7cjcAuthor Commented:
if i take out ds=nothing then the code always goes into the else part of the statement... which is no good since i am trying to stop the following error:
 System.IndexOutOfRangeException: There is no row at position 0.

here is the rest of the code:
Sub Page_Load(Sender As Object, E As EventArgs)
    dim pmid as integer
    pmid = Trim(request.querystring("pmid"))

    'create the variable to hold the connection
            Dim ConnectToDatabase As String = System.Configuration.ConfigurationSettings.AppSettings.Get("ConnectionString")

            Dim DataAdapterVariable as new SQLDataAdapter("SELECT tbl_job.jobTitle, tbl_job.jobDescription, tbl_job.began, tbl_job.jobID, tbl_job.endDate, tbl_job.extensionDate, tbl_status.statusName, RTRIM(ISNULL(tbl_property.address1, '')) + ' ' + RTRIM(ISNULL(tbl_property.address2, '')) + ' ' + RTRIM(ISNULL(tbl_property.address3, '')) + ' ' + RTRIM(ISNULL(tbl_property.postcode, '')) + ' ' + RTRIM(ISNULL(tbl_property.city, '')) AS title, RTRIM(ISNULL(tbl_projectmanager.firstname, '')) + ' ' + RTRIM(ISNULL(tbl_projectmanager.lastname, '')) AS pmname, tbl_colour.code as colourname FROM tbl_job INNER JOIN tbl_status ON tbl_job.statusId = tbl_status.statusId INNER JOIN tbl_property ON tbl_job.propertyId = tbl_property.propertyID INNER JOIN tbl_ProjectManagerProperty ON tbl_property.propertyID = tbl_ProjectManagerProperty.PropertyId INNER JOIN tbl_projectmanager ON tbl_ProjectManagerProperty.ProjectManagerId = tbl_projectmanager.projectManagerID INNER JOIN tbl_colour ON tbl_projectmanager.colourId = tbl_colour.colourid WHERE (tbl_ProjectManagerProperty.ProjectManagerId =" & pmid & ")",connecttodatabase)
             Dim Ds as new DataSet
             Ds = Nothing
             DataAdapterVariable.Fill(DS, "People")

       
            if (Ds is nothing) then
            header.text = "There are no notes for this project manager"

            else
            header.text = "Notes by Project Manager: " &  DS.tables("People").Rows(0).Item("pmname").ToString()
            PeopleGrid.Datasource = Ds.tables("People")

            end if

            page.databind
     end sub
martie_11Commented:
>System.IndexOutOfRangeException: There is no row at position 0.
Means your ds does not contain any rows...

What makes you so sure that your select is returning # of rows > 0?
martie_11Commented:
          Dim Ds as new DataSet  ' this line allocates memory for DS
            Ds = Nothing      ' this line says:  release the memory allocated to DS

...point being, you can't be setting Ds to Nothing
lz7cjcAuthor Commented:
i don't understand your point...
i want to stop this error happening... as i understood it when i get this error there are no records returned... i want to display a message if this is the case
????
what do i do?
thanks
martie_11Commented:
Okay, I know what you mean...

Lets start with this:  How do you know your query returns any rows?
lz7cjcAuthor Commented:
That is what i was hoping to check
i know that if there is data in the relevant tables then i get a records returned in the query
but there won't always be data - it depends on the "where" bit
perhaps i am getting confused with terminology but i basically wanted to check if the query returned any records and if it didn't to avoid trying to display rows that don't exist

is that wrong?
martie_11Commented:
Try this:

C#:

int rowCount = Ds.Tables[0].Rows.Count;
if (rowCount < 1)
     ...

I'm sure the VB.NET equivelent is simple as well...
lz7cjcAuthor Commented:
and therein lies the rub!
i don't know what the equivalent is so we are stuck and i can't get any further with regard to resolving my problem

martie_11Commented:
lol

Okay...brb
martie_11Commented:
       Dim Ds As DataSet = New DataSet
        Dim rowCount As Integer
        rowCount = Ds.Tables(0).Rows.Count

try that

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lz7cjcAuthor Commented:
i have very great pleasure in finally being able to award you these points... enjoy them!
thanks for your help
Nick
martie_11Commented:
np, I hope that's not the only buy you worked on this month : P
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.