0

I am trying to insert data to data base but i want to show an alert message if the data is not inserted or any other expectation from data base at that time a tying to show alert in catch but it is not showing up and on finally is working fine.

        try
        {
            using (var con = new MySqlConnection(constr))
            using (var cmd = con.CreateCommand())
            {
                cmd.CommandText = "insert into time_table (`year`, `Day`) values(@id, @REG)";
                con.Open();
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    cmd.Parameters.Clear();
                    TextBox txtUsrId = (TextBox)GridView1.Rows[i].FindControl("txtStudent_Name");
                    TextBox REGId = (TextBox)GridView1.Rows[i].FindControl("txtreg_number");
                    cmd.Parameters.Add("@id", MySqlDbType.VarChar).Value = txtUsrId.Text;
                    cmd.Parameters.Add("@REG", MySqlDbType.VarChar).Value = REGId.Text;
                    cmd.ExecuteNonQuery();
                }
            }
        }
        catch 
        {

            ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", "alert('Error Contact Admin')", true);
        }
        finally {

            ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", "alert('recorded added')", true);
        }
1
  • in finally, use string "alert_finally or something different form "alert" Commented Dec 21, 2015 at 6:37

2 Answers 2

2

Scripts in ScriptManager are identified by it's type and it's key. Your problem is because in catch and in finally you wrote two identical keys - "alert". To achieve expected behavior use different keys, for ex. "alert_catch" and "alert_finally"

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks worked but there on catch that shows the alert perfect after that it is showing the fineally alert also wat to do for that
@Shaik Try to find answer there stackoverflow.com/questions/11121251/…
put the finally alert just after loop code ending. cmd.ExecuteNonQuery(); } // here
0

If you Used Update Panels Then You can Use:

ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "javascriptFunction();", true);

Other Wise You can Use

ClientScript.RegisterStartupScript
        (GetType(),Guid.NewGuid().ToString(), "javascriptFunction();",true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.