-4
    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. same way I have to display all errors which occurs in try block. I apologize for my English. Note : I have to display all these error messages in web page.

Mathis
  • 41
  • 1
  • 9
  • it really depends on what library you use to run sql and what database you accessing if it is a database at all, provide more details ... code sample will help – Woland Aug 29 '15 at 07:51

1 Answers1

3

You can loop through .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

Devon Burriss
  • 2,497
  • 1
  • 19
  • 21
  • Thanks deebo. It Executing well. But How can display that error message in web page. – Mathis Aug 29 '15 at 09:48
  • 1
    If you are in an Action method in the controller either add it to your viewmodel, or add it to ModelState.Errors and display in the validation summary. I'll edit answer. – Devon Burriss Aug 29 '15 at 09:55
  • What kind of web project is it? Web Pages or MVC. If it answered your question can you mark it as the answer please? – Devon Burriss Aug 29 '15 at 09:57
  • For MVC.NET check out this question http://stackoverflow.com/questions/5739362/modelstate-addmodelerror-how-can-i-add-an-error-that-isnt-for-a-property – Devon Burriss Aug 29 '15 at 10:00
  • It just simple web page with one Gridview which is invoking a table from database. – Mathis Aug 29 '15 at 10:26
  • I suggest you ask that as a new question as that is a completely different topic. And mark this one as answered... – Devon Burriss Aug 29 '15 at 10:36