Asp.net C#向数据库插入数据时显示捕获错误

Asp.net C#向数据库插入数据时显示捕获错误,c#,mysql,asp.net,C#,Mysql,Asp.net,我正在尝试向数据库中插入数据,但如果数据未插入,或数据库当时有任何其他期望,我希望显示一条警报消息。在catch中显示警报,但它没有显示,并且在上,最后工作正常 try { using (var con = new MySqlConnection(constr)) using (var cmd = con.CreateCommand()) { cmd.Comma

我正在尝试向数据库中插入数据,但如果数据未插入,或数据库当时有任何其他期望,我希望显示一条警报消息。在catch中显示警报,但它没有显示,并且在
上,最后
工作正常

        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);
        }
试试看
{
使用(var con=new MySqlConnection(constr))
使用(var cmd=con.CreateCommand())
{
cmd.CommandText=“插入时间表(`year`,`Day`)值(@id,@REG)”;
con.Open();
对于(int i=0;i
脚本管理器中的脚本通过其类型和键进行标识。您的问题是因为在
catch
finally
中编写了两个相同的键-
“alert”
。要实现预期的行为,请使用不同的键,例如,
“alert\u catch”
“alert\u finally”
如果使用了更新面板,则可以使用:

ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "javascriptFunction();", true);
你还可以用别的方法

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

在finally中,使用字符串“alert_finally”或其他与“alert”不同的形式表示感谢,但在捕获时显示了完美的警报,然后显示了精细的警报,这也是值得做的that@Shaik尝试在那里找到答案,将finally警报放在循环代码end.cmd.ExecuteNonQuery();}//之后