ASP.NET 2.0 and ObjectDataBinding

D

dm1608

I'm doing an ASP.NET 2.0 project where I have a BAL and DAL layer. I'm
passing a DataSet from the DAL to the BAL. The BAL is passing generics back
to the presentation. All works fine, however, I noticed a problem with my
code that does something like this in the BAL:


List<JobSummary> jobSummary = new List<JobSummary>();

foreach (DataRow row in ds.Tables[0].Rows)
{
jobSummary.Add(new JobSummaryClass((Int32)row["JobNum"],
(DateTime)row["StartDate"],(DateTime)row["EndDate"]));
}

return jobSummary


I'm receiving an INvalidCastException on my jobSummary.Add() above. I
noticed that on some rows, my EndTime is null. I'm temporarily fixed this
by enclosing the code in a try/catch and not rethrowing the exception. But
then this record is skipped.

What are my options with how to fix this?
 
G

Guest

You can use


if (row["StartDate"] != DBNull.Value)
{
//
}

or
if (!row.IsNull("StartDate"))
{
//
}


HTH

Elton Wang
 
D

dm1608

But I need to assign NULL to the EndTime DateTime variable in order to
populate my collection for display. How do I get around this?




Elton W said:
You can use


if (row["StartDate"] != DBNull.Value)
{
//
}

or
if (!row.IsNull("StartDate"))
{
//
}


HTH

Elton Wang

dm1608 said:
I'm doing an ASP.NET 2.0 project where I have a BAL and DAL layer. I'm
passing a DataSet from the DAL to the BAL. The BAL is passing generics
back
to the presentation. All works fine, however, I noticed a problem with
my
code that does something like this in the BAL:


List<JobSummary> jobSummary = new List<JobSummary>();

foreach (DataRow row in ds.Tables[0].Rows)
{
jobSummary.Add(new JobSummaryClass((Int32)row["JobNum"],
(DateTime)row["StartDate"],(DateTime)row["EndDate"]));
}

return jobSummary


I'm receiving an INvalidCastException on my jobSummary.Add() above. I
noticed that on some rows, my EndTime is null. I'm temporarily fixed
this
by enclosing the code in a try/catch and not rethrowing the exception.
But
then this record is skipped.

What are my options with how to fix this?
 
R

Rune B

But I need to assign NULL to the EndTime DateTime variable in order to
populate my collection for display. How do I get around this?

How about using a Nullable DateTime? - and then don't assign anything if the
value is DBNull.

class JobSummaryItem
{
// ...

public DateTime? EndTime
{
get { ... }
}
}
 
D

dm1608

Hi --

thanks for the reply.

Can you further describe this. Is DateTime? EndTime some sort of C#
shorthand?

Basically, the database has NULL for the EndTime and I still need to display
it as a blank field within the GridView. The issue is that my JobSummary
class is defined as accepting the EndTime as a DateTime datatype. I guess I
cannot assign NULL to a DateTime datatype???
 
G

Guest

You can assign to DateTime.MinValue

HTH

Elton


dm1608 said:
But I need to assign NULL to the EndTime DateTime variable in order to
populate my collection for display. How do I get around this?




Elton W said:
You can use


if (row["StartDate"] != DBNull.Value)
{
//
}

or
if (!row.IsNull("StartDate"))
{
//
}


HTH

Elton Wang

dm1608 said:
I'm doing an ASP.NET 2.0 project where I have a BAL and DAL layer. I'm
passing a DataSet from the DAL to the BAL. The BAL is passing generics
back
to the presentation. All works fine, however, I noticed a problem with
my
code that does something like this in the BAL:


List<JobSummary> jobSummary = new List<JobSummary>();

foreach (DataRow row in ds.Tables[0].Rows)
{
jobSummary.Add(new JobSummaryClass((Int32)row["JobNum"],
(DateTime)row["StartDate"],(DateTime)row["EndDate"]));
}

return jobSummary


I'm receiving an INvalidCastException on my jobSummary.Add() above. I
noticed that on some rows, my EndTime is null. I'm temporarily fixed
this
by enclosing the code in a try/catch and not rethrowing the exception.
But
then this record is skipped.

What are my options with how to fix this?
 
R

Rune B

Can you further describe this. Is DateTime? EndTime some sort of C#
shorthand?

