Results 1 to 4 of 4

Thread: Send message to user if Where clause is not found in DataReader (solved)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354

    Send message to user if Where clause is not found in DataReader (solved)

    How do I catch and send a message to the user if the text entered in the Where clause is not found among the records in the data reader?

    VB Code:
    1. Try
    2.     cnn.ConnectionString = (.........)
    3.     cnn.Open()
    4.     cmd = cnn.CreateCommand
    5.     cmd.CommandText = "SELECT Phase FROM JCPM WHERE VbPhase=" _
    6.             & "'" & txtPC.Text & "'"
    7.     dr = cmd.ExecuteReader
    8.         While dr.Read()
    9.             txtPhaseCode.Text = dr("Phase")
    10.         End While
    11.         dr.Close()
    12.         cnn.Close()
    13. Catch ex As Exception
    14.     MsgBox("Error: " & ex.Source & ": " & ex.Message, _
    15.             MsgBoxStyle.OKOnly, "Job Name Error")
    16. End Try
    Last edited by BukHix; Dec 19th, 2003 at 12:38 PM.

  2. #2
    Junior Member techmanbd's Avatar
    Join Date
    Dec 2003
    Location
    Studio City, CA
    Posts
    16
    one way is that if a record is not found from the where clause then there won't be any so you can put the if dr.hasrows like i did below
    Code:
    Try
    	cnn.ConnectionString = (.........)
    	cnn.Open()
    	cmd = cnn.CreateCommand
    	cmd.CommandText = "SELECT Phase FROM JCPM WHERE VbPhase=" _
            	& "'" & txtPC.Text & "'"
    	dr = cmd.ExecuteReader
    if dr.hasrows then
           	While dr.Read()
            	txtPhaseCode.Text = dr("Phase")
          	End While
    else
      'code if doesn't find anything
    end if
          	dr.Close()
          	cnn.Close()
    Catch ex As Exception
    	MsgBox("Error: " & ex.Source & ": " & ex.Message, _
            	MsgBoxStyle.OKOnly, "Job Name Error")
    End Try

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If you are only returning one result from one field like that then it is more efficent to use ExecuteScalar instead of ExecuteReader:
    VB Code:
    1. Try
    2.     cnn.ConnectionString = (.........)
    3.     cmd = cnn.CreateCommand
    4.     cmd.CommandText = "SELECT Phase FROM JCPM WHERE VbPhase=" _
    5.             & "'" & txtPC.Text & "'"
    6.     cnn.Open()
    7.     Dim results As Object = cmd.ExecuteScalar
    8.     cnn.Close()
    9.         If result Is Nothing Then
    10.                         MsgBox("No records matched your query!")
    11.                 Else
    12.                         txtPhaseCode.Text = result
    13.                 End If
    14. Catch ex As Exception
    15.     MsgBox("Error: " & ex.Source & ": " & ex.Message, _
    16.             MsgBoxStyle.OKOnly, "Job Name Error")
    17. End Try

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    Got it now. Once again I thank you for the help/

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