Hello , I don't know why I am catching an error when I do a select statement that returns no rows. I am Using SQL Server (yes i am using oledb but only for testing/learning purposes).


Here is the SQL statement
SELECT * FROM tblLogIn WHERE UserName= '" & UserName & "' AND Password= '" & Password & "'"

Here is the error

System.Data.OleDb.OleDbException: Object or data matching the name, range, or selection criteria was not found within the scope of this operation. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleRow(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) at OptimaBackEndTest.LogIn.ExecuteQuery(String SQL, String DB) in C:\Inetpub\wwwroot\OptimaBackEndTest\login.aspx.vb:line 36

If it returns a row then I get no error.

Here is the code for the page

VB Code:
  1. Imports System.Data.OleDb
  2. Public Class LogIn
  3.     Inherits System.Web.UI.Page
  4.     Protected WithEvents lblBadLogIn As System.Web.UI.WebControls.Label
  5.     Protected WithEvents txtUserName As System.Web.UI.HtmlControls.HtmlInputText
  6.     Protected WithEvents txtPassword As System.Web.UI.HtmlControls.HtmlInputText
  7.     Protected WithEvents lblConnectionError As System.Web.UI.WebControls.Label
  8.     Dim Conn As OleDbConnection, Command As OleDbCommand, DataRead As OleDbDataReader
  9.     Protected WithEvents btnLogIn As System.Web.UI.WebControls.Button
  10.  
  11.  
  12.     Public Sub ExecuteQuery(ByVal SQL As String, ByVal DB As String)
  13.         If Application("Connection") Is Nothing Then
  14.             Try
  15.                 Conn = New OleDbConnection(System.Configuration.ConfigurationSettings.AppSettings.Get(DB))
  16.                 Conn.Open()
  17.                 Command = New OleDbCommand(SQL, Conn)
  18.                 DataRead = Command.ExecuteReader(CommandBehavior.SingleRow)
  19.             Catch ex As Exception
  20.                 Response.Write(ex)
  21.                 lblBadLogIn.Text = "Wrong UserName/Password Combonation."
  22.                 Exit Sub
  23.             End Try
  24.         Else
  25.             lblConnectionError.Text = "<div align='center'><b>Error:</b> There is already a connection to the database.<br>Please close the connection and try again.</div>"
  26.         End If
  27.         Response.Redirect("index.aspx")
  28.     End Sub
  29.  
  30.    
  31.  
  32.     Private Sub btnLogIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogIn.Click
  33.         Dim UserName As String, Password As String
  34.         UserName = Request.Form("txtUserName")
  35.         Password = Request.Form("txtPassword")
  36.         ExecuteQuery("SELECT * FROM tblLogIn WHERE UserName= '" & UserName & "' AND Password= '" & Password & "'", "Test")
  37.     End Sub
  38. End Class



What i want to do is check to see how many rows were returned and if the number of rows returned was <=0 then send the message "bad login" or something like that. But i never get a chance to check the rows becasue it just errors out.

I currently am just setting the bad login text when i get the error and this works, but it doesn't seem right because I could get an error for a totaly different reason and wouldn't know it.

I wanted to use If DataRead.FieldCount <= 0 Then etc...

Any idea why i'm getting this error?

Thanks in advance!