Basically, the database has NULL for the EndTime and I still need to
display it as a blank field within the GridView. The issue is that my
JobSummary class is defined as accepting the EndTime as a DateTime
datatype. I guess I cannot assign NULL to a DateTime datatype???

Not really, the Nullable class encapsulates the DateTime type.. it is really
a generic class, used for this purpose ... to make value types able to be
null
type is NullAble<DateTime>
shorthand is DateTime?

But You can have a field like this:

private DateTime? _endtime = null;

// and later aks if it has a value

if(_endtime == null)
{
_endtime = DateTime.Now;
}
return _endtime.Value;


and so on ... I find it very usable for values where it is important to
track whether thay have been assigned or not.
 
D

dm1608

I'm still having issues with trying to use the DateTime? within my classes.
I will attempt to work on it again tomorrow and post more code here if I
can't get it to work. Even though I changed all my references from
DateTime to DateTime?, it looks like it still doesn't like the NULL coming
back from the database.
 
R

Rune B

That could be the fact that row["EndDate"] really returns DBNull and
not 'null'

- you should consider doing what 'Elton W' suggested in the very first reply
 
D

dm1608

I'm still working on this and haven't spent much time today on it. Stayed
up late last night trying different things.

My DAL is returning a DataSet to the BAL.

My BAL is looping thru each record of the DataSet and adding each field to
my generic collection of type JobSummary.

The generic collection from the BAL is returned to ASP.NET ObjectDataSource
for display within a GridView control.

I verified that when I receive an InvalidCastException that the
row["EndTime"] field is indeed null. The type shows sqlDBNull in the watch
window.

Using the nullable DateTime? time doesn't appear to help. Trying to pass
the fields to my Add() collection fails each time... and I changed all the
DateTime references within my type class to use DateTime?.

Looking back at "Elton W" suggestion doesn't make much sense to me. I mean,
even if I verify that it is null or not, then what? I still got to pass a
valid DateTime type to my collection.

I want to pass either a valid date (which appears to work when the field
contains it) or NULL so that the GridView will simply display blank for that
item. Essentially... a job is still running if it doesn't have an EndTime.
So it's a perfectly valid case.




Rune B said:
That could be the fact that row["EndDate"] really returns DBNull and
not 'null'

- you should consider doing what 'Elton W' suggested in the very first
reply


dm1608 said:
I'm still having issues with trying to use the DateTime? within my
classes. I will attempt to work on it again tomorrow and post more code
here if I can't get it to work. Even though I changed all my references
from DateTime to DateTime?, it looks like it still doesn't like the NULL
coming back from the database.
 
G

Guest

if it's DBNull.Value, you can set EndTime to DateTime.MaxValue. In GridView,
if it's DateTime.MaxValue show Not End.

HTH

Elton Wang

dm1608 said:
I'm still working on this and haven't spent much time today on it. Stayed
up late last night trying different things.

My DAL is returning a DataSet to the BAL.

My BAL is looping thru each record of the DataSet and adding each field to
my generic collection of type JobSummary.

The generic collection from the BAL is returned to ASP.NET ObjectDataSource
for display within a GridView control.

I verified that when I receive an InvalidCastException that the
row["EndTime"] field is indeed null. The type shows sqlDBNull in the watch
window.

Using the nullable DateTime? time doesn't appear to help. Trying to pass
the fields to my Add() collection fails each time... and I changed all the
DateTime references within my type class to use DateTime?.

Looking back at "Elton W" suggestion doesn't make much sense to me. I mean,
even if I verify that it is null or not, then what? I still got to pass a
valid DateTime type to my collection.

I want to pass either a valid date (which appears to work when the field
contains it) or NULL so that the GridView will simply display blank for that
item. Essentially... a job is still running if it doesn't have an EndTime.
So it's a perfectly valid case.




Rune B said:
That could be the fact that row["EndDate"] really returns DBNull and
not 'null'

- you should consider doing what 'Elton W' suggested in the very first
reply


dm1608 said:
I'm still having issues with trying to use the DateTime? within my
classes. I will attempt to work on it again tomorrow and post more code
here if I can't get it to work. Even though I changed all my references
from DateTime to DateTime?, it looks like it still doesn't like the NULL
coming back from the database.
 
R

Rune B

Using the nullable DateTime? time doesn't appear to help. Trying to pass
the fields to my Add() collection fails each time... and I changed all the
DateTime references within my type class to use DateTime?.

