Writing Nested If condition using C# in ASP.Net

Last Reply on Jul 21, 2016 09:52 AM By Shashikant

Posted on Jul 21, 2016 07:45 AM

hi

I use if condition in behind code:

if (Ch3D.Checked == true)
{
    _cmd.Parameters.AddWithValue("@GenrePre", "1");

}
else
{
    if (ChOscar.Checked == true)
    {
        _cmd.Parameters.AddWithValue("@GenrePre", "2");

    }
    else
    {

        if (ChDobleF.Checked == true)
        {
            _cmd.Parameters.AddWithValue("@GenrePre", "3");

        }
    }
}

as you see I used neasted if else condition is there other code that I use it instead of neasted if else contition?

best regards

neda

 

Posted on Jul 21, 2016 07:44 AM
nedash says:
if (Ch3D.Checked == true)
            {
                _cmd.Parameters.AddWithValue("@GenrePre", "1");
 
            }
            else
            {
                if (ChOscar.Checked == true)
                {
                    _cmd.Parameters.AddWithValue("@GenrePre", "2");
 
                }
                else {
 
                    if (ChDobleF.Checked == true)
                    {
                        _cmd.Parameters.AddWithValue("@GenrePre", "3");
 
                    }
                }
            }

 Please refer below code.

if (Ch3D.Checked == true)
{
    _cmd.Parameters.AddWithValue("@GenrePre", "1");
}
if (ChOscar.Checked == true)
{                 
    _cmd.Parameters.AddWithValue("@GenrePre", "2");
}
if (ChDobleF.Checked == true)
{                    
     _cmd.Parameters.AddWithValue("@GenrePre", "3");
}

Posted on Jul 21, 2016 09:49 AM

but here maybe users select 2 checkbox i.e:

ch3D and chOscar I want if they select ch3d it doesn't check other checkbox and insert "1" in @Genrepre and if they don't select ch3d after that it checked other condition...


Posted on Jul 21, 2016 09:52 AM
nedash says:
if (Ch3D.Checked == true)
            {
                _cmd.Parameters.AddWithValue("@GenrePre", "1");
 
            }
            else
            {
                if (ChOscar.Checked == true)
                {
                    _cmd.Parameters.AddWithValue("@GenrePre", "2");
 
                }
                else {
 
                    if (ChDobleF.Checked == true)
                    {
                        _cmd.Parameters.AddWithValue("@GenrePre", "3");
 
                    }
                }
            }

Hi,

as per your condition your code is proper.