Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello, I am testing on three different conditions;
First one is the selectedIndex of the dropDown1==2 and the TextBox is empty
Second is the selectedIndex of the DropDown1==2 and the TextBox is not empty
Last one is the selectedIndex of the dropDown ==3
When executing It goes directly to the Last condition and doesnt even test the first ones.

This is my code
C#
protected void btnValider_Click(object sender, EventArgs e)
{
    if (cmbStatut.SelectedIndex == 2)
    {
        if (txtNvSt.Text != null)
        {
            con.charger("update Materiel set reparation= NULL where serviceTag='" + txtServiceTag.Text + "'", false);
            Session["ST"] = txtNvSt.Text;
            Response.Redirect("NouveauMAt.aspx");
        }
        else
            if (txtNvSt.Text == null)
            {
                con.charger("insert into Stocker values('1', '" + txtServiceTag.Text + "')", false);
            }
    }
    else
    {
        con.charger("update Materiel set reparation = NULL, idEmplacement = NULL where serviceTag='" + txtServiceTag.Text + "'", false);
        Response.Redirect("StockHS.aspx");
    }
}
Posted
Updated 1-Jul-14 5:28am
v2
Comments
[no name] 1-Jul-14 11:18am    
It's will go to your last condition if your selected index is anything except 2
Text will never be null so those condition will never be satisfied.
PIEBALDconsult 1-Jul-14 11:24am    
It's doing exactly what you told it to do. Your code doesn't match your spec.

The property Text is never null. It could be empty, could contain only blank spaces, and so on. If you need to test if some entered data is empty, you can do this
C#
if (txtNvSt.Text == string.Empty) //...

or
C#
if (string.IsNullOrEmpty(txtNvSt.Text)) //...

Also, you may want to consider something like
C#
if (txtNvSt.Text.Trim() != string.Empty) //...

The first and last fragment essentially based on assumption that txtNvSt.Text is never null. In other cases where null is possible you should also check for null.

To see what part of your code is really executed, in case of even slightest doubt, always use the debugger. It will also tell you which part of condition is true or false. :-)

—SA
 
Share this answer
 
Comments
Debabrata_Das 1-Jul-14 11:41am    
my 5!
Your string is probably not null, but empty.
Use String.IsNullOrEmpty()

C#
protected void btnValider_Click(object sender, EventArgs e)
{
  if (cmbStatut.SelectedIndex == 2)
  {
    if (!String.IsNullOrEmpty(txtNvSt.Text))
    { 
       con.charger("update Materiel set reparation= NULL where serviceTag='" + txtServiceTag.Text + "'", false);
       Session["ST"] = txtNvSt.Text;
       Response.Redirect("NouveauMAt.aspx");
     }
     else
     { 
       con.charger("insert into Stocker values('1', '" + txtServiceTag.Text + "')", false);
     }
   }
   else if (cmbStatut.SelectedIndex == 3)
   {
     con.charger("update Materiel set reparation = NULL, idEmplacement = NULL where serviceTag='" + txtServiceTag.Text + "'", false);
     Response.Redirect("StockHS.aspx");
   }
   else
  {
      // Unsupported case
  }
}
 
Share this answer
 
Comments
Member 10889990 2-Jul-14 5:34am    
Thanks alot it worked
In addition to the other solutions provided by SA and George, you may use switch-case statements instead of if-else which looks more structured:
C#
protected void btnValider_Click(object sender, EventArgs e)
{
    switch (cmbStatut.SelectedIndex)
    {
        case 2:
            if (!string.IsNullOrEmpty(txtNvSt.Text))
            {
                con.charger("update Materiel set reparation= NULL where serviceTag='"
                            + txtServiceTag.Text + "'", false);
                Session["ST"] = txtNvSt.Text;
                Response.Redirect("NouveauMAt.aspx");
            }
            else
            {
                con.charger("insert into Stocker values('1', '"
                            + txtServiceTag.Text + "')", false);
            }

            break;

        case 3:
            con.charger("update Materiel set reparation = NULL, idEmplacement = NULL where serviceTag='"
                        + txtServiceTag.Text + "'", false);
            Response.Redirect("StockHS.aspx");

            break;

        default:
            // Write code here to handle any other value
    }
}
 
Share this answer
 
Comments
Member 10889990 2-Jul-14 5:33am    
Thank you so much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900