简体   繁体   中英

C# doesn't catch execute non query exception

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."

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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