|
-
Oct 26th, 2006, 12:41 PM
#1
Thread Starter
Lively Member
[RESOLVED] SQL Quiery Try/Catch Question
Hello community, I have a block of code i wish to place a try statment around. I basicaly want to catch the SQL statment in case it returns an item not found. Im fairly new to VB and have only used try catch statments during some tutorial material. please no flaming on the noobish layout and syntax.
Here is the code, can some one show me where i would but the try/catch??
VB Code:
Private Sub Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCriteriaSearch.Click
Dim serialSelect As String = "equipment_serial_number"
Dim cfSelect As String = "equipment_cf_tag"
Dim sqlColumn As String
Dim searchCriteria As String
If cboCriteria.SelectedIndex = 0 Then
sqlColumn = serialSelect
Else
sqlColumn = cfSelect
End If
searchCriteria = txtCriteriaSearch.Text.ToUpper
Dim mySqlConnection As SqlConnection = New SqlConnection(getConnectionString)
Dim mySqlCommand As SqlCommand = New SqlCommand()
Dim mySqlReader As SqlDataReader
mySqlConnection.Open()
mySqlCommand.Connection = mySqlConnection
mySqlCommand.CommandText = ( _
"SELECT EQ_Equipment.equipment_serial_number, " & _
"EQ_Equipment.equipment_cf_tag, " & _
"EQ_Equipment.equipment_description, " & _
"EQ_Equipment.equipment_warranty, " & _
"EQ_Equipment.item_class_id, " & _
"EQ_Equipment.location_id, " & _
"EQ_Equipment.status_id, " & _
"EQ_Equipment.user_id " & _
"FROM EQ_Equipment " & _
"WHERE " & sqlColumn & " = '" & searchCriteria & "'")
mySqlReader = mySqlCommand.ExecuteReader()
While mySqlReader.Read()
txtSerialNumber.Text = mySqlReader.GetString(0)
txtTag.Text = mySqlReader.GetString(1)
txtNotes.Text = mySqlReader.GetString(2)
dtpWarranty.Value = mySqlReader.GetDateTime(3)
cboClassType.SelectedValue = mySqlReader.GetInt32(4)
cboLocation.SelectedValue = mySqlReader.GetInt32(5)
cboItemStatus.SelectedValue = mySqlReader.GetInt32(6)
cboUserList.SelectedValue = mySqlReader.GetInt32(7)
End While
mySqlConnection.Close()
variableUpdate()
End Sub
-
Oct 26th, 2006, 12:45 PM
#2
Re: SQL Quiery Try/Catch Question
When the WHERE clause finds no rows it does not return an error.
It simply has no data rows.
All the metadata - column count - column definitions - column names come back from SQL.
Simply no rows.
If you step through that code in debug mode doesn't it simply not enter the loop when you specify a SERIAL NUMBER that does not match?
-
Oct 26th, 2006, 02:02 PM
#3
Thread Starter
Lively Member
Re: SQL Quiery Try/Catch Question
Yes your are correct. I guess i was concerned about actualy throwing up an exception to inform the user that nothing was returned, thus indicating an empty search due to the value not existing.
-
Oct 26th, 2006, 02:20 PM
#4
Re: SQL Quiery Try/Catch Question
Maybe like this:
VB Code:
mySqlReader = mySqlCommand.ExecuteReader()
If mySQLReader.HasRows() then
While mySqlReader.Read()
txtSerialNumber.Text = mySqlReader.GetString(0)
txtTag.Text = mySqlReader.GetString(1)
txtNotes.Text = mySqlReader.GetString(2)
dtpWarranty.Value = mySqlReader.GetDateTime(3)
cboClassType.SelectedValue = mySqlReader.GetInt32(4)
cboLocation.SelectedValue = mySqlReader.GetInt32(5)
cboItemStatus.SelectedValue = mySqlReader.GetInt32(6)
cboUserList.SelectedValue = mySqlReader.GetInt32(7)
End While
else
messagebox.show("No Data found that matched the search criteria.","No Data")
end if
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Oct 26th, 2006, 02:30 PM
#5
Re: SQL Quiery Try/Catch Question
@GM - good stuff - I was just going into HELP to find out what the syntax was!
-
Oct 26th, 2006, 02:34 PM
#6
Re: SQL Quiery Try/Catch Question
you should also wrap up most of that routine in try/catch blocks anyway. There is any number of reasons an error you aren't expecting could occur. A prime example is if the database is offline when someone tries to access it.
This will also allow you to clean up and dispose of your DB objects in a finally block of the try/catch
-
Oct 26th, 2006, 02:34 PM
#7
Re: SQL Quiery Try/Catch Question
I've started using that in all calls for my datareader. I pass a SQL statement to a class that returns a type of datareader then see if there are any rows in the returned item.
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Oct 26th, 2006, 02:41 PM
#8
Thread Starter
Lively Member
Re: SQL Quiery Try/Catch Question
Worked like a champ!! I did not know about the method "HasRows" Thanx! and i also added the Try catch for extra measure. I tested it and it works perfect.
VB Code:
Try
mySqlReader = mySqlCommand.ExecuteReader()
If mySqlReader.HasRows Then
While mySqlReader.Read()
txtSerialNumber.Text = mySqlReader.GetString(0)
txtTag.Text = mySqlReader.GetString(1)
txtNotes.Text = mySqlReader.GetString(2)
dtpWarranty.Value = mySqlReader.GetDateTime(3)
cboClassType.SelectedValue = mySqlReader.GetInt32(4)
cboLocation.SelectedValue = mySqlReader.GetInt32(5)
cboItemStatus.SelectedValue = mySqlReader.GetInt32(6)
cboUserList.SelectedValue = mySqlReader.GetInt32(7)
End While
Else
MessageBox.Show("No Item found. Check Serial number or CFTag")
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
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
|