Results 1 to 8 of 8

Thread: [RESOLVED] SQL Quiery Try/Catch Question

  1. #1

    Thread Starter
    Lively Member smilbuta's Avatar
    Join Date
    Apr 2005
    Location
    Orlando
    Posts
    104

    Resolved [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:
    1. Private Sub Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCriteriaSearch.Click
    2.         Dim serialSelect As String = "equipment_serial_number"
    3.         Dim cfSelect As String = "equipment_cf_tag"
    4.         Dim sqlColumn As String
    5.         Dim searchCriteria As String
    6.  
    7.         If cboCriteria.SelectedIndex = 0 Then
    8.             sqlColumn = serialSelect
    9.         Else
    10.             sqlColumn = cfSelect
    11.         End If
    12.         searchCriteria = txtCriteriaSearch.Text.ToUpper
    13.  
    14.         Dim mySqlConnection As SqlConnection = New SqlConnection(getConnectionString)
    15.         Dim mySqlCommand As SqlCommand = New SqlCommand()
    16.         Dim mySqlReader As SqlDataReader
    17.         mySqlConnection.Open()
    18.         mySqlCommand.Connection = mySqlConnection
    19.         mySqlCommand.CommandText = ( _
    20.         "SELECT EQ_Equipment.equipment_serial_number, " & _
    21.         "EQ_Equipment.equipment_cf_tag, " & _
    22.         "EQ_Equipment.equipment_description, " & _
    23.         "EQ_Equipment.equipment_warranty, " & _
    24.         "EQ_Equipment.item_class_id, " & _
    25.         "EQ_Equipment.location_id, " & _
    26.         "EQ_Equipment.status_id, " & _
    27.         "EQ_Equipment.user_id " & _
    28.         "FROM EQ_Equipment " & _
    29.         "WHERE " & sqlColumn & " = '" & searchCriteria & "'")
    30.  
    31.         mySqlReader = mySqlCommand.ExecuteReader()
    32.         While mySqlReader.Read()
    33.             txtSerialNumber.Text = mySqlReader.GetString(0)
    34.             txtTag.Text = mySqlReader.GetString(1)
    35.             txtNotes.Text = mySqlReader.GetString(2)
    36.             dtpWarranty.Value = mySqlReader.GetDateTime(3)
    37.             cboClassType.SelectedValue = mySqlReader.GetInt32(4)
    38.             cboLocation.SelectedValue = mySqlReader.GetInt32(5)
    39.             cboItemStatus.SelectedValue = mySqlReader.GetInt32(6)
    40.             cboUserList.SelectedValue = mySqlReader.GetInt32(7)
    41.         End While
    42.  
    43.         mySqlConnection.Close()
    44.         variableUpdate()
    45.     End Sub

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Lively Member smilbuta's Avatar
    Join Date
    Apr 2005
    Location
    Orlando
    Posts
    104

    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.

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL Quiery Try/Catch Question

    Maybe like this:

    VB Code:
    1. mySqlReader = mySqlCommand.ExecuteReader()
    2.         If mySQLReader.HasRows() then
    3.             While mySqlReader.Read()
    4.                 txtSerialNumber.Text = mySqlReader.GetString(0)
    5.                 txtTag.Text = mySqlReader.GetString(1)
    6.                 txtNotes.Text = mySqlReader.GetString(2)
    7.                 dtpWarranty.Value = mySqlReader.GetDateTime(3)
    8.                 cboClassType.SelectedValue = mySqlReader.GetInt32(4)
    9.                 cboLocation.SelectedValue = mySqlReader.GetInt32(5)
    10.                 cboItemStatus.SelectedValue = mySqlReader.GetInt32(6)
    11.                 cboUserList.SelectedValue = mySqlReader.GetInt32(7)
    12.             End While
    13.       else
    14.           messagebox.show("No Data found that matched the search criteria.","No Data")
    15.       end if
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Quiery Try/Catch Question

    @GM - good stuff - I was just going into HELP to find out what the syntax was!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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

  7. #7
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    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

  8. #8

    Thread Starter
    Lively Member smilbuta's Avatar
    Join Date
    Apr 2005
    Location
    Orlando
    Posts
    104

    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:
    1. Try
    2.             mySqlReader = mySqlCommand.ExecuteReader()
    3.             If mySqlReader.HasRows Then
    4.                 While mySqlReader.Read()
    5.                     txtSerialNumber.Text = mySqlReader.GetString(0)
    6.                     txtTag.Text = mySqlReader.GetString(1)
    7.                     txtNotes.Text = mySqlReader.GetString(2)
    8.                     dtpWarranty.Value = mySqlReader.GetDateTime(3)
    9.                     cboClassType.SelectedValue = mySqlReader.GetInt32(4)
    10.                     cboLocation.SelectedValue = mySqlReader.GetInt32(5)
    11.                     cboItemStatus.SelectedValue = mySqlReader.GetInt32(6)
    12.                     cboUserList.SelectedValue = mySqlReader.GetInt32(7)
    13.                 End While
    14.             Else
    15.                 MessageBox.Show("No Item found. Check Serial number or CFTag")
    16.             End If
    17.         Catch ex As Exception
    18.             MessageBox.Show(ex.Message)
    19.         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
  •  



Click Here to Expand Forum to Full Width