0

I have this code that calls the stored procedure to 'insert' data to tables in SQL.

using (SqlConnection connection = new SqlConnection(Global_Variables.DBcon))
{
    SqlCommand cmd = new SqlCommand("sp_WinApps_Import_ERData", connection);
    cmd.CommandType = CommandType.StoredProcedure;
    connection.Open();
    try
    {
        cmd.ExecuteNonQuery();
        Console.WriteLine("file imported!");
    }

    catch (SqlException ex)
    {
        Console.WriteLine("BATCH ID ALREADY EXISTS!" + ex.Message );
    }
    finally
    {
        connection.Close();
    }
}

but the problem is, it doesn't catch the error in the 'try-catch method' i dont know why but my code is correct.

The Error says: "Violation of UNIQUE KEY constraint 'Cons_BatchID'. Cannot insert duplicate key in object 'dbo.tbl_WinApps_FileHeader'. Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated."

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1954418
  • 963
  • 7
  • 21
  • 29

1 Answers1

-1

I solved it guys. Go to Debug > Exceptions and i just 'unchecked thrown checkbox' in Common Language Runtime Exceptions

user1954418
  • 963
  • 7
  • 21
  • 29
  • This will not help you if your Program ran not in Visual Studio -_- – Smartis has left SO again Jul 01 '13 at 06:17
  • @Smartis No, it is a misunderstanding. The OP thought an error wasn't being caught. It was, but Visual Studio would break on the immediate cause of the exception. This option stops that. Had the OP stepped through the code he would have seen the catch working as expected (assuming it was a `SqlException`). – Adam Houldsworth Dec 09 '14 at 15:03