数据表为空 [英] DataTable is getting null

查看:230
本文介绍了数据表为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我是.NET技术的新手.目前,我正在构建具有三层体系结构的.NET 3.5应用程序.我正面临数据表的问题.我正在使用数据表将其与下拉列表绑定.问题是,虽然数据表已由我执行的SP填充到DataAccess层中,但在将其返回给业务时却得到了空值,因此导致了空引用异常.我的代码简单明了.

数据访问代码:

Hi,
I''m preety new to .NET technology. Currently I''m building an .NET 3.5 application which has a three tier architecture. I''m facing a problem with a datatable. I''m using a datatable to bind it with a dropdown list. The problem is although the datatable is getting populated in DataAccess layer by the SP I''ve executed but while returning it to Business I''m getting null hence causing a null reference exception. My code is preety straight forward.

DataAccess Code:

public System.Data.DataTable FillData(string strConnString)
        {
            loginDataAccess loginDa = new loginDataAccess();
            SqlConnection Conn = loginDa.createConnection(strConnString);
            SqlCommand Cmd = new SqlCommand("proc_FillData", 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)
                {
                   /* Here the datatable is populating but while returning to the business it is getting null*/
                    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();
                }
             }
        }



因此,DT已正确填充,我已通过在代码区域上放置调试点来对其进行了验证.但是当我在业务层中收集此DataTable时,它给了我一个Null引用对象异常.我的业务层代码如下.



SO, the DT is populating properly I''ve verified it by putting a debug point on the code region. but when I''m collecting this DataTable in business layer It is giving me an Null Reference Object exception. My Business layer code is as below.

 public System.Data.DataTable FillData(string strConnString)
        {
            
            
          qualityDataAccess fillDa = new qualityDataAccess();
            DataTable getauditReasonDt =new DataTable();

            try
            {
/* From here I''m calling the DataAccess layer method and getting a null back while in DataAccess the DataTable is getting populated perfectly*/
                getauditReasonDt = fillDa.FillData(strConnString);

                if (getauditReasonDt.Rows.Count > 0)
                {
                    return getauditReasonDt;
                }
                else
                {
                    return null;
                }
            }
            

            
            catch (Exception ex)
            {
                throw ex;
            }
            
            finally
            {
                fillDa = null;
                if (getauditReasonDt != null)
                {
                    getauditReasonDt = null;
                    getauditReasonDt.Dispose();

                }
          }          
        }



请帮助我解决此问题,因为这非常紧急,需要尽快解决.



Please help me to resolve this issue as this is very urgent and need to be resolved ASAP.

推荐答案

您的代码远远多于完成任务所需的代码.试试这个

You have much more code than is necessary to accomplish the task. Try this

using(SqlConnection conn = new SqlConnection(connString))
{
   using(SqlCommand cmd = new SqlCommand("proc_FillData", conn) )
   {
      cmd.CommandType = CommandType.StoredProcedure;
      conn.Open();
      DataTable dt = new DataTable();
      dt.Load(cmd.ExecuteReader());

      return dt;
   }
}


Sda成为空null的原因是因为此行

终于
{
....
如果(Sda!= null)
{
Sda = null;
Sda.Dispose();
}
}

现在,Sda会返回与您返回的元素相同的元素,并最终始终执行.因此,在返回引用之前,请将其设置为null,然后对其进行处置,因此,您的不良业务层将获得null

因此,请删除这些行,您的代码将起作用.

让我知道您的情况如何
The reason that Sda is beocomin null is because of this line

finally
{
....
if (Sda != null)
{
Sda = null;
Sda.Dispose();
}
}

Now Sda reffers to the same element as you are returning and finally always gets executed. So before returning the reference you are setting it to null and then disposing it hence your poor business layer is getting a null

Thus please remove these lines and your code will work.

Let me know how things turn out for u


因此,gaurav有点正确,但并不是在您代码的正确位置.您的问题实际上是您处置了getauditReasonDT.

这是您的代码在内存中发生的情况(例如).

您可以创建getauditReasonDT来创建一个指针.

地址 类型 数据
0x1 指针


然后将其设置为New Table()(这是完全不必要的,因为以后将其设置为其他名称.
地址 类型 数据
0x1 指针 0x2
0x2 数据表 空白数据表


然后,将getauditReasonDT返回,这实际上只是传递数据在内存中的位置(因此,在这种情况下,它只是传递0x2.

然后,在内存中设置dispose,以将数据表的处置方式设置为使这些指针无处指向.

至少我是这样理解的.

据我所知,处置DataAdapter不会处置它填充的DataTable.我一直在我的代码中执行此操作,从来没有任何问题.
So, gaurav is somewhat right, but not in the right spot of your code. Your problem is actually that you dispose of getauditReasonDT.

Here''s what happens in memory (as an example) with your code.

You create getauditReasonDT which creates a pointer.

AddressTypeData
0x1PointerNull


You then set it to New Table() (which is total unnecessary since you later set it to something else.
AddressTypeData
0x1Pointer0x2
0x2DataTableBlank DataTable


Then, you pass getauditReasonDT back which is really only passing the location of the data in memory (so in this case, it''s just passing 0x2.

Then, you set dispose which disposes of the datatable in memory so those pointers have nowhere to point.

At least that''s how I understand it.

And as far as I know, dispose of the DataAdapter doesn''t dispose of the DataTable that it filled. I do this in my code all of the time and never have any problems.


这篇关于数据表为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