0

When I use the first item in a zero-based Enum cast to Int32 as a query parameter, the parameter value is null. I've worked around it by simply setting the first item to a value of 1, but I was wondering though what's really going on here? This one has me scratching my head. Why is the parameter value regarded as null, instead of 0?

Enum LogEventType : int
{
    SignIn,
    SignInFailure,
    SignOut,
    ...
}

private static DataTable QueryEventLogSession(DateTime start, DateTime stop)
{
    DataTable entries = new DataTable();

    using (FbConnection conn = new FbConnection(DSN))
    {
        using (FbDataAdapter adapter = new FbDataAdapter(
            "SELECT event_type, event_timestamp, event_details FROM event_log " +
            "WHERE event_timestamp BETWEEN @start AND @stop " +
            "AND event_type IN (@signIn, @signInFailure, @signOut) " +
            "ORDER BY event_timestamp ASC", conn))
        {
            adapter.SelectCommand.Parameters.AddRange(new Object[] {
                new FbParameter("@start", start),
                new FbParameter("@stop", stop),
                new FbParameter("@signIn", (Int32)LogEventType.SignIn),
                new FbParameter("@signInFailure", (Int32)LogEventType.SignInFailure),
                new FbParameter("@signOut", (Int32)LogEventType.SignOut)});

            Trace.WriteLine(adapter.SelectCommand.CommandText);
            foreach (FbParameter p in adapter.SelectCommand.Parameters)
            {
                Trace.WriteLine(p.Value.ToString());
            }

            adapter.Fill(entries);
        }
    }
    return entries;
}
Timothy
  • 4,630
  • 8
  • 40
  • 68
  • the first param casted to int should not be null but zero. Are you sure this is the problem? – Mitch Wheat May 09 '10 at 05:01
  • Yes. The `Trace.WriteLine(p.Value.ToString());` is what throws the NullReferenceException on SignIn. Instead of the exception, I expect it to print 0. – Timothy May 09 '10 at 05:05
  • what is `FbParameter` for? does it assume 0 for null? what if you don't use the cast? Try removing the call to `(Int32)`. – shahkalpesh May 09 '10 at 05:11
  • FbParameter is the Firebird equivalent of SqlParameter. The application makes use of an embedded Firebird database. – Timothy May 09 '10 at 05:14
  • The output of the Trace when I remove the cast is SignIn 1 2 – Timothy May 09 '10 at 05:16
  • Add a trace for fb.ParameterName in your foreach so you know which parameter is null. – Raj Kaimal May 09 '10 at 05:20
  • I get @start: 5/9/2010 12:00:00 AM, @stop: 5/9/2010 11:59:59 PM, then A first chance exception of type 'System.NullReferenceException' occurred. The watch panel shows p.ParameterName is "@signIn" and p.Value is null. – Timothy May 09 '10 at 05:32
  • what do you mean by "SignIn 1 2" above, when you remove the cast? – shahkalpesh May 09 '10 at 05:51
  • The name of the enum member is printed, followed by the integer values 1 and 2 (the values of the subsequent two members). – Timothy May 09 '10 at 06:38
  • What happens when you change the enum used in new FbParameter("@signIn", (Int32)LogEventType.SignIn), with SignInFailure – Raj Kaimal May 11 '10 at 01:00
  • (Int32)LogEventType.SignInFailure casts to 1 and there is no exception. I've found through experimentation that FbParameter("@param", 0) will throw an exception, but FbParameter("@param", "0") will not. It's something to do with passing a zero value as the parameter value. I don't want to jump to conclusions, but is this possibly a bug? It's just too strange. – Timothy May 11 '10 at 02:11
  • FbParameter("@signIn", LogEventType.SignIn) also works without exception. It has something to do with a zero value if I cast it. – Timothy May 11 '10 at 02:13

1 Answers1

1

I do not know this api but it may be a bug. Try passing in a FbDbType enum when defining the params.

http://www.firebirdsql.org/dotnetfirebird/documentation/api/1.7/FirebirdSql.Data.Firebird.FbDbType.html

http://www.firebirdsql.org/dotnetfirebird/documentation/api/1.7/FirebirdSql.Data.Firebird.FbParameter.html

Another option is to try adding the parameters one by one instead of using an object array and the AddRange method.

Raj Kaimal
  • 8,304
  • 27
  • 18
  • Thank you for your help and your answer. `adapter.SelectCommand.Parameters.Add("@signIn", FbDbType.Integer).Value = (Int32)LoggerEventType.SignIn;` works as intended. I agree, it must be a bug. – Timothy May 11 '10 at 03:56