Data Serialization ?

  • Thread starter Thread starter WJ
  • Start date Start date
W

WJ

Hello,

I got an exception telling me that Invalid Casting on a Date field. Here is
what I attempted to do in my Asp.Net app:

1. A class that contains 4 fields called Employee Record (eye), two out of 4
fields are DateTime type. The class name is "eyeClass".

******** Code sample *********

using System;

namespace hr.eye
{
[Serializable]
public class eyeClass
{
private string _LastName;
private string _FirstName;
private DateTime _BirthDate;
private DateTime _HireDate;

public eyeClass()
{
}

public eyeClass
(
string LastName
,string FirstName
,DateTime BirthDate
,DateTime HireDate
)

{
this._LastName=LastName;
this._FirstName=FirstName;
this._BirthDate=BirthDate;
this._HireDate=HireDate;
}

public string LastName {get{return _LastName;}}
public string FirstName {get{return _FirstName;}}
public DateTime BirthDate {get{return _BirthDate;}}
public DateTime HireDate {get{return _HireDate;}}
}
}
On Asp.Net Page Behind code (onReadDataOKClick button): I have the following
codes:

DataSet ds=new DataSet();
Call SqlHelper.FillDataset to extract data into ds; //it returned two
tables with data (one record per table).

//this instruction below attempts to fill "eyeClass" with data retrieved
from DataSet .
//Exception is thrown when any DateField is NULL. (Invalid Casting)

eyeClass eye=new eyeClass(
ds.Tables[0].Rows[0]["LastName"].ToString()
,ds.Tables[0].Rows[0]["FirstName"].ToString()
,(DateTime)ds.Tables[0].Rows[0]["BirthDate"]
,(DateTime)ds.Tables[0].Rows[0]["HireDate"]);

******** Code sample Ended *********

Problem: The exception is thrown on the HireDate because it is NULL and I
need it to be nulled in some cases. I cannot trap the message here with the
TRY/Catch.

My question is: How can I force the application just go ahead to eat the
"exception"... and move on instead of exiting the program completely.

Thanks

John
 
Why not take the information out first and then create the object. Something
like:

if(ds.Tables[0].Rows[0]["DateField"]==DbNull.Value)
tempDateHolder=ds.Tables[0].Rows[0]["DateField"];
else
tempDateHolder=new DateTime(1,0,0,0,0,0);

Then, in the class you can test for the "null" date.

If you use strongly typed DataSets, you can test for a NULL more elegantly.

Unfortunately, you cannot blow through an error on a constructor.

---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************

WJ said:
Hello,

I got an exception telling me that Invalid Casting on a Date field. Here is
what I attempted to do in my Asp.Net app:

1. A class that contains 4 fields called Employee Record (eye), two out of 4
fields are DateTime type. The class name is "eyeClass".

******** Code sample *********

using System;

namespace hr.eye
{
[Serializable]
public class eyeClass
{
private string _LastName;
private string _FirstName;
private DateTime _BirthDate;
private DateTime _HireDate;

public eyeClass()
{
}

public eyeClass
(
string LastName
,string FirstName
,DateTime BirthDate
,DateTime HireDate
)

{
this._LastName=LastName;
this._FirstName=FirstName;
this._BirthDate=BirthDate;
this._HireDate=HireDate;
}

public string LastName {get{return _LastName;}}
public string FirstName {get{return _FirstName;}}
public DateTime BirthDate {get{return _BirthDate;}}
public DateTime HireDate {get{return _HireDate;}}
}
}
On Asp.Net Page Behind code (onReadDataOKClick button): I have the following
codes:

DataSet ds=new DataSet();
Call SqlHelper.FillDataset to extract data into ds; //it returned two
tables with data (one record per table).

//this instruction below attempts to fill "eyeClass" with data retrieved
from DataSet .
//Exception is thrown when any DateField is NULL. (Invalid Casting)

eyeClass eye=new eyeClass(
ds.Tables[0].Rows[0]["LastName"].ToString()
,ds.Tables[0].Rows[0]["FirstName"].ToString()
,(DateTime)ds.Tables[0].Rows[0]["BirthDate"]
,(DateTime)ds.Tables[0].Rows[0]["HireDate"]);

******** Code sample Ended *********

Problem: The exception is thrown on the HireDate because it is NULL and I
need it to be nulled in some cases. I cannot trap the message here with the
TRY/Catch.

My question is: How can I force the application just go ahead to eat the
"exception"... and move on instead of exiting the program completely.

Thanks

John
 
Cowboy (Gregory A. Beamer) - MVP said:
Why not take the information out first and then create the object.
Something
like:

if(ds.Tables[0].Rows[0]["DateField"]==DbNull.Value)
tempDateHolder=ds.Tables[0].Rows[0]["DateField"];
else
tempDateHolder=new DateTime(1,0,0,0,0,0);

Thanks Cowboy. But this is too obvious. And filling the date field with a
bogus date is prohibited in my project. It is either valid or dbNull.Value.
Nothing else.

John
 
Back
Top