DataTable getting null in business layerhttp://forums.asp.net/t/1587289.aspx/1?DataTable+getting+null+in+business+layerMon, 09 Aug 2010 13:38:54 -040015872894013453http://forums.asp.net/p/1587289/4013453.aspx/1?DataTable+getting+null+in+business+layerDataTable getting null in business layer <p>&nbsp;Hi Friends,</p> <p>I'm preety new to .NET technology and I'm facing a problem with datatable. The scenario is stated below.</p> <p>My application has a three tier architectute</p> <ul> <li>UI </li><li>Business </li><li>DataAccess</li></ul> <p>Here, I'm calling a SP from Data Access layer which will populate a DataTable and return the DataTable back to Business.&nbsp;My source code is like below.</p> <pre class="prettyprint">public System.Data.DataTable Filldata(string strConnString) { loginDataAccess loginDa = new loginDataAccess(); SqlConnection Conn= loginDa.createConnection(strConnString); SqlCommand Cmd = new SqlCommand(&quot;proc_getAuditReason&quot;, 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 &gt; 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(); } } }</pre> <P>Here, when I set debugpoint I can see the DataTable is getting populated by the data so my</P> <P>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.</P><pre class="prettyprint">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 &gt; 0) { return getauditReasonDt; } else { return null; } } catch (Exception ex) { throw ex; } finally { fillauditReasonDa = null; if (getauditReasonDt != null) { getauditReasonDt = null; getauditReasonDt.Dispose(); } } }</pre> <p><br> Please help me as it need to be solved ASAP. Please let me know if you need any further information.</p> <p>&nbsp;&nbsp;<br> </p> <p>&nbsp;</p> <p>&nbsp;</p> 2010-08-06T11:59:27-04:004014058http://forums.asp.net/p/1587289/4014058.aspx/1?Re+DataTable+getting+null+in+business+layerRe: DataTable getting null in business layer <p>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.&nbsp;</p> 2010-08-06T18:25:52-04:004017394http://forums.asp.net/p/1587289/4017394.aspx/1?Re+DataTable+getting+null+in+business+layerRe: DataTable getting null in business layer <p>Hi,</p> <p>This issue is resolved. I've used DataTable.Load(SqlCommand.ExecuteReader()) to resolve this issue. I think previously&nbsp;disposing of the datatable in finally was causing the issue. Thanks a ton for your reply.</p> <p>&nbsp;</p> <p>&nbsp;&nbsp;</p> 2010-08-09T13:38:54-04:00