Firebird ADO.NET provider FormatException

I’ve spent almost an hour searching for a solution to a silly problem I’ve faced with using Firebird embedded database in C#. I was writing a data mapper that takes an object and saves it to the database using stored procedures. Data and procedure parameter names is taken from object’s properties, marked with a special attribute.

When I started testing, a strange exception came from FirebirdClient ADO.NET provider when trying to execute the procedure. It was a FormatException with message “Input string was not in a correct format”, which was completely unrelated to anything I’ve written. What caused the problem is:

The order in which you add parameters to FbCommand object must match the order of stored procedure parameters order.

So, if you have a class like this:

public class Foo
{
    // this attribute tells that property should be passed to stored procedure with name "bar1"
    [DatabaseField("bar1")]
    public int Bar1
    {
        get;
        set;
    }

    [DatabaseField("bar2")]
    public string Bar2
    {
        get;
        set;
    }
}

you must declare your stored procedure parameters in the order in witch you define your properties:

CREATE PROCEDURE PR_FOO_SAVE (
  "bar1" INTEGER,
  "bar2" VARCHAR(100)
)
AS
BEGIN
  SUSPEND;
END

Quite a silly issue in my opinion 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s