值不能为null异常

时间:2013-06-13 21:04:42

标签: c# .net linq

我有以下代码从存储过程中读取数据集,而不是将其保存在列表中。存储过程中的数据集工作正常,它返回带有值的表。但是在将它保存到List中时我会一直遇到异常。

代码是

    public class mList
        {
            public DateTime Ee { get; set; }
            public DateTime ndate { get; set; }
            public int SNo { get; set; }
            public int CId { get; set; }
            public int rID { get; set; }
        }

    internal class Program
    {
        private static void Main(string[] args)
        {
     string procName = "listest";
                SqlConnection conn=new SqlConnection(cs);
                try
                {
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = new SqlCommand(procName, conn);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;

                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    ds.Tables["t0"].TableName = "Rows";

        List<mL> newlist = ds.Tables["t0"].AsEnumerable().Select(row => new mList
        {
            Ee = row.Field<DateTime?>(0).GetValueOrDefault(),
            ndate = row.Field<DateTime?>(1).GetValueOrDefault(),
            SNo = row.Field<int?>(2).GetValueOrDefault(),
            CId = row.Field<int?>(3).GetValueOrDefault(),
            rID = row.Field<int?>(4).GetValueOrDefault()
        }).ToList();
    }
}

我得到的例外是System.ArgumentNullException was caught, Message=Value cannot be null.

1 个答案:

答案 0 :(得分:1)

我设法复制了你的问题,看起来你的表名必须是“Rows”而不是“t0”。您在代码中将表重命名为更高。评论中有人已提到这一点。我在控制台应用程序中重新创建了您的示例,以下工作原理:

注意:该示例基于一些假设。 您的数据类型是正确的,查询将根据mList对象定义返回。我可以假设这个,因为如果不是,你会得到一个演员异常。

class Program
{
    public class mList
    {
        public DateTime Ee { get; set; }
        public DateTime ndate { get; set; }
        public int SNo { get; set; }
        public int CId { get; set; }
        public int rID { get; set; }
    }

    static void Main(string[] args)
    {

        DataSet ds = new DataSet();

        DataTable t = new DataTable("Rows");
        t.Columns.Add("ee", typeof(DateTime));
        t.Columns.Add("ndate", typeof(DateTime));
        t.Columns.Add("sno", typeof(int));
        t.Columns.Add("cid", typeof(int));
        t.Columns.Add("rid", typeof(int));

        t.Rows.Add(DateTime.Now, DateTime.Now, 0, 1, null);
        t.Rows.Add(DateTime.Now, DateTime.Now, 2, 1, 2);
        t.Rows.Add(DateTime.Now, DateTime.Now, 4, 1, 1);
        t.Rows.Add(DateTime.Now, DateTime.Now, 5, 1, 1);

        ds.Tables.Add(t);

        //IF TABLE IS t0 - You get a null reference exception
        List<mList> newlist = ds.Tables["Rows"].AsEnumerable().Select(row => new mList
        {
            Ee = row.Field<DateTime?>(0).GetValueOrDefault(),
            ndate = row.Field<DateTime?>(1).GetValueOrDefault(),
            SNo = row.Field<int?>(2).GetValueOrDefault(),
            CId = row.Field<int?>(3).GetValueOrDefault(),
            rID = row.Field<int?>(4).GetValueOrDefault()
        }).ToList();
    }

}

以下是使用存储过程的示例。注意我的初始表名是Table而不是t0。

存储过程是模拟存储过程:

CREATE PROCEDURE ListTest
AS
BEGIN
    SELECT  GETDATE()+1 AS EE
            ,GETDATE()-1 AS ndate
            ,1 AS SNo,2 AS CId,NULL AS rID
    UNION ALL
    SELECT  GETDATE()+1
            ,GETDATE()-1
            ,1 ,2, 2
    UNION ALL --TEST WITH ALL NULL VALUES
    SELECT  NULL
            ,NULL
            ,NULL ,NULL, NULL
END

使用c#修改控制台应用程序:

string procName = "listtest";
string cs = "Data Source=server;Initial Catalog=dbname;User ID=user;Password=password;Trusted_Connection=False;";
SqlConnection conn=new SqlConnection(cs);

SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(procName, conn);
da.SelectCommand.CommandType = CommandType.StoredProcedure;

DataSet ds = new DataSet();
da.Fill(ds);
ds.Tables["Table"].TableName = "Rows";

List<mList> newlist = ds.Tables["Rows"].AsEnumerable().Select(row => new mList
{
    Ee = row.Field<DateTime?>(0).GetValueOrDefault(),
    ndate = row.Field<DateTime?>(1).GetValueOrDefault(),
    SNo = row.Field<int?>(2).GetValueOrDefault(),
    CId = row.Field<int?>(3).GetValueOrDefault(),
    rID = row.Field<int?>(4).GetValueOrDefault()
}).ToList();
相关问题