locked
Condition not being met RRS feed

  • Question

  • User-1499457942 posted

    Hi

     I have below code .  According to the condition it should go in else part but it is going in Add condition

    protected void gvwInflator_RowCommand(object sender, GridViewCommandEventArgs e)
            {
     
                if (e.CommandName == "EditRow")
                {
                    ViewState["op"] = "Edit";
      ClientScript.RegisterStartupScript(this.GetType(), "Pop", "openModal();", true);
                }
            }
                   
    protected void InsertData(object sender, EventArgs e)
            {
                    if (ViewState["op"] != null)
                    {
                        if (ViewState["op"].ToString() == "Add")
                        {
                            try
                            {
                                using (SqlConnection con = new SqlConnection(CommonFunction.connectionString))
                                {
                                    SqlCommand cmd = new SqlCommand("Sp_Inflator", con);
                                    cmd.CommandType = CommandType.StoredProcedure;
    
                                    cmd.Parameters.AddWithValue("@Action", "I");
                                    cmd.Parameters.AddWithValue("@Code", SqlDbType.VarChar).Value = txt_Code.Text.ToUpper();
                                    cmd.Parameters.AddWithValue("@Inflator", SqlDbType.Decimal).Value = txt_Inflator.Text;
                                    cmd.Parameters.AddWithValue("@Deflator", SqlDbType.Decimal).Value = txt_Deflator.Text;
                                    con.Open();
                                    cmd.ExecuteNonQuery();
                                    FillGrid();
                                    ViewState["Code"] = txt_Code.Text;
                                    string message = CommonFunction.recordInsertedSucessfully;
                                    ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('" + message + "');", true);
                                }
                            }
                            catch (Exception ex)
                            {
                                string a = ex.Message.ToString().Replace("\n", "").Replace("\r", "");
                                ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup(\"" + a + "\");", true);
                            }
                        }
                        else
                        {
                            try
                            {
                                using (SqlConnection con = new SqlConnection(CommonFunction.connectionString))
                                {
                                    SqlCommand cmd = new SqlCommand("Sp_Inflator", con);
                                    cmd.CommandType = CommandType.StoredProcedure;
    
                                    cmd.Parameters.AddWithValue("@Action", "U");
                                    cmd.Parameters.AddWithValue("@Inflator", SqlDbType.Decimal).Value = txt_Inflator.Text;
                                    cmd.Parameters.AddWithValue("@Deflator", SqlDbType.Decimal).Value = txt_Deflator.Text;
                                    con.Open();
                                    cmd.ExecuteNonQuery();
                                    FillGrid();
                                    string message = CommonFunction.recordUpdatedSucessfully;
                                    ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('" + message + "');", true);
                                }
                            }
                            catch (Exception ex)
                            {
                                ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('" + ex.Message + "');", true);
                            }
                        }
                    }
                    ViewState["op"] = "Add";
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('" + errMessage + "');", true);
                }
            }

                 
    Friday, August 17, 2018 12:09 PM

All replies

  • User409696431 posted

    What you pasted in the code section has structure errors.

    protected void InsertData(object sender, EventArgs e)
    {
        if (ViewState["op"] != null)
        {
          ...
        }
        ViewState["op"] = "Add";
    }
    
    ^The closing of the InsertData function
    
    Followed by:
    
        else
        {
                    ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('" + errMessage + "');", true);
        }
    }
    
    

    You will always reach ViewState["op"]="Add", and the else condition is outside the scope of the function.

    Saturday, August 18, 2018 3:22 AM