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

后端 未结 5 1036
伪装坚强ぢ
伪装坚强ぢ 2021-01-21 01:47
public partial class Form2 : Form
{
        public Form2()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArg         


        
5条回答
  •  庸人自扰
    2021-01-21 02:40

    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 
    

提交回复
热议问题