|
-
Dec 17th, 2003, 02:51 PM
#1
Thread Starter
Hyperactive Member
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:
Try
cnn.ConnectionString = (.........)
cnn.Open()
cmd = cnn.CreateCommand
cmd.CommandText = "SELECT Phase FROM JCPM WHERE VbPhase=" _
& "'" & txtPC.Text & "'"
dr = cmd.ExecuteReader
While dr.Read()
txtPhaseCode.Text = dr("Phase")
End While
dr.Close()
cnn.Close()
Catch ex As Exception
MsgBox("Error: " & ex.Source & ": " & ex.Message, _
MsgBoxStyle.OKOnly, "Job Name Error")
End Try
Last edited by BukHix; Dec 19th, 2003 at 12:38 PM.
-
Dec 17th, 2003, 03:34 PM
#2
Junior Member
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
-
Dec 17th, 2003, 03:52 PM
#3
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:
Try
cnn.ConnectionString = (.........)
cmd = cnn.CreateCommand
cmd.CommandText = "SELECT Phase FROM JCPM WHERE VbPhase=" _
& "'" & txtPC.Text & "'"
cnn.Open()
Dim results As Object = cmd.ExecuteScalar
cnn.Close()
If result Is Nothing Then
MsgBox("No records matched your query!")
Else
txtPhaseCode.Text = result
End If
Catch ex As Exception
MsgBox("Error: " & ex.Source & ": " & ex.Message, _
MsgBoxStyle.OKOnly, "Job Name Error")
End Try
-
Dec 19th, 2003, 12:41 PM
#4
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|