【问题标题】:Asp.net C# show Catch error on data insert to databaseAsp.net C# 在将数据插入数据库时​​显示 Catch 错误
【发布时间】:2015-12-21 14:58:52
【问题描述】:

我正在尝试将数据插入数据库,但我想在未插入数据或当时来自数据库的任何其他期望时显示警报消息,以便在 catch 中显示警报但它没有显示并且finally 上工作正常。

        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);
        }

【问题讨论】:

  • 最后,使用字符串“alert_finally”或其他形式的“alert”

标签: c# mysql asp.net


【解决方案1】:

ScriptManager 中的脚本由其类型和键来标识。你的问题是因为在catchfinally 中你写了两个相同的键-"alert"。要实现预期的行为,请使用不同的键,例如。 "alert_catch""alert_finally"

【讨论】:

  • 感谢工作,但捕获显示警报完美,之后它显示精细警报也为此做准备
  • @Shaik 尝试在那里找到答案stackoverflow.com/questions/11121251/…
  • 在循环代码结束后放置 finally 警报。 cmd.ExecuteNonQuery(); } // 这里
【解决方案2】:

如果您使用了更新面板,那么您可以使用:

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

您可以使用的其他方法

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-29
    • 1970-01-01
    • 2023-04-11
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    相关资源
    最近更新 更多