i have written this in else condition ..can anyone please check this...

else

            if (DDlDepartment.SelectedValue != null && DDLStaffid.SelectedValue != null)
            {
                con.Open();
                string str1 = DDlDepartment.SelectedValue;
                string str2 = DDLStaffid.SelectedValue;
                


SqlDataAdapter ad1 = new SqlDataAdapter("Select * from ManstaSalary where  Department='" + str1 + "'and StaffID= '" + str2 + "'", con);
                DataSet ds1 = new DataSet();
                ad1.Fill(ds1);

                GridView1.DataSource = ds1;
                GridView1.DataBind();
                con.Close();
            }

Edited 6 Years Ago by Nick Evan: Added code-tags

"If" condition is working but "else if" condition is not working..i am not understanding vats the problem is ..

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 ((DDlDepartment.SelectedValue != null) && (DDLStaffid.SelectedValue != null))
        {
            con.Open();
            string str1 = DDlDepartment.SelectedValue;
            string str2 = DDLStaffid.SelectedValue;
            //string sql1 = "Select * from ManstaSalary where Department='" + str1 + "'and StaffID='" + str2+ "'";


            SqlDataAdapter ad = new SqlDataAdapter("Select * from ManstaSalary where  Department= '" + str1 + "' and StaffID = '" + str2 + "'", con);
            DataSet ds = new DataSet();
            ad.Fill(ds);

            GridView1.DataSource = ds;
            GridView1.DataBind();
            con.Close();
        }
    }
}

Edited 6 Years Ago by Nick Evan: Added code-tags

Your second condition will never be true, you need to swap them around. The if statement starts at the first condition and works its way down until one matches. In your case if DDLDepartment.SelectedValue is not null it will match the first. The only way your else statement will be checked is if DDLDepartment.SelectedValue IS null, in which case the condition fails and the statement wont run.

If you swap them around, if both are not null it will match and run query. If DDLStaffid.SelectedValue is null then the first condition will fail and the second will be checked, running if DDLDepartment.SelectedValue is not null.

You should probably move your original "Select * from ManstaSalary" query down into a final else statement so if both conditions fail it loads all data.

Again, you could have avoided the if statements if you had taken the time to examine the flexible search query i showed you in your original post.

i have given as what you have said..but its not working.. will u please send me "if" condition in else part..

You have swapped the if and else conditions around?
What isnt working now?
Please post the revised snippet of code and highlight the part that is not working correctly.

i have given staffid as null.. i dnt know how to swap..help me

else if ((DDlDepartment.SelectedValue != null) && (DDLStaffid.SelectedValue != null))

Just switch the two blocks of code around:

if ((DDlDepartment.SelectedValue != null) && (DDLStaffid.SelectedValue != null))
{
   ...
}
else if (DDLDepartment.SelectedValue!=null)
{
   ...
}
else
{
   //default query
}

As i said, the if..else statement works top down. It will run the first block of code whose condition equates to true, so you should always start with the most specific conditions and move down the list getting less specific.

your code looks like mine? Have you debugged to check the values for DDLDepartment.SelectedValue and DDLStaffid.SelectedValue?

not getting what? I'm sorry, but can you try to be more descriptive with your answers. We can only help you if we know exactly what the problem is and without seeing your full code we only have your psots to guide us.

Insert your breakpoint and examine (Quick Watch) your drop down lists at the point where you are about to enter your if..else block.
Its possible tt SelectedValue isnt being set as you expect it to be. Are your dropdownlists populated manually or are they databound?

This article has been dead for over six months. Start a new discussion instead.