Results 1 to 7 of 7

Thread: how to handle a datareader with no records..

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2000
    Location
    treehouse
    Posts
    106

    how to handle a datareader with no records..

    I am using a datareader and its returning no records and I want to know how to handle that.

    Example:

    If not IsDbNull(dr("field1")) Then
    listbox1.Items.FindByValue(CType(dr("field1"), string).Selected = True
    End If

    Error:
    Invalid attempt to Read when no data is present.

  2. #2
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    if not objReader.Read() then
    'no records
    else
    Do
    'stuff
    while objReader.read()
    end if
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2000
    Location
    treehouse
    Posts
    106
    Thanks..

  4. #4
    Member Ooogaleee's Avatar
    Join Date
    Nov 2002
    Location
    Jacksonville, FL
    Posts
    52
    I had an issue that the datagrid was still showing the results of the last query when the datareader was coming back empty. Couldn't figure out how to clear it out in that case. I ended up setting the visible property to false if "not datareader.read()" and to true elsewise...

    Is there another way of doing this?...

    Ooogs

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2000
    Location
    treehouse
    Posts
    106
    are you binding the data to the grid?

  6. #6
    Member Ooogaleee's Avatar
    Join Date
    Nov 2002
    Location
    Jacksonville, FL
    Posts
    52
    yep, like this:


    Dim qry2 As String = "SELECT * FROM [tbl Main] WHERE (processor =" & Chr(39) & who & Chr(39) & ") AND (code <> 'C - Complete') and [Date Completed] Is Null"
    dc1.Open()
    Dim cmd As New OleDb.OleDbCommand()
    cmd.Connection = dc1
    cmd.CommandText = qry2
    Dim datareader As OleDb.OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    If Not datareader.Read() Then
    lblTest.Text = "Empty set"
    dgProc.Visible = False
    Else
    dgProc.Visible = True
    dgProc.DataSource = datareader
    dgProc.DataBind()
    dc1.Close()
    End If

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2000
    Location
    treehouse
    Posts
    106
    Have you tried to set the datasource property = nothing at the top of the code.

    I would think the datasource property is the one that is the source of the problem. Thats where the DataBind() method gets the dataset or datareader from. At least thats my understanding.

    just a thought.....

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