Skip to main content

How to search in asp.net with multiple search criteria

Hello friends today I am going to explain you how you can Get Data between two Date Range
                                                                     Or
How to search in asp.net with multiple search criteria
I have a requirement like need to get user details who belongs to particular country and hire date from some specific range for that I have written Code like as below  
                  


Step 1: Database Creation
I have created a database with structure below to complete my task ( you can get Complete Database script with Code)



Step2: Website setup
Now create a new web project and add one button, 2 TextBox, DropDownList and GridView on aspx page .This page will look like below Code  
<asp:Button ID="Button1" runat="server" Text="Click To Search" OnClick="Button1_Click" />
    <table>
        <tr>
            <td>
                Hire Date(From Date)
            </td>
            <td>
                <asp:TextBox ID="txtfromDate" ClientIDMode="Static" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                To Date
            </td>
            <td>
                <asp:TextBox ID="txtTodate" ClientIDMode="Static" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Select Country
            </td>
            <td>
                <asp:DropDownList ID="ddlcountry" runat="server">
                    <asp:ListItem>USA</asp:ListItem>
                    <asp:ListItem>UK</asp:ListItem>
                </asp:DropDownList>
            </td>
        </tr>
    </table>
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" PageSize="5"
            EmptyDataText="Now Record to Show Change Your Search." AllowPaging="true">
        </asp:GridView>




Step3: Logic for searching

For Search based on multiple search criteria we are going to use Sql between Operator and And Operator
The BETWEEN operator selects values within a range. The values can be numbers, text, or dates.
The AND operator displays a record if both the first condition AND the second condition are true.
Final query that I have written is below
String getState = "SELECT * from [Employees] where HireDate BETWEEN '" + fromdate + "' AND '" + Todate + "' AND Country= '" + country + "'";
  
In this query fromdate and Todate are variable those value comes from textbox and country Values from Dropdown






You can Download Code from here http://1drv.ms/NlMy7A





Comments