473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

ASP.NET 2.0 and ObjectDataBinding

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?

Feb 21 '06 #1
16 1047
You can use
if (row["StartDate"] != DBNull.Value)
{
//
}

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

Elton Wang

"dm1608" wrote:
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?

Feb 21 '06 #2
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" <El****@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.com...
You can use
if (row["StartDate"] != DBNull.Value)
{
//
}

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

Elton Wang

"dm1608" wrote:
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?

Feb 21 '06 #3
> 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 { ... }
}
}
Feb 21 '06 #4
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???

"Rune B" <ye*******@bingo.com> wrote in message
news:Oq**************@TK2MSFTNGP10.phx.gbl...
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 { ... }
}
}

Feb 21 '06 #5
You can assign to DateTime.MinValue

HTH

Elton
"dm1608" wrote:
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" <El****@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.com...
You can use
if (row["StartDate"] != DBNull.Value)
{
//
}

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

Elton Wang

"dm1608" wrote:
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?


Feb 21 '06 #6
> 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.
Feb 22 '06 #7
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.


"Rune B" <ye*******@bingo.com> wrote in message
news:uw**************@TK2MSFTNGP10.phx.gbl...
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.

Feb 22 '06 #8
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" <dm****@spam.net> wrote in message
news:u$*************@TK2MSFTNGP11.phx.gbl...
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.

Feb 22 '06 #9
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" <ye*******@bingo.com> wrote in message
news:eb**************@tk2msftngp13.phx.gbl...
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" <dm****@spam.net> wrote in message
news:u$*************@TK2MSFTNGP11.phx.gbl...
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.


Feb 22 '06 #10
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" wrote:
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" <ye*******@bingo.com> wrote in message
news:eb**************@tk2msftngp13.phx.gbl...
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" <dm****@spam.net> wrote in message
news:u$*************@TK2MSFTNGP11.phx.gbl...
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.



Feb 23 '06 #11
> 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);


Feb 23 '06 #12
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" <El****@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
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" wrote:
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" <ye*******@bingo.com> wrote in message
news:eb**************@tk2msftngp13.phx.gbl...
> 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" <dm****@spam.net> wrote in message
> news:u$*************@TK2MSFTNGP11.phx.gbl...
>> 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.
>
>


Feb 23 '06 #13
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" <ye*******@bingo.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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);

Feb 23 '06 #14
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" <dm****@spam.net> wrote in message
news:uS**************@TK2MSFTNGP09.phx.gbl...
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" <El****@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
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" wrote:
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" <ye*******@bingo.com> wrote in message
news:eb**************@tk2msftngp13.phx.gbl...
> 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" <dm****@spam.net> wrote in message
> news:u$*************@TK2MSFTNGP11.phx.gbl...
>> 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.
>
>


Feb 23 '06 #15
> 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-)
Feb 23 '06 #16
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" wrote:
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-)

Apr 25 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Brett Wickard | last post by:
Anyone know of a good way to autogenerate stored procedure parameters to put into c# code? I don't need to do it on the fly, just while coding. I've got some SPs with a lot of parameters, so it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.