Table is nullable DateTime, but DataSet throws an exception?

前端 未结 9 1207
悲哀的现实
悲哀的现实 2020-12-13 14:42

I\'m attempting to use the DataSet designer to create a datatable from a query. I got this down just fine. The query used returns a nullable datetime column from the datab

9条回答
  •  北海茫月
    2020-12-13 15:29

    Thanks this solved my similar issue : Here is the code. In case of this question

    Isevent_start_date()
    

    would return whether or not the field is null.

    In my case: I had a similar problem and I used the following solution

                //Table's Name is Efforts,
                //Column's name is Target
                //So the dataset would automatically generate a property called IsTargetNull() which can be used to check nullables
                //Create an Adaptor
                EffortsTableAdapter ad = new EffortsTableAdapter();
                ProjectDashBoard.Db.EffortsDataTable efforts = ad.GetData();
                DataColumn targetColumn = new DataColumn();
                targetColumn = efforts.TargetColumn;
    
                List targetTime = new List();
                foreach (var item in efforts)
                {
    
                    //----------------------------------------------------
                    //This is the line that we are discussing about : 
                    DateTime? myDateTime = item.IsTargetNull() ? null : (DateTime?)item.Target;
                    //----------------------------------------------------
    
                    targetTime.Add(myDateTime);
    
                }
    

提交回复
热议问题