Reputation: 219
i try to send mail in asp.net like this
code
if ( DropDownListcontrol!= null)
{
if (DropDownListcontrol.SelectedValue == true)
{
//get Current EMAIL_ID from the DataKey
string emailId =
((Label)Repeater2.Items[i].FindControl("Label2")).Text;
//write code to send mail
SendEmailUsingGmail(emailId);
dt.Clear();
dt.Dispose();
}
else if (DropDownListcontrol.SelectedValue == false)
{
}
}
but here occur in this line
DropDownListcontrol.SelectedValue == false
ERROR: Operator '==' cannot be applied to operands of type 'string' and 'bool'
how i solve this?
Upvotes: 1
Views: 20294
Reputation: 861
you should have been more specific about what your values in dropdown list are.
anyways, my suggestions:
if values in your dropdown list are "true" and "false" , then you have to use:
if(DropDownListcontrol.SelectedValue == "true")
{
}
if you are just checking whether a user has selected some value in the dropdown list then you have to use:
if(DropdownListControl.SelectedIndex == -1)
{
}
Upvotes: 1
Reputation: 98
The DropDownList SelectedValue property is a string, so you won't be able to compare the string to a bool value - which is what the error message is (perhaps not very clearly) saying.
Are you trying to tell if there is a selected value in the DropDownList and, if there is, then you want to perform some action (like send email)?
Or, does the DropDownList contain some value that you want to use to determine if an email should be sent? ie, "true" and "false"?
If you are trying to tell if there is a selected value in the DropDownList, then check to see if the SelectedValue is null. However, it might be that there is always an item selected in the list.
Upvotes: 1
Reputation: 44439
DropDownListcontrol.SelectedValue
This returns a string. You cannot apply a boolean operator to it.
I don't immediatly see the need for an if
statement. What is you want to do exactly?
If your values in your dropdownlist are actually "true" and "false", just use a literal string comparison:
if(DropDownListcontrol.SelectedValue == "true")
Upvotes: 2
Reputation: 28991
Does the dropdown contain string values representing true/false (e.g. "True", "False")? Then you have to convert them to booleans (see: Boolean.TryParse
), or just do a string comparison (e.g. SelectedValue.Equals("True")
).
Side note: if you have bool isFubar
, you never need to write if (isFubar == true)
. You can simply write if (isFubar)
.
Upvotes: 1
Reputation: 33306
Change it to this:
DropDownListcontrol.SelectedValue == "true"
Or cast your selected value to a bool.
Upvotes: 2