Looking back at "Elton W" suggestion doesn't make much sense to me. I
mean, even if I verify that it is null or not, then what? I still got to
pass a valid DateTime type to my collection.

I think what he suggested was instead of the very optimistic filling:
(DateTime)row["EndDate"]

you should check whether content is null or not first, (wrap it in a
function)

DateTime? enddate;
object obj = row["EndDate"];
if(obj != null)
enddate = (DateTime);
 
D

dm1608

Hi -- can you clarify the DateTime.MaxDate. I'm not familiar with what
this is even for or how it can be used, much less the DateTime.MinDate.



Elton W said:
if it's DBNull.Value, you can set EndTime to DateTime.MaxValue. In
GridView,
if it's DateTime.MaxValue show Not End.

HTH

Elton Wang

dm1608 said:
I'm still working on this and haven't spent much time today on it.
Stayed
up late last night trying different things.

My DAL is returning a DataSet to the BAL.

My BAL is looping thru each record of the DataSet and adding each field
to
my generic collection of type JobSummary.

The generic collection from the BAL is returned to ASP.NET
ObjectDataSource
for display within a GridView control.

I verified that when I receive an InvalidCastException that the
row["EndTime"] field is indeed null. The type shows sqlDBNull in the
watch
window.

Using the nullable DateTime? time doesn't appear to help. Trying to pass
the fields to my Add() collection fails each time... and I changed all
the
DateTime references within my type class to use DateTime?.

Looking back at "Elton W" suggestion doesn't make much sense to me. I
mean,
even if I verify that it is null or not, then what? I still got to pass
a
valid DateTime type to my collection.

I want to pass either a valid date (which appears to work when the field
contains it) or NULL so that the GridView will simply display blank for
that
item. Essentially... a job is still running if it doesn't have an
EndTime.
So it's a perfectly valid case.




Rune B said:
That could be the fact that row["EndDate"] really returns DBNull
and
not 'null'

- you should consider doing what 'Elton W' suggested in the very first
reply


I'm still having issues with trying to use the DateTime? within my
classes. I will attempt to work on it again tomorrow and post more
code
here if I can't get it to work. Even though I changed all my
references
from DateTime to DateTime?, it looks like it still doesn't like the
NULL
coming back from the database.
 
D

dm1608

Thanks for all that replied. I did get this working finally last night. I
guess my interpration was the DBNull was the same as "null" in .NET.
Apparently not.

I basically changed the line that read (DateTime?)row["EndDate"] to do a
tenary check:

DateTime? TimeEnd;

TimeEnd = row["TimeEnd"] == DBNull.Value ? null : (DateTime?)row["TimeEnd"];

This seems to work fine and displays as an empty cell when listing in
GridView; which was what I wanted.

Of course, I need to now do the same for TimeStart and any other
SmallDateTime fields I have within SQL that may return NULLs.

This seems like a common mistake that folks would make when using
ObjectDataSource and I'm surprised that there has really been no mention of
this anywhere in the many examples that I've looked at. I guess
trial-and-error and posting in these newsgroups is the only way to figure
these sorts of "opportunities" out.


Now for another question ---

Since I'm creating a type class for all the fields within my DAL that is
returned to my BAL, is there an easy way to create the class?

Currently, I'm doing something like:

private Int16 _JobNum;
private string _Status;
private DateTime? _TimeStart;
private DateTime? _TimeEnd;
private DateTime? _JobName;

Then I do my constructor and the get/set functions...

public Int16 JobNum
{
get { return _JobNum; }
set { _JobNum = value; }
}

If my query in the DAL has 15 fields, it takes a while to create my type
class for this. I'm wondering if I can easily do this thru Visual Studio
or some other product.

Any help or recommendations would be appreciated.

Thanks all!




Rune B said:
Using the nullable DateTime? time doesn't appear to help. Trying to pass
the fields to my Add() collection fails each time... and I changed all
the DateTime references within my type class to use DateTime?.

Looking back at "Elton W" suggestion doesn't make much sense to me. I
mean, even if I verify that it is null or not, then what? I still got
to pass a valid DateTime type to my collection.

I think what he suggested was instead of the very optimistic filling:
(DateTime)row["EndDate"]

you should check whether content is null or not first, (wrap it in a
function)

DateTime? enddate;
object obj = row["EndDate"];
if(obj != null)
enddate = (DateTime);
 
E

Elton Wang

