I am confused as to why I am getting a null reference exception when trying to return a Dataset.
I have a handler and DBaccess classes as in an nTier architecture.
First here is a portion on my casefile handler/and access classes which are working fine. I'm including them because I cant see why this works and the other doesn’t.
public class CaseFileHandler
{
CaseFileAccess caseaccess = null;
public CaseFileHandler()
{
caseaccess = new CaseFileAccess();
}
public DataSet getCaseFileDataset(int caseID)
{
return caseaccess.GetCaseFileDataSet(caseID);
}
public bool UpdateCasefile(CaseFile casefile)
{
return caseaccess.updateCasefile(casefile);
}
}
And here is the access class. The method that returns a dataset is working just fine.
public class CaseFileAccess
{
const string CONNECTION_STRING = "Data Source = SQLSERVER;Initial Catalog=CaseManager;Integrated Security=SSPI;";
DBManager _DAL = new DBManager(DataProvider.SqlServer, CONNECTION_STRING);
public CaseFile GetCaseFile(int caseID)
{
CaseFile casefile = new CaseFile();
_DAL.Open();
_DAL.CreateParameters(1);
//stuff
return casefile;
}
public DataSet GetCaseFileDataSet(int caseID)
{
_DAL.Open();
_DAL.CreateParameters(1);
_DAL.AddParameters(0, "@CaseID", caseID);
DataSet ds = _DAL.ExecuteDataSet(CommandType.StoredProcedure, "sp_BL_AdjustmentsPage_READ_Case");
_DAL.Close();
return ds;
}
public bool updateCasefile(CaseFile casefile)
{
int result = 0;
_DAL.CreateParameters(56);
//stuff
_DAL.Open();
result = _DAL.ExecuteNonQuery(CommandType.StoredProcedure, "dbo.sp_BL_AdjustmentsPage_UPDATE_Case");
_DAL.Close();
return (result > 0);
}
}
And this is why I am confused. I created a handler and an access class to return datasets to bind to asp:dropdown lists and constructed the classes in the same way as the case file classes
Here is the handler class:
public class ddMenuHandler
{
ddMenuAccess menuaccess = null;
public ddMenuHandler()
{
ddMenuAccess menuaccess = new ddMenuAccess();
}
public DataSet dsCwkrCaseEditing(int cwID)
{
return menuaccess.CwkrsCaseEditing(cwID);
}
}
And the access class:
public class ddMenuAccess
{
const string CONNECTION_STRING = "Data Source = SQLSERVER;Initial Catalog=CaseManager;Integrated Security=SSPI;";
DBManager _DAL = new DBManager(DataProvider.SqlServer, CONNECTION_STRING);
public DataSet CwkrsCaseEditing(int cwID)
{
_DAL.CreateParameters(1);
_DAL.AddParameters(0, "@ID",cwID);
_DAL.Open();
DataSet ds = _DAL.ExecuteDataSet(CommandType.StoredProcedure, "sp_BL_CwkrsActiveCaseEdit");
_DAL.Close();
return ds;
}
}
And this is my code behind:
protected global::Adjustfiles.CaseFile casefile;
protected global::System.Web.UI.WebControls.DropDownList ddCaseworker;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
int caseID = Convert.ToInt32(Request.QueryString["caseID"]);
MySession.Current.editCaseID = caseID;
CaseFileHandler cfhandler = new CaseFileHandler();
casefile = cfhandler.getCaseFile(caseID);
DataSet DScase = cfhandler.getCaseFileDataset(caseID);
fv_casefile.DataSource = DScase;
fv_casefile.DataBind();
BindDdCwkrs();
}
}
private void BindDdCwkrs()
{
ddMenuHandler menuaccess = new ddMenuHandler();
DataSet ds = menuaccess.dsCwkrCaseEditing(casefile.CaseWorkerID);
int i = 0;
ddCaseworker.DataSource = ds;
ddCaseworker.DataTextField = "CwName";
ddCaseworker.DataValueField = "CwID";
ddCaseworker.DataBind();
ddCaseworker.SelectedValue = Convert.ToString(casefile.CaseWorkerID);
}
At the line ddCaseworker.DataSource = ds;
is where I am getting my null reference exception, menuaccess is null
. But why?
Source Error:
Line 29:public DataSet dsCwkrCaseEditing(int cwID)
Line 30:{
Line 31: return menuaccess.CwkrsCaseEditing(cwID);
Line 32:}
Line 33:
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=Adjustfiles
StackTrace:
at Adjustfiles.ddMenuHandler.dsCwkrCaseEditing(Int32 cwID) in H:\Visual Studio 2010\Projects\Adjustfiles\Adjustfiles\ddMenuHandler.cs:line 31
at Adjustfiles.adjustFile66EditCase.BindDdCwkrs() in H:\Visual Studio 2010\Projects\Adjustfiles\Adjustfiles\adjustFile66EditCase.aspx.cs:line 45
at Adjustfiles.adjustFile66EditCase.Page_Load(Object sender, EventArgs e) in H:\Visual Studio 2010\Projects\Adjustfiles\Adjustfiles\adjustFile66EditCase.aspx.cs:line 29
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException: