Iam Having three dropdown lists named Department,StaffID,StaffType,textbox Name and one submit button...If I select Department as BEMechanical and click the button i have to get all the details regarding that branch and if i select Department,StaffID,stafftype and name i should get the details of the selected one.Any one plz help me..i want this in C# Web Application

Recommended Answers

All 12 Replies

Use nullable search parameters in your sql query:

declare @id int = 1
declare @Name nvarchar(20) = null

SELECT * FROM Products
WHERE (@id is null OR Products.ProductID = @id) AND (@Name is null OR Products.Name = @Name

By making the search terms nullable and checking for null values you can retreive subsets based on which values are specified.
So if Name is set and id is null it will find all products that just match the name, but if i then set the id as well it will find all products that match the id AND the name.
Hope this helps :)

will u please tell me the "if" condition for the dropdownlist selected values to display them in grid view.

I didnt suggest an if statement.

Your choices are a) change the data retreived from database or b) filter the rows that are visible in the datagridview.

I showed you a way to do the first by altering your query so that it could include optional search parameters. Amend the sql query you use (either in stored procedure if you are making your app secure, or in code if you arent) so that it checks for the search values. You can set the search values when the dropdownlist SelectedIndexChanged event fires.

Please refrain from creating duplicate posts!
Have you tried using the sql statement i have given you?
You seem determined to use an if...else structure. But it is far more flexible to use nullable parameters as I have shown you as you only ever use a single query. Pass as many or as few of the values as you like.

i have tried with if else condition its working

If your code is now working, please mark this and the two duplicate posts as solved.

can u share u code here?

The code is available in the original post here. Which is precisely why we the forum rules state NOT to post duplicate questions. The answers get spread out and it makes it difficult and confusing for the solvers to help you and other forum readers to learn from :(

can u show the code using database MS Access?

Hi Kayfar, please avoid asking questions in existing threads unless they directly relate to the thread matter.
If you ened help with a MS Access connection, post a new thread. It keeps related code together and ensures you get help from those who can best answer your problem :)

am using SQL database
Else if condition is not working..

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Management_Staff_Salary_Details : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString);

        //string sql = "Select * from ManstaSalary";
        //SqlCommand cmd = new SqlCommand(sql, con);


   if (DDlDepartment.SelectedValue != null)
    {
      con.Open();
      string str = DDlDepartment.SelectedValue;
            //string sql = "Select * from ManstaSalary where Department= '" + str+"'" ;

      SqlDataAdapter ad = new SqlDataAdapter("Select * from ManstaSalary where Department= '" + str + "'", con);
      DataSet ds = new DataSet();
      ad.Fill(ds);
      GridView1.DataSource = ds;
      GridView1.DataBind();
      con.Close();
        }

      else if ( DDLStaffID.SelectedValue != null )
        {
      con.Open();
      //string str1 = DDlDepartment.SelectedValue;
      string str2 = DDLStaffID.SelectedValue;
      //string str3 = DDlStafftype.SelectedValue;
            //string sql1 = "Select * from ManstaSalary where Department='" + str1 + "'and StaffID='" + str2+ "'";


      SqlDataAdapter ad = new SqlDataAdapter("Select * from ManstaSalary where StaffID = '" + str2 + "'", con);
      DataSet ds = new DataSet();
      ad.Fill(ds);
      GridView1.DataSource = ds;
      GridView1.DataBind();
      con.Close();
        }
    }
}

GAH! Stop posting in both threads! Please just mark this thread (and any other copies) as solved and limit all posts to the original thread. It gets very confusing when you post back and forth like this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.