views:

37

answers:

1

Hello fellows, good day. I am using an OLEDB connection to connect to DB2 database. I am having problems mapping the dates from database inside .NET.

In my business object I defined a private DateTime _genftmdpdate=DateTime.MinValue; But whenever I fetch the date from database and populate inside my variable I get Specified Cast not valid error

myftModInstall.genFtMDPDate = myRecord.GetDateTime(myRecord.GetOrdinal("GENFTMDPDATE"))

Please help me as I don't want to convert the date into string all the time.

Edit --- This won't work as well myftModInstall.genFtMDPDate = Convert.ToDateTime(myRecord.GetType((System.Data.OleDb.OleDbType.Date)(myRecord.GetOrdinal("GENFTMDPDATE"))));

A: 

Make sure that you check for a NULL before attempting to retrieve a value.

if(!myRecord.IsDBNull(myRecord.GetOrdinal("GENFTMDPDATE"))
    myftModInstall.genFtMDPDate = myRecord.GetDateTime(myRecord.GetOrdinal("GENFTMDPDATE"));
Hiya I have done that exactly. It does not fix the problem. The problem lies in fetching the date from datatabase 'myRecord.GetDateTime', if I change it to myRecord.GetString it works but then I have to do whole conversion to date thing.
Popo
What's the SQL data type for the database column?
In database it is of DATE type. But I can never manage to read it as date by myrecord.GetDate. GetString works only.
Popo
What do you get from `myRecord.GetFieldType(myRecord.GetOrdinal("GENFTMDPDATE"))`?