0

i have 3 dropdownlist and 1 button.., when page load,its goes to last else part...if i select any one dropdown and click btn..its shows empty even if i have data in database... i think i made some mistake in selecteditem part...can u plz suggest some idea for this....

if (employee_name_ddl.SelectedItem.Text != "Select" & employee_paricularpro_ddl.SelectedItem.Text == "Select" & employee_status_ddl.SelectedItem.Text == "All Status")
            {
                ............
            }
            else if (employee_name_ddl.SelectedItem.Text == "Select" & employee_paricularpro_ddl.SelectedItem.Text != "Select" & employee_status_ddl.SelectedItem.Text == "All Status")
            {
                ............................
            }
            else if (employee_name_ddl.SelectedItem.Text == "Select" & employee_paricularpro_ddl.SelectedItem.Text == "Select" & employee_status_ddl.SelectedItem.Text != "All Status")
            {
               ............................
            }
            else if (employee_name_ddl.SelectedItem.Text != "Select" & employee_paricularpro_ddl.SelectedItem.Text != "Select" & employee_status_ddl.SelectedItem.Text == "All Status")
            {
                ...............................
            }
            else if (employee_name_ddl.SelectedItem.Text != "Select" & employee_paricularpro_ddl.SelectedItem.Text == "Select" & employee_status_ddl.SelectedItem.Text != "All Status")
            {
               ........................
            }
            else if (employee_name_ddl.SelectedItem.Text == "Select" & employee_paricularpro_ddl.SelectedItem.Text != "Select" & employee_status_ddl.SelectedItem.Text != "All Status")
            {
               ...................
            }
            else if (employee_name_ddl.SelectedItem.Text != "Select" & employee_paricularpro_ddl.SelectedItem.Text != "Select" & employee_status_ddl.SelectedItem.Text != "All Status")
            {
       .........................
            }
            else
            {
                ....................
            }
user1285783
  • 59
  • 2
  • 2
  • 7

1 Answers1

0

The default for ListControl.SelectedItem is null, hence you might get a NullReferenceException on the Text property somewhere when any DropDownList is not selected.

You're also using the bitwise comparison & instead of the correct logical &&.

Here's an example to show the difference of & and &&:

if(employee_name_ddl.SelectedItem != null && employee_name_ddl.SelectedItem.Text == "Select")
{
    // no exception whether or not employee_name_ddl.SelectedItem is null
}

if(employee_name_ddl.SelectedItem != null & employee_name_ddl.SelectedItem.Text == "Select")
{
    // exception when employee_name_ddl.SelectedItem is null,
    // because the second condition will be evaluated anyway
}

What is the diffrence between & and && operators in C#

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • hi @Tim Schmelter, thanks for replying me...shall i use if(employee_name_ddl.SelectedItem != null & employee_name_ddl.SelectedItem == null) – user1285783 Apr 17 '12 at 08:21
  • @user1285783: I thought it would be self-explanatory. No, you should use `if(employee_name_ddl.SelectedItem != null && employee_name_ddl.SelectedItem.Text == "Select")` (the first approach with the comment "// no exception") – Tim Schmelter Apr 17 '12 at 08:31
  • if i use && it will check both condition? – user1285783 Apr 17 '12 at 08:58