Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying this code to display the data of dropdownlist selected item in gridview it's working but only "if" condition is working and "else" condition is not. Any help would be appreciated.

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();
        }
    }
Posted
Updated 19-Jul-10 23:36pm
v4

1 solution

anuradha_d wrote:
if (DDlDepartment.SelectedValue != null)


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


If you notice the difference between two If statements are of DDLStaffid value. If you read about If[^] statement execution then the first condition that is satisfied is executed.

Based on the current implementation, your code would either go into first if or just end of if. Else-If is never going to be executed.
If you really want that condition, then just reverse it. Use the two conditioned if first and then one conditioned.

Hope you got it.


UPDATE:
You definitely need to read on If statements if that was your requirement from start.

Ok. Now, if you want both of them to execute then it can be done in multiple ways. For example
OPTION 1: just separate them!
C#
If(DDlDepartment.SelectedValue != null)
{
  // Do something 1
}

//followed by

if (DDlDepartment.SelectedValue != null && DDLStaffid.SelectedValue != null)
{
  // Do something 2
}

OPTION 2: Cascade the if's
C#
if (DDlDepartment.SelectedValue != null)
{
  // Do soemthing 1

  if (DDLStaffid.SelectedValue != null)
  {
    // Do something 2
  }
}


There can be other ways to handle using, other statements. For now, this should do for you.
 
Share this answer
 
v4
Comments
anuradha_d 20-Jul-10 6:31am    
As u said i've reversed the condition still first "if" stmt is working and second "if" is not.i want to execute both stmts.pls help me out.
Sandeep Mewara 20-Jul-10 7:04am    
You cannot execute both statement at the same time. Be clear on the conditions and execute accordingly.
anuradha_d 20-Jul-10 7:43am    
My requirement is to execute both "if" stmts.can u pls tel me how to execute both the stmts.
Sandeep Mewara 20-Jul-10 8:02am    
I have updated my answer. Hope this one is fine and acceptable to you!
anuradha_d 20-Jul-10 8:25am    
i have tried both the ways but am not getting the result only one "if" condition is working in both the options..pls help me in another way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900