Solved

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

Posted on 2016-08-30
11
32 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
  • 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 500 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now