Return SQL Server error to user

I want to return error that occured at the time of update in SQL Server 2008.

Now I want to display this error to user so that he can understand what goes wrong.

  • Find a specific column entry in an unknown table in a database?
  • Shrink and Group Column using Pivot in SQL Server
  • pyQT and MySQL or MSSQL Connectivity
  • How can i replace content under a specific URL using SQL?
  • Linq To Sql: Exception “String must be exactly one character long”
  • SQL server select distinct rows using most recent value only
  • How should I do this in c#?


  • SQL Server:An expression of non-boolean type specified in a context where a condition is expected
  • How can I join and group two tables by a column that may or may not have a known prefix?
  • Returning multiple values in a CASE expression
  • Which server platform to choose: SQL Azure or Hosted SQL Server for new project
  • How to create excel file and folder at runtime based on city
  • Cannot resolve the collation conflict between “SQL_Latin1_General_CP1_CI_AS” and “Latin1_General_CI_AS” in the equal to operation
  • 3 Solutions collect form web for “Return SQL Server error to user”

    Errors when performing an SQL statement against a database raise an exception in the connector. Those exceptions are of type SqlException and, as such, have a Message property that contains a user friendly description of the error.

    Remember though that while user-friendly, this message is aimed at developers. You should not display that to an end-user.

    As usual, to trap an exception you have to use a try-catch block:

        SqlConnection conn = null; 
        conn = new SqlConnection(connString);  // connstring is your connection string to the database
        var strSQLCommand = "SELECT* FROM TABLE";  // statement is wrong! will raise an exception
        var command = new SqlCommand(strSQLCommand, conn); 
        var ret = command.ExecuteScalar(); 
        return ret; 
    catch (SqlException e)

    This example of course assumes a Console application.

    Try using a try, catch clause

    something like that:

    try {
    // Update Code Here
    catch (Exception e) {
    // Error message in e.Message
    // On a form
       MessageBox.Show(e.Message, "Error!");
    // TextBox text1 is defined
       text1.Text = e.Message;

    where you can replace Exception with the type of exception you want to catch (SqlException).

    try this

          //your sql code
    catch(SqlException sqlEx)
        for (int i = 0; i < ex.Errors.Count; i++)
                errorMessages.Append("Index #" + i + "\n" +
                    "Message: " + ex.Errors[i].Message + "\n" +
                    "LineNumber: " + ex.Errors[i].LineNumber + "\n" +
                    "Source: " + ex.Errors[i].Source + "\n" +
                    "Procedure: " + ex.Errors[i].Procedure + "\n");

    SqlException – which catches all the sql exception

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.