Solved

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

Posted on 2016-08-30
11
61 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.Net remove carriage returns, line feeds and tabbs 5 38
Select coumns form Data table? 3 27
I need to clear List Box in MVVM panel. 15 27
Javascript function 3 23
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 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