Solved

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

Posted on 2016-08-30
11
52 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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