HEEN
HEEN

Reputation: 4721

Dropdown value coming blank but still going in IF condition

While submitting a form, I am not selecting any value from the dropdownlist and I am checking condition as

if (Request.Form["CmbNextUser"] != Session["UserId"].ToString() || Request.Form["CmbNextUser"] != null || Request.Form["CmbNextUser"] != "")
        {
            TransferMail();
        }

So, while debugging my value comes as like below.

IMG

The value is coming blank but still it is going in the IF condition

Upvotes: 0

Views: 68

Answers (2)

Milney
Milney

Reputation: 6417

Your IF conditions have logical ORs between them. This means if any one of them are met, the whole thing will execute. Did you mean to use && (and) instead of || (or).

Upvotes: 0

Jaydip Jadhav
Jaydip Jadhav

Reputation: 12309

  1. Condition one is causing for this since you are comparing value

     Request.Form["CmbNextUser"] != Session["UserId"].ToString() 
    

    i.e "" != Session["UserId"].ToString() which is evaluated as ture thats why it it executing if block.

  2. Change code like this

    if (!String.IsNullOrEmpty(Request.Form["CmbNextUser"]) || Request.Form["CmbNextUser"] != Session["UserId"].ToString() )
    {
        TransferMail();
    }
    

Upvotes: 1

Related Questions