• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 869
  • Last Modified:

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

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
excellis
Asked:
excellis
  • 4
  • 2
1 Solution
 
käµfm³d 👽Commented:
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
 
Paul JacksonCommented:
Use :
While (Reader.Read)
...
End While
0
 
excellisAuthor Commented:
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
excellisAuthor Commented:
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
 
Paul JacksonCommented:
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
 
excellisAuthor Commented:
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
 
excellisAuthor Commented:
No one answered the initial question.  Got work arounds.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now