0

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.

A. Celarc
  • 1
  • 1
  • 1
    Can any of the involved fields be null? In that case calling ToString on them will fail with the exception mentioned. And using using is the way to go. – Ralf Mar 23 '23 at 13:42
  • I think not. I get this error even if I comment all the if statments. and the error is ocuring in this line "using (FbDataReader myreader3 = readCommand3.ExecuteReader())". I added screenshot of the error. – A. Celarc Mar 23 '23 at 14:01
  • Sounds weird. For clarity it may help attaching the stacktrace here when this happens. – Ralf Mar 23 '23 at 14:36
  • 1
    Have you tried with a newer version? FirebirdSql.Data.FirebirdClient version 5.12.1 is relatively old, the latest is 9.1.0.0. Also, please post the full exception stacktrace **as text**. – Mark Rotteveel Mar 23 '23 at 15:40
  • 2
    I updated FirebirdSql.Data.FirebirdClient to 9.1.1 and now the error is gone. That is only logical explanation for that odd behaviour. Thanks both of you for your help. I updated the question and added the stacktrace of error before update if you are interested what was going on, but for me problem is solved. – A. Celarc Mar 24 '23 at 10:19
  • Good to hear updating solved it. The stacktrace suggests it occurred when starting a transaction, which as far as I can guess, means that either the connection/socket handle itself was null, or the transaction parameter buffer was, both of which would suggest some bug within the driver itself. – Mark Rotteveel Mar 25 '23 at 09:42

1 Answers1

-1

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197