Problem with SafeDataReader - getting object reference not set

Problem with SafeDataReader - getting object reference not set

Old forum URL: forums.lhotka.net/forums/t/3265.aspx


RSB posted on Wednesday, July 25, 2007

I have the object not set to a reference problem (null exception) problem with SafeDataREader

Its concept wise contradicting. The very purpose of using SafeDataReader is to avoid excess code checking for null exceptions. its the problem with Module.ParentId in datafetch method. This could be null in the DB..

I think I know what the problem is, is it because we are initializing new objects in getList, but this is the way, I have it working in all my other BO objects and list...

Any help appreciated..

Thanks,
RSB

 

In ModuleList.cs

private void Fetch(SafeDataReader dr)

{
   while (dr.Read()){
               
this.Add(BOModules.GetAllModules(dr));
               }
}

In Module.cs

internal static BOModules GetAllModules(SafeDataReader dr)

{
      
return new BOModules(dr);
}

private BOModules(SafeDataReader dr)
{
      MarkAsChild();
         DataPortal_Fetch(dr);

}

private void DataPortal_Fetch(SafeDataReader dr){

BModuleID = dr.GetValue("MODULEID").ToString();

BModuleName = dr.GetString("MODULENAME");

BModuleDesc = dr.GetString("MODULEDESC");

BModuleDisplay = dr.GetValue("MODULEDISPLAY").ToString();

//if (dr.GetValue("MODULEPARENTID") != null)

BModuleParentID = dr.GetValue("MODULEPARENTID").ToString();

//else

//BModuleParentID = String.Empty;

MarkOld();

}

triplea replied on Thursday, July 26, 2007

Just out of curiosity, where do you create your SafeDataReader object? Could you post that code as well?

RSB replied on Thursday, July 26, 2007

I have a custom data access class which retrieves the list of modules from DB. The code is as follows

private void GetGroupModules(string pGroupID)

{

DataAccessLayer dl = new DataAccessLayer();

dl.DbCommand = "GETGROUPMODULES";

try

{

dl.AddParameter("poReturn_code", DbTypes.INT);

dl.AddParameter("cur_OUT", DbTypes.REFCURSOR);

dl.AddParameter("pGROUPID", pGroupID);

using (SafeDataReader dr = new SafeDataReader(dl.ExecuteReader()))

{

Fetch(dr);

}

dl.Close();

}

catch (System.Exception ex)

{

throw new System.Security.SecurityException("Error in BOModulesList.GetGroupModules(string pGroupID): " + ex);

}

Thanks,
RSB

RockfordLhotka replied on Thursday, August 02, 2007

RSB:

I have the object not set to a reference problem (null exception) problem with SafeDataREader

What object is null?

Are you saying that GetValue() is sometimes returning null? That is the intended behavior of GetValue() in the case that the database value is DBNull. It has to be this way, because GetValue() returns type object, and the "default" value of object is null.

If you want to avoid getting a null, call GetInt32() or one of the other strongly typed methods.

RSB replied on Thursday, August 02, 2007

Thank you!!  This worked!!

RSB

Copyright (c) Marimer LLC