HEllo Everyone:
I am new in VB2005, with no programming experience. I am working on a project whith a search screen where I have multiples combo boxes, text boxes and so far everything is working the way I want it, meaning I can search using any of the combo boxes. The problem I am having is that I don't know how to write an Error Handler. I want to be able that if my criteria doesn't match to display a message "No Record Found". Please help
BK.
Here is copy of the code that I have for my serach screen.
*****************************************************
Private Sub get_searchtable_records()
Dim conn As New SqlClient.SqlConnection
Dim searchtable_string As String = ""
Dim searchtable_string2 As String = ""
'Dim strWhereCondition As String
Dim testsearch_connection As New SqlClient.SqlConnection("Persist Security Info=False;Integrated Security=SSPI;database=SupportDesk;server=local")
searchtable_string = "SELECT * FROM CallLog" & trimstring
searchtable_string2 = "" & trimstring
'This is to search using the Priority Combo Box
If Trim(CmbSrPriority.Text) <> "" Then
If searchtable_string2 = "" Then
searchtable_string2 = " WHERE Priorityid = '" & CmbSrPriority.Text & "' "
Else
searchtable_string2 = searchtable_string2 & " AND Priorityid = '" & CmbSrPriority.Text & "' "
End If
End If
'*************************
'This is to search using the Status Combo Box
If Trim(CmbSrStatus.Text) <> "" Then
If searchtable_string2 = "" Then
searchtable_string2 = " WHERE Statusid = '" & CmbSrStatus.Text & "' "
Else
searchtable_string2 = searchtable_string2 & " AND Statusid = '" & CmbSrStatus.Text & "' "
End If
End If
'*************************
'This is to search using the District Combo Box
If Trim(CmbSrDistrict.Text) <> "" Then
If searchtable_string2 = "" Then
searchtable_string2 = " WHERE DistrictNumberID = '" & CmbSrDistrict.Text & "' "
Else
searchtable_string2 = searchtable_string2 & " AND DistrictNumberID = '" & CmbSrDistrict.Text & "' "
End If
End If
'*************************
'This is to search using the Application Combo Box
If Trim(CmbSrApplication.Text) <> "" Then
If searchtable_string2 = "" Then
searchtable_string2 = " WHERE Applicationid = '" & CmbSrApplication.Text & "' "
Else
searchtable_string2 = searchtable_string2 & " AND Applicationid = '" & CmbSrApplication.Text & "' "
End If
End If
'*************************
'This is to search using the Group Combo Box
If Trim(CmbSrGroup.Text) <> "" Then
If searchtable_string2 = "" Then
searchtable_string2 = " WHERE Groupid = '" & CmbSrGroup.Text & "' "
Else
searchtable_string2 = searchtable_string2 & " AND Groupid = '" & CmbSrGroup.Text & "' "
End If
End If
'*************************
'This is to search using the Assignee Combo Box
If Trim(CmbSrAssignee.Text) <> "" Then
If searchtable_string2 = "" Then
searchtable_string2 = " WHERE Assigneeid = '" & CmbSrAssignee.Text & "' "
Else
searchtable_string2 = searchtable_string2 & " AND Assigneeid = '" & CmbSrAssignee.Text & "' "
End If
End If
'*********************************
'This is to search using the Call Description Combo Box
If Trim(TxtSrCallDescription.Text) <> "" Then
If searchtable_string2 = "" Then
searchtable_string2 = " WHERE RichTextBox1 LIKE '%" & TxtSrCallDescription.Text & "%' "
Else
searchtable_string2 = searchtable_string2 & " AND RichTextBox1 LIKE '%" & TxtSrCallDescription.Text & "%' "
End If
End If
'*************************
'This is to search using the Call Resolution Combo Box
If Trim(TxtSrCallResolution.Text) <> "" Then
If searchtable_string2 = "" Then
searchtable_string2 = " WHERE RichTextBox2 LIKE '%" & TxtSrCallResolution.Text & "%' "
Else
searchtable_string2 = searchtable_string2 & " AND RichTextBox2 LIKE '%" & TxtSrCallResolution.Text & "%' "
End If
End If
'*************************
searchtable_string = searchtable_string & searchtable_string2
searchtable_command = New SqlClient.SqlCommand(searchtable_string, testsearch_connection)
searchtable_dataadapter = New SqlClient.SqlDataAdapter(searchtable_command)
If searchtable_command.Connection.State = ConnectionState.Closed Then
searchtable_command.Connection.Open()
End If
searchtable_table.Clear()
'clear table to not show duplicates after refilling
searchtable_dataadapter.Fill(searchtable_table)
searchtable_command.Connection.Close()
Me.DataGridView1.DataSource = searchtable_table
End Sub
JUliusPosted Jun 6, 2008, 10:50 AM
Thank you very much Scott. I'll give a try.
Julius
Scott LyslePosted Jun 5, 2008, 9:12 PM
It is pretty straight forward; wrap code that may an error inside a try-catch block and handle the exceptions within each catch. Here is a simple example:
First, you can test for certain condition that would otherwise produce an error and handle them before they get to that point. Below, I have two textboxes that both have to contain a value in order to perform division on the two values. I just checked for an empty string in both text boxes (you'd actually also want to make sure they contained numbers and not text or symbols) and if either is emtpy, I tell the user to enter both values and then I leave the subroutine. When I actually do the division, I wrap the code that does the work inside of a try-catch block and, for the sake of an example, I catch null reference and standard exceptions and handle them by telling the user that something is wrong by displaying the trapped error's message.
'status message
If (txtVal1.Text = String.Empty Or txtVal2.Text = String.Empty) Then
MessageBox.Show("Enter both values to continue.", "Invalid Entry")
Return
End If
Dim dVal1 As Double = Convert.ToDouble(txtVal1.Text)
Dim dVal2 As Double = Convert.ToDouble(txtVal2.Text)
' trap errors with a try-catch block
Try
Dim dResult As Double
dResult = dVal1 / dVal2
' status
MessageBox.Show(dResult.ToString(), "The answer")
Catch zex As NullReferenceException
MessageBox.Show(zex.Message, "Null Reference")
Catch ex As Exception
MessageBox.Show(ex.Message, "Standard Exception")
Finally ' optional
' will always execute one you have entered the
' try catch block
MessageBox.Show("Enter two new values and try again", "Next")
txtVal1.Text = String.Empty
txtVal2.Text = String.Empty
End Try
Here is a shorter version; in this one, I declare a hashtable and try to access it before creating a new instance of it; this will result in a null reference exception.
Try
Dim c As Hashtable()
c.Length.ToString()
Catch zex As NullReferenceException
MessageBox.Show(zex.Message, "Null Reference")
Catch ex As Exception
MessageBox.Show(ex.Message, "Standard Exception")
End Try