DataSet不支持System.Nullable<> c#中的异常

时间:2013-09-20 06:26:51

标签: c# dataset

public partial class Form2 : Form
{
        public Form2()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            RST_DBDataContext db = new RST_DBDataContext();
            var d = (from s in db.TblSpareParts
                                        select new {  s.SPartName, s.SPartCode, s.ModelID, s.SPartLocation,  s.SPartActive, s.SPartSalePrice }).ToArray();
            CrystalReport1 c = new CrystalReport1();
            c.SetDataSource(d);
            crystalReportViewer1.ReportSource = c;

        } 
}

我正在尝试生成水晶报告 在sql表中,由于在c.SetDataSource(d),SPartSalePrice可以为空;例外来请解决它

5 个答案:

答案 0 :(得分:6)

使用匿名投影中的null coalescingconditional运算符来映射null

<强>聚结

var d = (from s in db.TblSpareParts
  select new 
  { 
    s.SPartName,
    ...,
    SPartSalePrice = s.SPartSalePrice ?? 0.0,
    ...
  }).ToArray();

条件(对空值不太有用,但对投影其他值很有用)

  SPartSalePrice = s.SPartSalePrice == null ? 0.0 : s.SPartSalePrice,

该字段需要提供一个名称(我保留了原始名称SPartSalePrice),替换type0.0)应与字段类型匹配

答案 1 :(得分:4)

也许您的一个对象值是Null。尝试类似的东西

        private void Form2_Load(object sender, EventArgs e)
    {
        RST_DBDataContext db = new RST_DBDataContext();
        var d = (from s in db.TblSpareParts
                                    select new {  
                                                    s.SPartName?? DBNull.Value, 
                                                    s.SPartCode?? DBNull.Value, 
                                                    s.ModelID ?? DBNull.Value, 
                                                    s.SPartLocation ?? DBNull.Value,  
                                                    s.SPartActive ?? DBNull.Value, 
                                                    s.SPartSalePrice ?? DBNull.Value, 
                                                }).ToArray();
        CrystalReport1 c = new CrystalReport1();
        c.SetDataSource(d);
        crystalReportViewer1.ReportSource = c;

    } 

答案 2 :(得分:1)

试试这个:

public partial class Form2 : Form
{
        public Form2()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            RST_DBDataContext db = new RST_DBDataContext();
    var d = (from s in db.TblSpareParts
             select new { s.SPartName, s.SPartCode, s.ModelID, s.SPartLocation, s.SPartActive, newPartSalePrice = s.SPartSalePrice == null ? 0 : s.SPartSalePrice }).ToArray();
    CrystalReport1 c = new CrystalReport1();
    c.SetDataSource(d);
    crystalReportViewer1.ReportSource = c;

        } 
}

您应该使用

检查选择中的null
s.SPartSalePrice == null ? 0 : s.SPartSalePrice 

如果为null,则为null,则返回0,否则返回该值并将其赋值给新变量

newPartSalePrice = s.SPartSalePrice == null ? 0 : s.SPartSalePrice 

答案 3 :(得分:1)

iF数据类型是字符串然后

SPartSalePrice =s.SPartSalePrice == null ? "" : s.SPartSalePrice;

如果数据为空则显示空白,否则显示数据。

如果数据类型是int,那么

PartSalePrice = s.SPartSalePrice == null ? 0 : s.SPartSalePrice 

答案 4 :(得分:0)

如果您只有一个可以为空的属性,请检查您的班级:decimal? ,把它放在十进制。对于那些在声明中有问号的属性,只需删除问号并执行即可。