title: Asp.net C# show Catch error on data insert to database
body: <p>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 <code>finally</code> is working fine.</p>
  
  <pre><code>        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 &lt; 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);
          }
  </code></pre>