How to retrieve TimeSpan from typed DataSet?

  • Thread starter Thread starter deko
  • Start date Start date
D

deko

I keep getting an InvalidCast exception when trying to do this:

DataRow row = dtP.Rows.Find(projectID);
bakInterval = new TimeSpan();
bakInterval = (TimeSpan)row[colBI]; //colBI is a string const

Error Message: "Specified cast is not valid".

As far as I know, the rowBI column is in fact a TimeSpan. Below is how the
dtP table is created. The last line shows colBI created as a TimeSpan type.

DataTable tblProject = database.Tables.Add("TableProject");
DataColumn project_ID = tblProject.Columns.Add("Project_ID",
typeof(Int32));
project_ID.AllowDBNull = false;
project_ID.Unique = true;
project_ID.AutoIncrement = true;
project_ID.AutoIncrementSeed = 1;
project_ID.AutoIncrementStep = 1;
tblProject.PrimaryKey = new DataColumn[] { tblProject.Columns[0] };
tblProject.Columns.Add("ProjectName", typeof(string));
tblProject.Columns.Add("BackupInterval", typeof(TimeSpan));

I can cast ProjectName to a string, and Project_ID to an int, but I can't
cast BackupInterval to a TimeSpan.

What am I doing wrong?

Thanks in advance.
 
Back
Top