Catch REFERENCE constraint exception from SQL in C# - c#

What will be proper way to catch exception which throw SQL server when I deleting data with Reference constraint from C# code.
I want to show my users message like:
"I can't delete the data because is used"
, rather than of showing message like this:
The DELETE statement conflicted with the REFERENCE constraint ... The conflict ccurred*in database "rampa", table "dbo.doc", column 'kartica_id'.

Use this:
try
{
//Execute delete statement
}
catch (SqlException sqlEx)
{
//Handle exception
}
finally
{
//Close connection
}
All sql errors are thrown as SqlException and there are no specific errors. To find out what was error exactly, there is property SqlException.Number which is the same as SQL Error Code. You can find the list of codes here.

You can access the ConstraintException and map its fields however you want to your own preferred error output.
using System.Data;
try
{
// code that violates constraint
}
catch (ConstraintException exc)
{
// build output message you wish using exc.Message and other fields,
// return cleanly or rethrow as your own exception
}

Related

SQLite issue with insert not unique

I have a button in my project that inserts a record into my SQLite database.
but there is an issue cus I have unique index so if the insert is a dup it will error out and quit the function.. how can I prevent from showing an error and quitting?
SQLite error (2067): abort at 11 in [INSERT INTO my_success VALUES('123456')]: column myids is not unique
A first chance exception of type 'System.Data.SQLite.SQLiteException' occurred in System.Data.SQLite.dll
Probably not the best solution, but you should be able to catch the exception:
try
{
// put your SQLite insert code here...
}
catch(System.Exception)
{
}

Handling Sql Custom Exception

How to identify the custom error message raised from the sql stored procedure with c# code?
Stored Procedure the error will be raised like this
RAISERROR (N'This is message %s %d.', -- Message text.
10, -- Severity,
1, -- State,
N'number', -- First argument.
5); -- Second argument.
From the above, how to identify that the error is custom error message. (ie) Something like this
try{
--actual code here
}
catch(SqlException ex)
{
--how to check here that the exception is custom one
}
When you raise the error you can provide a MessageId instead of a Message text. This number will be found in the Number propertie of the Exception:
SQL:
RAISERROR(50001, 12, 1)
C#:
if (sqlException.Number == 50001)
{
throw new CustomSQLException(//whatever);
}

Customize Sql Exception message in catch

I ´m doing a form where the user can Insert/Delete/Add entities. I´m using Winforms c# and entity framework 4.
Ok, the user can delete objects. Now, those objects can be referenced by other entities, so if the user wants to delete it, he will get an exception.
I catch that exception so as to show him a message that says that that object is begin used in other objects.
The exception I got is (UpdateException). But this exception can be raised if there is an update issue too. Is there any way to get the error code from Sql using this exception? Because I do have the error code that throws when this happens.
If I use SqlException I can check its number, but that's not the exception I´m receiving.
Do you mean you want to see the full exception or you want the user to the see the exception?
If you want to the see exception, you could write it to a text file:
try
{
// do something
}
catch(SQLException sqlex)
{
using (var file = new StreamWriter(#"C:\Users\Home\Desktop\sqlException.txt"))
{
file.WriteLine(sqlex.ToString());
}
}
catch(Exception e)
{
using (var file = new StreamWriter(#"C:\Users\Home\Desktop\generalException.txt"))
{
file.WriteLine(e.ToString());
}
}
This will write the exception to a text file on your desktop (change directories as appropriate).

Converting MS Oracle client to Oracle Data Provider - OracleException

I'm converting a product from System.Data.OracleClient to Oracle.DataAccess.Client, and came across a question Here's a snippet of some code:
try
{
//some db code
}
catch (System.Data.OracleClient.OracleException ex)
{
if (ex.Code == 00904)
{
// log specific error
}
}
The problem is, Oracle.DataAccess.Client.OracleException has no Code property. It has a Number property. Is this the same thing? The docs say this about the property -
This error number can be the topmost level of error generated by
Oracle and can be a provider-specific error number.
The Code property contained the ORA- error code.
Yes, they are the same thing. Oracle.DataAccess.Client.OracleException.Number is the same info as System.Data.OracleClient.OracleException.Code.

What happens if limit of Sql Server Compact Edition is reached?

What happens if a database reaches the limit of 4GB of the SQL Server Compact Edition? Is there a special exception for this?
Can I safely catch this event or exception and, let's say, create a new database?
I have not experienced this myself, but it looks like a SqlCeException will be thrown and the NativeError property of the contained SqlCeError will have an error code of 25104 (SSCE_M_DATABASETOOBIG).
Here's a listing of SqlCeError Native Codes related to db engine errors -- the one about the db file being too big is about 2/3 of the way down. The listing is for SQL CE 3.5; you didn't specify what version you were using, but I'm guessing that it wouldn't change.
I don't see why you couldn't catch this exception and then create a new database in your catch section.
try {
//do something
} catch (SqlCeException cexc){
foeach (SqlCeError aError in cexc.Errors) {
if (aError.NativeError == 25104) { //this is the code for the TOO BIG error code
//handle too big error -- maybe create a new database
}
}
}
I hope this helps!

Categories