DataSet does not support System.Nullable<> exception in c#

試著忘記壹切 提交于 2019-12-20 01:46:13

问题


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;

        } 
}

i am trying generate crystal report there in sql table SPartSalePrice is nullable due to that at c.SetDataSource(d); exception come please solve it


回答1:


Use the null coalescing or conditional operators in your anonymous projection to map out the null:

Coalescing:

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

Conditional (Not really useful for nulls, but useful for projecting other values)

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

The field needs to be given a name (I've kept the original one, SPartSalePrice), and the type of substitution (0.0) should match the type of the field.




回答2:


maybe One of your object values is Null. try something like that

        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;

    } 



回答3:


Try this :

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;

        } 
}

you should check for null in the selection with

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

whetherit is null or not if it is null then it will return 0 otherwise it returns the value and assign it to the new variable

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



回答4:


iF Data Type is String then

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

which show blank if data is null else show data.

If data type is int then

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



回答5:


Check in your class if you have only one attribute nullable like this : decimal? , put it in decimal. For all of your attributes which have the question mark in there declaration, delete the question mark simply and execute.



来源:https://stackoverflow.com/questions/18910210/dataset-does-not-support-system-nullable-exception-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!