C# Firebird FbDataReader Object reference not set to an instance of an object

171 Views Asked by At

I'm starting a Windows Forms program from Task Scheduler under specific user used only for that. I have some queries to the database executed by FbDataReader and all queries execute work OK, except sometimes (not always, that is the most confusing part). I get error "System.NullReferenceException: 'Object reference not set to an instance of an object.'". I also tried without using statement with manual open and close connection, but still the same problem (I don't know what is better using statement or manual open and close connection?).

The error occurs at this query:

using (FbConnection ConnBMC = new FbConnection(BMC.connStringBMC))
{
    ConnBMC.Open();
    string ukaz = "SELECT IME,VREDNOST_STR,VREDNOST_REAL,VREDNOST_INT FROM PARAM WHERE IME = 'EPOSTA_UPORABNISKO_IME' OR IME = 'EPOSTA_GESLO' OR IME = 'EPOSTA_STREZNIK' OR IME = 'EPOSTA_VRATA' OR IME = 'EPOSTA_SEND_FROM_NOREPLY' OR IME = 'EPOSTA_NASLOV_INT'";

    using (FbCommand readCommand3 = new FbCommand(ukaz, ConnBMC))
    {
        using (FbDataReader myreader3 = readCommand3.ExecuteReader())
        {
            while (myreader3.Read())
            {
                if (myreader3["IME"].ToString().Trim() == "EPOSTA_UPORABNISKO_IME") BMC.emailUser = myreader3["VREDNOST_STR"].ToString().Trim();
                else if (myreader3["IME"].ToString().Trim() == "EPOSTA_GESLO") BMC.emailPass = myreader3["VREDNOST_STR"].ToString().Trim();
                else if (myreader3["IME"].ToString().Trim() == "EPOSTA_STREZNIK") BMC.emailServer = myreader3["VREDNOST_STR"].ToString().Trim();
                else if (myreader3["IME"].ToString().Trim() == "EPOSTA_VRATA") BMC.emailPort = myreader3["VREDNOST_STR"].ToString().Trim();
                else if (myreader3["IME"].ToString().Trim() == "EPOSTA_ENABLE_SSL") BMC.emailSSL = Convert.ToBoolean(myreader3["VREDNOST_INT"]);
                else if (myreader3["IME"].ToString().Trim() == "EPOSTA_NASLOV_INT") BMC.emailInternalAddress = myreader3["VREDNOST_STR"].ToString().Trim();
            }
        }
    }
}

If I set the break point after I initialize "readCommand3" (image in the attachment), everything looks fine, the connection is open and valid command text is set and so on. So, I really don't have any idea why would this happen and why only some times. Before that query, I have 9 queries on the same database executing perfectly.

I'm using FirebirdSql.Data.FirebirdClient version 5.12.1

Breakpoint result Error message with commented if statements

Stacktrace:

   at FirebirdSql.Data.Client.Managed.Version10.GdsTransaction.BeginTransaction(TransactionParameterBuffer tpb)
   at FirebirdSql.Data.Client.Managed.Version10.GdsDatabase.BeginTransaction(TransactionParameterBuffer tpb)
   at FirebirdSql.Data.FirebirdClient.FbTransaction.BeginTransaction()
   at FirebirdSql.Data.FirebirdClient.FbCommand.Prepare(Boolean returnsSet)
   at FirebirdSql.Data.FirebirdClient.FbCommand.ExecuteCommand(CommandBehavior behavior, Boolean returnsSet)
   at FirebirdSql.Data.FirebirdClient.FbCommand.ExecuteReader(CommandBehavior behavior)
   at FirebirdSql.Data.FirebirdClient.FbCommand.ExecuteReader()
   at BMC.Form1.readParam() in C:\Development\Visual Studio 2017\Projects\BMC\BMC\Form1.cs:line 1008
   at BMC.Form1.Form1_Load(Object sender, EventArgs e) in C:\Development\Visual Studio 2017\Projects\BMC\BMC\Form1.cs:line 103
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at DevExpress.XtraEditors.XtraForm.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at DevExpress.XtraEditors.XtraForm.WndProc(Message& msg)
   at BMC.Form1.WndProc(Message& msg) in C:\Development\Visual Studio 2017\Projects\BMC\BMC\Form1.cs:line 455
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

I read data from a Firebird database and get a null reference exception.

1

There are 1 best solutions below

0
Mark Rotteveel On

The issue was solve by upgrading to FirebirdSql.Data.FirebirdClient version 9.1.1. See also the comment by A. Celarc:

I updated FirebirdSql.Data.FirebirdClient to 9.1.1 and now the error is gone. That is only logical explanation for that odd behaviour.