In ASP.NET, how come .HasRows does not work properly?

Posted on 2011-03-18
Last Modified: 2012-05-11
I have the following code:

Using connection As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString1"))
                Dim command As New SqlCommand(queryString, connection)
                command.Parameters.Add("@param_ClientID", Data.SqlDbType.NVarChar, 10).Value = ClientID.Trim.ToString
                Dim reader As SqlDataReader = command.ExecuteReader()
                    If reader.HasRows Then
                        stPrompt = "Client Id Check: " & reader(0).ToString

I am working with test data so i know the client does not exist.  When it gets to "If reader.HasRows" it is always coming up as TRUE.  When it gets to the "reader(0).ToString", it gives an error "Invalid attempt when no data is present."

Am I using .HasRows properly?

Should I do something other than "SqlDataReader = command.ExecuteReader()"?

Question by:excellis
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35166309
What does your query look like? A query that return no records will result in HasRows being false; a query that returns a count of 0 to indicate no records has 1 records, so HasRows would be true.
LVL 29

Expert Comment

by:Paul Jackson
ID: 35166350
Use :
While (Reader.Read)
End While

Author Comment

ID: 35166427

Here is the Stored Procedure

      @param_ClientID nvarchar(10) = NULL

      SELECT *
      FROM Master_Clients
      WHERE Client_ID LIKE COALESCE(@param_ClientID, '%');

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 35166474
The Stored Procedure is used in other parts.  I did not want to make one specific for this part of the program.  

Before I get to this part of the program, I have already checked that there is something in the Client ID field so I know that the parameter will be something other than a blank or null.
LVL 29

Expert Comment

by:Paul Jackson
ID: 35166492
You need to call :

If reader.HasRows Then
            Do While reader.Read()
                Console.WriteLine(reader.GetInt32(0) _
                  & vbTab & reader.GetString(1))
            Console.WriteLine("No rows found.")
        End If


Accepted Solution

excellis earned 0 total points
ID: 35166535

Doesn't solve the problem of why reader.HasRows is always coming up as TRUE.  I will try working with your 1st suggestion "While (Reader.Read)" and see if i can get that to work.

Author Closing Comment

ID: 35499761
No one answered the initial question.  Got work arounds.

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question