简体   繁体   English

在 catch 块中显示特定的错误消息

[英]display the particular error message in catch block

    try
    {
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ToString());
        SqlDataAdapter sda = new SqlDataAdapter("Select * from CustomeDetails", con);
        sda.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    catch (SqlException d)
    {

       //Display Error Message
    }

How can i display the error message, If there is any error in try block for example, In my Code "Invalid Object name CustomeDetails" is a error.我如何显示错误消息,例如,如果 try 块中有任何错误,在我的代码中“无效的对象名称 CustomeDetails”是一个错误。 same way I have to display all errors which occurs in try block.同样,我必须显示在 try 块中发生的所有错误。 I apologize for my English.我为我的英语道歉。 Note : I have to display all these error messages in web page.注意:我必须在网页中显示所有这些错误消息。

You can loop through .Errors.您可以遍历 .Errors。 Might have what you need.可能有你需要的东西。

catch (SqlException ex)
{
  Console.WriteLine(ex.Message);
  DisplaySqlErrors(ex);
}


private static void DisplaySqlErrors(SqlException exception)
{
    for (int i = 0; i < exception.Errors.Count; i++)
    {
        Console.WriteLine("Index #" + i + "\n" +
        "Error: " + exception.Errors[i].ToString() + "\n");
    }
}

From

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM