?
Solved

how to get button click to search multiple fields in vb.net

Posted on 2016-08-30
11
Medium Priority
?
70 Views
Last Modified: 2016-08-30
I have 2 search criteria fields, with one search button. If Criteria 1(SearchFlightCode) is entered, then the button click need to search the database. If Criteria 2 (SearchFlightName) is entered, then the same button click need to search the same database based on Criteria 2.

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
         SearchFlightCode(tbFlightCode.Text)
         SearchFlightName(tbFlightName.Text)
         Me.Cursor = System.Windows.Forms.Cursors.WaitCursor 
        btnSearch.Enabled = False
        dgvFlightEnquiry.DataSource = Nothing

        Load_Data(ds)

        Me.Cursor = System.Windows.Forms.Cursors.Arrow
        btnSearch.Enabled = True

    End Sub

Open in new window

0
Comment
Question by:fadiel ras
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41775858
OK.  What is the question?
0
 

Author Comment

by:fadiel ras
ID: 41775859
Hi, how do I get the single button click to handle multiple search criteria being entered?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41775878
By doing what you are currently doing.  

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click

         SearchFlightCode(tbFlightCode.Text)     'Search on the code ??

         SearchFlightName(tbFlightName.Text)   'Search on the name  ??


         Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
        btnSearch.Enabled = False
        dgvFlightEnquiry.DataSource = Nothing

        Load_Data(ds)

        Me.Cursor = System.Windows.Forms.Cursors.Arrow
        btnSearch.Enabled = True

    End Sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41775879
You might want to post what the two SearchFlightXXXX functions do.
0
 

Author Comment

by:fadiel ras
ID: 41775883
thanks Andy.
so the search works 100% on "SearchFlightName(tbFlightName.Text)", but not on the other, yet my code dealing with the Sql part is basically identical (meaning, I've copied and pasted it in VB, simply changed the Sql Parameter).
Could there be anything else driving this behavior?
0
 

Author Comment

by:fadiel ras
ID: 41775889
The 2 Functions for my search

Private Sub SearchFlightName (FlightName As String)
        Try
            Dim strSql As String = ""

            ClearParm()

            AddParm("@FlightName", FlightName, DbType.String)

            strSql = " SELECT  FlightCode , FlightName , Active "
            strSql = strSql & " ,'N' RowModified "
            strSql = strSql & " From tblFlights "
            strSql = strSql & " where FlightName = @FlightName "
            ds = SelectDataSet(strSql)
        Catch ex As Exception
            MsgBox("Error finding Data", MsgBoxStyle.Exclamation, "Load Error - Test Data")
        End Try
    End Sub


    Private Sub SearchFlightCode (FlightCode As String)
        Try
            Dim strSql As String = ""

            ClearParm()

            AddParm("@FlightCode", FlightCode, DbType.String)

            strSql = " SELECT  FlightCode , FlightName , Active "
            strSql = strSql & " ,'N' RowModified "
            strSql = strSql & " From tblFlights "
            strSql = strSql & " where FlightCode = @FlightCode "
            ds = SelectDataSet(strSql)
        Catch ex As Exception
            MsgBox("Error finding Data", MsgBoxStyle.Exclamation, "Load Error - Test Data")
        End Try
    End Sub
End Class

Open in new window

0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 2000 total points
ID: 41775890
As the SearchFlightName is the second call of the two are you just throwing away the results of the first search?

You might need a function rather like:
SearchFlight(tbFlightCode.Text, tbFlightName.Text)

and inside that function code like (air code to show the logic)
if tbFlightCode.Text = "" then
  search on name only
else if tbFlightName.Text = "" then
  search on code only
else
  search on both name and code
end if
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41775894
the two search functions.  Yes,  you are throwing the first search results away.  See my previous comment for what you should be doing.
0
 

Author Comment

by:fadiel ras
ID: 41775909
thanks, trying it and revert asap
0
 

Author Comment

by:fadiel ras
ID: 41775929
Thank you so much Andy. It worked!

The amended "Button Click" code as suggested by Andy

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click

        SearchEnquiry(tbRPHFlightCode.Text, tbRPHFlightName.Text)

        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
        btnSearch.Enabled = False

        dgvFlightEnquiry.DataSource = Nothing
        Load_Data(ds)

        Me.Cursor = System.Windows.Forms.Cursors.Arrow
        btnSearch.Enabled = True
    End Sub

Open in new window


The Amended code to include the IF , THEN

    Private Sub SearchEnquiry(FlightCode As String, FlightName As String)
        Try
            Dim strSql As String = ""
            ClearParm()

            AddParm("@FlightCode", FlightCode, DbType.String)
            AddParm("@FlightName", FlightName, DbType.String)
            If tbRPHFlightName.Text = "" Then
                strSql = " SELECT  FlightCode , FlightName , Active "
                strSql = strSql & " ,'N' RowModified "
                strSql = strSql & " From tblFlightCodes "
                strSql = strSql & " where FlightCode = @FlightCode "
                ds = SelectDataSet(strSql)
            End If
            If tbRPHFlightCode.Text = "" Then
                strSql = " SELECT  FlightCode , FlightName , Active "
                strSql = strSql & " ,'N' RowModified "
                strSql = strSql & " From tblFlightCodes "
                strSql = strSql & " where FlightName = @FlightName "
                ds = SelectDataSet(strSql)
            End If
        Catch ex As Exception
            MsgBox("Error finding Data", MsgBoxStyle.Exclamation, "Load Error - Test Data")
        End Try
    End Sub

Open in new window

0
 

Author Closing Comment

by:fadiel ras
ID: 41775930
Thanks Andy. you certainly saved me hours, as I already wasted a few prior to asking for assistance.
0

Featured Post

Docker-Compose to Simplify Multi-Container Builds

Our veteran DevOps Author takes you through how to build a multi-container environment, managed with a single utility in order to simplify your deployments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question