Table is nullable DateTime, but DataSet throws an exception?

前端 未结 9 1202
悲哀的现实
悲哀的现实 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:25

    It seems different with DataTable and Strongly Typed DataTable... Use Like this.

    DataSet1.DataTable1DataTable table = new DataSet1.DataTable1DataTable();
    table.Merge(adapter.GetData().CopyToDataTable());
    
    0 讨论(0)
  • 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<DateTime?> targetTime = new List<DateTime?>();
                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);
    
                }
    
    0 讨论(0)
  • 2020-12-13 15:29

    I did this to insert value NULL in a DateTime column,

    assuming that I have a nullable DateTime column in Database, I retrieved some data from the database in an object called response and I want to insert nullable DateTime value in the DataSet column that called RenewDate:

    // create anew row of the same type of your table row
    var rw = ds.StudentActionPrintDT.NewStudentActionPrintDTRow();
    
    // check for null value
    if(!response.RenewDate.HasValue)
    {
      // if null, then the let DataSet to set it null by it's own way 
      rw.SetRenewDateNull();
    }
    else
    {
      // if not null set value to the datetime value
      rw.RenewDate = response.RenewDate.Value;
    }
    // add the created row to the dateset [DataSetName].[ColumnName].Add[ColumnName]Row([The Created Row]);
    ds.StudentActionPrintDT.AddStudentActionPrintDTRow(rw);
    
    0 讨论(0)
提交回复
热议问题