Results 1 to 3 of 3

Thread: Error when no rows returned from SELECT statement

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2003
    Posts
    10

    Error when no rows returned from SELECT statement

    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!
    ?

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    For datareader object you use Read method that does two things:
    1-Checks if there is a row after current positon.
    2-If there is a row then moves to that row.
    So you should do:
    VB Code:
    1. If  DataRead.Read Then
    2. ' Some code Here
    3. End if
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2003
    Posts
    10
    Ok i figured out the problem... I was using

    DataRead = Command.ExecuteReader(CommandBehavior.SingleRow)

    and for some reason it was giving an error if no row was returned... but if i use

    DataRead = Command.ExecuteReader(CommandBehavior.CloseConnection)

    It works just fine.

    Thanks for the help though, it got me thinking!
    ?

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