it's me again
i had a code that searches a record based on user option.And i had the idea of creating a single function that
accepts a query statement and execute and return it to the caller but i can't come up with anything(not make the code bulky).i have 3 d/t search functions with the same line of code,the only thing that is d/t is the string statement.
i hope you will get my idea
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ComboBox1.SelectedItem = "" Then
MsgBox("Please choose what you want to search", vbInformation, "Bridge Asset Management")
ComboBox1.Focus()
ElseIf ComboBox1.SelectedItem = "Bridge Number" Then
If TextBox1.Text <> "" Then
search()
Else
MsgBox("Please enter the bridge number to search", vbInformation, "Bridge Asset Management")
End If
ElseIf ComboBox1.SelectedItem = "Bridge Name" Then
If TextBox1.Text <> "" Then
search1()
Else
MsgBox("Please enter the bridge name to search", vbInformation, "Bridge Asset Management")
End If
ElseIf ComboBox1.SelectedItem = "Section" Then
If ComboBox2.SelectedItem <> "" Then
search2()
Else
MsgBox("Please choose the section to search", vbInformation, "Bridge Asset Management")
End If
ElseIf ComboBox1.SelectedItem = "Road Segment" Then
If ComboBox3.SelectedItem <> "" Then
search3()
Else
MsgBox("Please choose the section to search", vbInformation, "Bridge Asset Management")
End If
End If
End Sub
Public Sub search()
Dim conn As OleDbConnection = GetDbConnection()
bridgenumber = TextBox1.Text.ToString()
' Select records.
comm = New OleDbCommand("SELECT * FROM AdigratT WHERE [Bridge Number]=@bridgenumber", conn)
comm.Parameters.AddWithValue("@bridgenumber", bridgenumber)
Dim data_reader As OleDbDataReader = comm.ExecuteReader()
If data_reader.HasRows = True Then
ListView1.Items.Clear()
Do While data_reader.Read()
Dim new_item As New ListViewItem(data_reader.Item("Bridge Number").ToString)
new_item.SubItems.Add(data_reader.Item("Bridge Name").ToString)
new_item.SubItems.Add(data_reader.Item("District Name").ToString)
new_item.SubItems.Add(data_reader.Item("Section Name").ToString)
new_item.SubItems.Add(data_reader.Item("Road Segment Name").ToString)
new_item.SubItems.Add(data_reader.Item("Bridge Type").ToString)
new_item.SubItems.Add(data_reader.Item("Bridge Span").ToString)
new_item.SubItems.Add(data_reader.Item("Bridge Condition").ToString)
new_item.SubItems.Add(data_reader.Item("Acquisition Year").ToString)
new_item.SubItems.Add(data_reader.Item("Evaluation Year").ToString)
new_item.SubItems.Add(data_reader.Item("End of Design Life Year").ToString)
new_item.SubItems.Add(data_reader.Item("Bridge Asset Value").ToString)
ListView1.Items.Add(new_item)
Loop
Else
MsgBox("There is no record in the database", vbCritical, "Bridge Asset Management")
ListView1.Items.Clear()
End If
' Close the connection.
data_reader.Close()
data_reader = Nothing
comm.Dispose()
comm = Nothing
conn.Close()
conn.Dispose()
conn = Nothing
End Sub