?
Solved

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

Posted on 2011-03-18
7
Medium Priority
?
854 Views
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
                connection.Open()
                Dim reader As SqlDataReader = command.ExecuteReader()
                Try
                    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()"?

Matt
0
Comment
Question by:excellis
[X]
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
7 Comments
 
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.
0
 
LVL 29

Expert Comment

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

Author Comment

by:excellis
ID: 35166427
To:kaufmed

Here is the Stored Procedure

ALTER PROCEDURE dbo.uspGET_Clients
      
      (
      @param_ClientID nvarchar(10) = NULL
      )
AS

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

RETURN
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

Author Comment

by:excellis
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.
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35166492
You need to call reader.read :

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

0
 

Accepted Solution

by:
excellis earned 0 total points
ID: 35166535
jack072,

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.
0
 

Author Closing Comment

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

Featured Post

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

Question has a verified solution.

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

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

771 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