Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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;

        } 
}

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

like image 311
Rizwan Ahmed Avatar asked Apr 21 '26 21:04

Rizwan Ahmed


2 Answers

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.

like image 68
StuartLC Avatar answered Apr 23 '26 11:04

StuartLC


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;

    } 
like image 40
Giorgi Lobjanidze Avatar answered Apr 23 '26 12:04

Giorgi Lobjanidze