Get All or Specific Data From the Database Using Single Inline Query

Sometimes a developer must show all the rows or just some rows on the same event and for that, generally developers use:

  • A Stored Procedure and write a if else condition in it.
  • Write the if else condition at the code level and use a different query in both conditions.

Like

  1. //if( Filter)  
  2. //Fetch Filtered data via Query1  
  3. //else means All  
  4. // Fetch All data via Query2 

But I will tell you how you can easily do it without using a stored procedure and conditions at the code level.

The following are the details of the preceding procedure.

Step 1

  1. Create a table named "Department" with 2 columns, "Department_ID" and "Department_Name".

    Create a table

    Add some information for some Departments into it.
    1. insert into Department values(1,'IT')  
    2. insert into Department values(2,'Finance'
    And finally select the "Department" Table.

    Department

  2. Now create one more table named "Employee" with the 3 columns "Emp_ID", "Emp_Name" and "Department_ID".

    one more table

    Add some information for some Employees into it.
    1. insert into Employee values(101,'Sumit',1)  
    2. insert into Employee values(102,'Amit',1)  
    3. insert into Employee values(103,'Udit',1)  
    4. insert into Employee values(104,'Ram',2)  
    5. insert into Employee values(105,'Shyam',2) 
    And finally select the "Employee" Table.

    Employee

    Note: Here 3 employees belong to the "IT" Department and 2 employees belong to the "Finance" Department.

Step 2

  1. Create a new Empty Website named "Website1".

    new Empty Website

  2. And add the web form named "Deafult.aspx" to it.

    Deafult

  3. Add a "DropdownList", a button with the Text as "Submit" and a click event and a "Gridview" into the "Deafult.aspx" page.

    DropdownList

Step 3

Write the code on Page load event in the "!IsPostBack" section due to remove duplicate bind.

  1. Bind the data in to the "DataTable" from the database.
    1. string Constr = ConfigurationManager.ConnectionStrings["Con"].ToString();  
    2. SqlConnection con = new SqlConnection(Constr);  
    3. string sql = "select * from Department";  
    4. con.Open();  
    5. SqlCommand cmd = new SqlCommand(sql, con);  
    6. cmd.CommandType = CommandType.Text;  
    7. DataTable dt = new DataTable();  
    8. SqlDataAdapter ad = new SqlDataAdapter();  
    9. ad.SelectCommand = cmd;  
    10. ad.Fill(dt);  
    11. con.Close(); 
  2. Add the "DataTable" values into the "DropDownList" Control.
    1. DropDownList1.DataSource = dt;  
    2. DropDownList1.DataTextField = "Department_Name";  
    3. DropDownList1.DataValueField = "Department_ID";  
    4. DropDownList1.DataBind(); 
  3. Add an item into the "DropDownList" Control for "All" rows selection with it's key value.
    1. DropDownList1.Items.Add("Select All");  
    2. DropDownList1.Items[DropDownList1.Items.Count - 1].Value = "00"
    Control

  4. By this I will get the output like this.

    output

    Note: Here 1 and 2 are Department Ids that come from the "Department" Table and "00" is the value of the "Select All" Text.

Step 4

Write the code on button click event.

  1. Write the inline query as in the following:
    1. string sql = @"select * from employee where Department_ID='" + DropDownList1.SelectedItem.Value + "' or (Department_ID!='" + DropDownList1.SelectedItem.Value + "' and '" + DropDownList1.SelectedItem.Value + "'='00')"
    Note: Here "00" should be the key value of the "Select All" item of "DropDownlist1".

  2. Bind the data to the "DataTable" from the database on the basis of the selected value of "DropDownList1".
    1. string Constr = ConfigurationManager.ConnectionStrings["Con"].ToString();  
    2. SqlConnection con = new SqlConnection(Constr);  
    3. con.Open();  
    4. SqlCommand cmd = new SqlCommand(sql, con);  
    5. cmd.CommandType = CommandType.Text;  
    6. DataTable dt = new DataTable();  
    7. SqlDataAdapter ad = new SqlDataAdapter();  
    8. ad.SelectCommand = cmd;  
    9. ad.Fill(dt);  
    10. con.Close(); 
  3. Add the "DataTable" values into the "GridView" Control.
    1. GridView1.DataSource = dt;  
    2. GridView1.DataBind(); 
    DataTable

Step 5

If I run the page and select all 3 values of "DropDownList" respectively and press the Submit button.

  1. The "IT" Department shows 3 rows.

    Department shows

  2. The "Finance" Department shows 2 rows.

    Finance Department

  3. The "Select All" Department shows all 5 rows.

    Select All

Up Next
    Ebook Download
    View all
    Learn
    View all