As you mentioned, you need pass EndDate (type as DateTime) to your
JobSummary object. DateTime is value type object. You cannot assign null
value to it. The workaround to value type object is to assign either
MinValue or MaxValue (they still in same type of object, e.g.
DateTime.MaxValue is type of DateTime.). So later on you know it's an
un-normal value (like null) need to specially deal with it.



HTH



dm1608 said:
Hi -- can you clarify the DateTime.MaxDate. I'm not familiar with what
this is even for or how it can be used, much less the DateTime.MinDate.



Elton W said:
if it's DBNull.Value, you can set EndTime to DateTime.MaxValue. In
GridView,
if it's DateTime.MaxValue show Not End.

HTH

Elton Wang

dm1608 said:
I'm still working on this and haven't spent much time today on it.
Stayed
up late last night trying different things.

My DAL is returning a DataSet to the BAL.

My BAL is looping thru each record of the DataSet and adding each field
to
my generic collection of type JobSummary.

The generic collection from the BAL is returned to ASP.NET
ObjectDataSource
for display within a GridView control.

I verified that when I receive an InvalidCastException that the
row["EndTime"] field is indeed null. The type shows sqlDBNull in the
watch
window.

Using the nullable DateTime? time doesn't appear to help. Trying to
pass
the fields to my Add() collection fails each time... and I changed all
the
DateTime references within my type class to use DateTime?.

Looking back at "Elton W" suggestion doesn't make much sense to me. I
mean,
even if I verify that it is null or not, then what? I still got to
pass a
valid DateTime type to my collection.

I want to pass either a valid date (which appears to work when the field
contains it) or NULL so that the GridView will simply display blank for
that
item. Essentially... a job is still running if it doesn't have an
EndTime.
So it's a perfectly valid case.




That could be the fact that row["EndDate"] really returns DBNull
and
not 'null'

- you should consider doing what 'Elton W' suggested in the very first
reply


I'm still having issues with trying to use the DateTime? within my
classes. I will attempt to work on it again tomorrow and post more
code
here if I can't get it to work. Even though I changed all my
references
from DateTime to DateTime?, it looks like it still doesn't like the
NULL
coming back from the database.
 
R

Rune B

private Int16 _JobNum;
private string _Status;
private DateTime? _TimeStart;
private DateTime? _TimeEnd;
private DateTime? _JobName;

Then I do my constructor and the get/set functions...

public Int16 JobNum
{
get { return _JobNum; }
set { _JobNum = value; }
}

If my query in the DAL has 15 fields, it takes a while to create my type
class for this. I'm wondering if I can easily do this thru Visual Studio
or some other product.


You'll be amazed how quick it will be to code even a 50 field class,
properties, fields and all, if you use the snippets in Visual Studio 2005.

try this:

within the class { } brackets type: prop[tab]
- meaning the letters p-r-o-p and then press the [TAB]-key, - if the
intellisence kicks in, press tab once more.
- then you'll see the outline for a complete property with fields and all.

After that you can tab between the green variable fields in the snippet, and
when you're done, press [enter]

----

There's a lot of different snippets, but this is the one I use the most.
Snippets basically eliminates 90% of all the basic repetitive plumbing.


my favorites:
prop
propg
ctor
exception

R-)
 
G

Guest

If you spend a bit of time learning CodeSmith you can have it generate the
code for you, from a list of columns or a database table. Invaluable when you
are working with large numbers of fields.

Rune B said:
private Int16 _JobNum;
private string _Status;
private DateTime? _TimeStart;
private DateTime? _TimeEnd;
private DateTime? _JobName;

Then I do my constructor and the get/set functions...

public Int16 JobNum
{
get { return _JobNum; }
set { _JobNum = value; }
}

If my query in the DAL has 15 fields, it takes a while to create my type
class for this. I'm wondering if I can easily do this thru Visual Studio
or some other product.


You'll be amazed how quick it will be to code even a 50 field class,
properties, fields and all, if you use the snippets in Visual Studio 2005.

try this:

within the class { } brackets type: prop[tab]
- meaning the letters p-r-o-p and then press the [TAB]-key, - if the
intellisence kicks in, press tab once more.
- then you'll see the outline for a complete property with fields and all.

After that you can tab between the green variable fields in the snippet, and
when you're done, press [enter]

----

There's a lot of different snippets, but this is the one I use the most.
Snippets basically eliminates 90% of all the basic repetitive plumbing.


my favorites:
prop
propg
ctor
exception

R-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top