C# 值不能为null异常
下面的代码从存储过程中读取数据集,然后将其保存在列表中。存储过程中的数据集工作正常,它返回带有值的表。但当我将它保存到列表中时,我总是遇到异常 代码是C# 值不能为null异常,c#,.net,linq,C#,.net,Linq,下面的代码从存储过程中读取数据集,然后将其保存在列表中。存储过程中的数据集工作正常,它返回带有值的表。但当我将它保存到列表中时,我总是遇到异常 代码是 public class mList { public DateTime Ee { get; set; } public DateTime ndate { get; set; } public int SNo { get; set; }
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被捕获,Message=Value不能为null。我设法复制了您的问题,看起来您的表名需要是行,而不是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();
}
}
使用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();
您是否已经/能够在运行时调试并检查这些值?mList的定义是什么?它允许空值吗?我希望您已经检查过了。您的问题还不清楚,但是数据集中的表真的命名为t0吗?这是mList公共类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;}}也许可以尝试分解LINQ查询以确定它的哪一部分失败。代码看起来很好,但它没有像我的代码中从存储过程中获取数据那样填充存储过程中的数据
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();