Dhivya
Dhivya

Reputation: 21

I have Two Dropdownlist but Only one dropdownlist value should get insert into database

In this code Else Part alone working even if i select dropdownlist2.

string Varddl;

if(DropDownList2.SelectedItem.Text.ToString()== null )
{

   Varddl = DropDownList2.SelectedItem.Text;

}
else
{
   Varddl = DropDownList3.SelectedItem.Text;
}

string OIMSquery="Insert into tablename values('" + varddl + "')";

Upvotes: 1

Views: 123

Answers (2)

Adil
Adil

Reputation: 148110

You will never get null after calling ToString() and condition will never come true and always else part of if will execute, you probably need to compare it with empty string.

if(DropDownList2.SelectedItem.Text == "")

Upvotes: 1

Freelancer
Freelancer

Reputation: 9074

put condition like this>>

if(DropDownList2.SelectedItem== null )
{
    .
    .
}
else
{
}

It will work.

Upvotes: 1

Related Questions