Here, when I set debugpoint I can see the DataTable is getting populated by the data so my
sp idreturning the data. there is no issue with the sp and dataaccess layer method. The problem lies below on my business layer. The source code is like below.
public System.Data.DataTable Filldata(string strConnString)
{
qualityDataAccess fillauditReasonDa = new qualityDataAccess();
DataTable getauditReasonDt =new DataTable();
try
{
// The below line is throwing a null reference exception basically here I'm
// I'm calling the dataaccess layer method which is returning
// the DataTable.
getauditReasonDt = fillauditReasonDa.FillAuditReason(strConnString);
if (getauditReasonDt.Rows.Count > 0)
{
return getauditReasonDt;
}
else
{
return null;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
fillauditReasonDa = null;
if (getauditReasonDt != null)
{
getauditReasonDt = null;
getauditReasonDt.Dispose();
}
}
}
Please help me as it need to be solved ASAP. Please let me know if you need any further information.
Hmm, you say you can call DAL method and everything is ok. And then there's a NullReferenceException. First of all, maybe it's just a typo but the DAL method at the top is called Filldata and in BL you call FillAuditReason. So it's the same method (just
the name is different) or it's a different method ? Next thing, if You get NullReferenceException, simply try to debug the code and find what line is throwing the exception.
This issue is resolved. I've used DataTable.Load(SqlCommand.ExecuteReader()) to resolve this issue. I think previously disposing of the datatable in finally was causing the issue. Thanks a ton for your reply.
sumajumd
Member
39 Points
38 Posts
DataTable getting null in business layer
Aug 06, 2010 11:59 AM|LINK
Hi Friends,
I'm preety new to .NET technology and I'm facing a problem with datatable. The scenario is stated below.
My application has a three tier architectute
Here, I'm calling a SP from Data Access layer which will populate a DataTable and return the DataTable back to Business. My source code is like below.
public System.Data.DataTable Filldata(string strConnString) { loginDataAccess loginDa = new loginDataAccess(); SqlConnection Conn= loginDa.createConnection(strConnString); SqlCommand Cmd = new SqlCommand("proc_getAuditReason", Conn); Cmd.CommandType = System.Data.CommandType.StoredProcedure; System.Data.DataTable DT = new System.Data.DataTable(); SqlDataAdapter Sda = new SqlDataAdapter(); try { Conn.Open(); Sda.SelectCommand = Cmd; Sda.Fill(DT); if (DT.Rows.Count > 0) { return DT; } else { return null; } } catch (Exception ex) { throw ex; } finally { Cmd = null; if (Conn != null) { Conn.Close(); Conn.Dispose(); } if (Sda != null) { Sda = null; Sda.Dispose(); } } }Here, when I set debugpoint I can see the DataTable is getting populated by the data so my
sp idreturning the data. there is no issue with the sp and dataaccess layer method. The problem lies below on my business layer. The source code is like below.
public System.Data.DataTable Filldata(string strConnString) { qualityDataAccess fillauditReasonDa = new qualityDataAccess(); DataTable getauditReasonDt =new DataTable(); try { // The below line is throwing a null reference exception basically here I'm // I'm calling the dataaccess layer method which is returning // the DataTable. getauditReasonDt = fillauditReasonDa.FillAuditReason(strConnString); if (getauditReasonDt.Rows.Count > 0) { return getauditReasonDt; } else { return null; } } catch (Exception ex) { throw ex; } finally { fillauditReasonDa = null; if (getauditReasonDt != null) { getauditReasonDt = null; getauditReasonDt.Dispose(); } } }Please help me as it need to be solved ASAP. Please let me know if you need any further information.
macpak
Member
408 Points
132 Posts
Re: DataTable getting null in business layer
Aug 06, 2010 06:25 PM|LINK
Hmm, you say you can call DAL method and everything is ok. And then there's a NullReferenceException. First of all, maybe it's just a typo but the DAL method at the top is called Filldata and in BL you call FillAuditReason. So it's the same method (just the name is different) or it's a different method ? Next thing, if You get NullReferenceException, simply try to debug the code and find what line is throwing the exception.
sumajumd
Member
39 Points
38 Posts
Re: DataTable getting null in business layer
Aug 09, 2010 01:38 PM|LINK
Hi,
This issue is resolved. I've used DataTable.Load(SqlCommand.ExecuteReader()) to resolve this issue. I think previously disposing of the datatable in finally was causing the issue. Thanks a ton for your reply.