I want to catch a specific mysql error with the number 1046.
If i use MessageBox.Show to display the error number it is always 0, no matter which Exception it is. Shouldn't the Exception have a number like the number 1046?
catch (MySqlException mysqlex)
{
MessageBox.Show(mysqlex.Number.ToString());
}
EDIT: Sorry, i am using the MySQL-Connector / Net.
You don't see a number because you did not connect to a database.
And probably MySQL is assigning numbers to error exceptions.
If you look at description for Number property here it says
Gets a number that identifies the type of error. This number corresponds to the error numbers given in Server Error Codes and Messages.
Because you did not made a connection to the server you cannot get server error code.
Check your connection string.
Probably same problem as this.
please try in the following code.
MessageBox.Show(mysqlex.InnnerException.Number.ToString());
Related
We are using an embedded Firebird V3.0.2 for our MVC5 Project
Problem: When we update the software, we want to find out if a table exists before we create the new table. Therefor we simply make a select on the desired table and if an exception with sql error code -204 occurs, then the table does not exist. But how can I resolve the error code -204?
Code:
var lFbCommand = new FbCommand();
lFbCommand.Connection = "MyConnectionstring";
lFbCommand.Connection.Open();
// check if table exists by an select query
lFbCommand.CommandText = string.Format("SELECT FIRST 1 * FROM {0}", "MyTableName");
try
{
pFbCommand.ExecuteNonQuery();
}
catch (FirebirdSql.Data.FirebirdClient.FbException e)
{
if(e.? == -204) // -204 seem to be the errorcode for "Table does not exists."
{
// table does not exist.
}
else
{
throw;
}
}
Note: We want to prevent parsing the message text in the exception. We want to get a clear flag for this error (e.g. the error code via any property).
You shouldn't look at the (so-called) SQLcode. SQLcodes are very unspecific, for example SQLcode -204 covers a broad range of errors (maybe dozens if not hundreds of sub-errors). Not just "table unknown", but also "trigger unknown", "Datatype unknown", etc.
You'd need to look at the error code (FbException.ErrorCode) (which is not the same as the SQLcode), although that is still tricky, because sometimes error codes are nested from unspecific (covering a similar range as the associated SQLcode) to more specific. And that is the case for "table unknown" (isc_dsql_relation_err or 335544580), which is nested in the group isc_dsql_error (335544569). The isc_dsql_error - IIRC - maps almost one-to-one to SQLcode -204 (which could be a few dozen to a few hundred errors).
As far as I know - but I don't usually program in C# - you will need to look at FbException.Errors (which returns a FbErrorCollection) and check if it contains the right error(s).
I am processing extremely large delimited files. These files have been pre-processed to ensure that field and row delimiters are valid. Occasionally a row is processed that fails TSQL constraints (usually a datatype issue). 'Fixing' the input data is not an option in this case.
We use MAXERRORS to set an acceptable number of input errors and ERRORFILE to log failed rows.
The bulk insert completes in SSMS with severity level 16 error messages logged to the messages window for each failed row. Attempting to execute this code via the C# SqlCommand class causes an exception to be thrown when the first severity level 16 error message is generated, causing the batch to fail.
Is there a way to complete the operation and ignore SQL error messages via C# and something like SqlCommand?
Example Command:
BULK INSERT #some-table FROM 'filename'
WITH(FIELDTERMINATOR ='\0',ROWTERMINATOR ='\n',FIRSTROW = 2, MAXERRORS = 100, ERRORFILE = 'some-file')
Why not use SqlBulkCopy, and then capture the rows copied using the SqlRowsCopied event. This will more closely mimic the BULK INSERT T-SQL command.
EDIT: It looks like the error handling isn't that robust with SqlBulkCopy. However, here is an example that seems to do what you're looking for:
http://www.codeproject.com/Articles/387465/Retrieving-failed-records-after-an-SqlBulkCopy-exc
Since .NET support all the datatype as SQL you should be able to TryParse in .NET to catch any conversion errors. On date you also need to test for within the SQL data range. On text need to test length. I do exactly this on some very large inserts were I parse down some CSV. TryParse is pretty fast. Better than a Try Catch as it does not have the overhead of throwing and error.
And why not insert in .NET C#. There is a class for bulkcopy. I use TVP asynch insert while parsing and do 10,000 at a time.
It appears that an exception is thrown for each row that has an error but is just counted by SQL. However, it is also passed back to C# (SSIS in my case). I found that wrapping the bulk insert with TRY/CATCH logic and using THROW (to re-throw the exceptions) when more than MAXERRORS occur, works for me.
BEGIN TRY
BULK INSERT #some-table FROM 'filename'
WITH(FIELDTERMINATOR ='\0',ROWTERMINATOR ='\n',FIRSTROW = 2, MAXERRORS = 100,
ERRORFILE = 'some-file')
END TRY
BEGIN CATCH
THROW;
END CATCH
I'm doing a project in Asp .Net and C#. I'm also using ODP .NET to connect to my Oracle DB.
I am using a stored procedure to insert a value into the database. Everything is fine, it also raises all the exceptions I have.
I have 3 exceptions:
when the value I try to insert already exists in the database
when i try to insert a null value
Default "Others"
When the first two exceptions are raised I insert the error into a table Errors, everything goes well.
What I really want to know is, how can I show a message to the user when a Exception occurs?
Something like dbms_output.put_line("Error");...but I want it to show in my webpage. Is this possible?
Any tips are welcome, thanks in advance.
Since your .NET program is your client, you should let any unhandled exceptions propagate from your PL/SQL program and back to the client, where you can deal with it like any other exception.
In other words, you should remove the "when others" exception from your PL/SQL code, and instead wrap your database call (using ODP.NET) with a C# exception block. There you can catch the exception and get the Oracle error number and text, and display it to the user if you want.
(Using this approach you can also use RAISE_APPLICATION_ERROR in your PL/SQL code to signal errors back to the C# client.)
You can fix the stored procedure to return an integer, 0 means ok, 1 means exception of type 1, 2 exception of type 2 and so on... in this way the UI can notify the user that saving method did not go well as expected.
Normally I would not have handled the exception in the stored procedure but I understand that you want to log the error from SQL so the above way should allow you to do what you want.
In our sql server stored procedures we have something like this:
IF ##ERROR <> 0
return (1)
ELSE
return (0)
but it depends on what the stored does and what else it returns, if the stored returns a table, you can then use output parameter to send back the integer discussed above.
I get a -2147024891 error code in a MessageQueueException; of type MessageQueueErrorCode. How can I find which errors occurred here?
Note: I have extracted values of this enum using System.Enum.GetValues and then used a LINQ query to find out which ones do fit in this error by using & (and); then getting it's name by System.Enum.GetName and joining them with a ',' separator...anyway: failed!
The error code is 0x80070005. The 7 is the 'facility code', it is Windows. In other words, you didn't get a message queuing error, you got a Windows error. Error code 5 is "Access Denied".
Something wrong with the user account, typically, not enough privileges.
You can cast your errorcode to an enum and use ToString():
string error = ((MessageQueueErrorCode)ex.ErrorCode).ToString();
which should return the Name of the enum value.
Actually i'm unsure what happens if the enum doesn't contain this specific value (maybe an InvalidCastException is thrown) but you can try it out for yourself.
How Can I Get The Error Number in my try catch ?
You can get Error Number in some specific Exceptions. Like sql exceptions and other.
But other wise you can get error message, error data and other stuffs
Try to explain a little bit more.