|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
ASP.NET 2.0 and ObjectDataBindingpassing 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? You can use
if (row["StartDate"] != DBNull.Value) { // } or if (!row.IsNull("StartDate")) { // } HTH Elton Wang Show quoteHide quote "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? > > > > 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? Show quoteHide quote "Elton W" <Elt***@discussions.microsoft.com> wrote in message news:EA8CD012-4D2B-4B93-B0F1-D3786F7D80E2@microsoft.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? >> >> >> >> > But I need to assign NULL to the EndTime DateTime variable in order to How about using a Nullable DateTime? - and then don't assign anything if the > populate my collection for display. How do I get around this? value is DBNull. class JobSummaryItem { // ... public DateTime? EndTime { get { ... } } } 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??? Show quoteHide quote "Rune B" <yeahri***@bingo.com> wrote in message news:OqTz1QzNGHA.3908@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 { ... } > } > } > > Can you further describe this. Is DateTime? EndTime some sort of C# Not really, the Nullable class encapsulates the DateTime type.. it is really > 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??? 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. 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. Show quoteHide quote "Rune B" <yeahri***@bingo.com> wrote in message news:uwELFZ0NGHA.2176@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. > > 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 Show quoteHide quote "dm1608" <dm1608@spam.net> wrote in message news:u$QQ350NGHA.668@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. 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. Show quoteHide quote "Rune B" <yeahri***@bingo.com> wrote in message news:ebveQY4NGHA.2992@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" <dm1608@spam.net> wrote in message > news:u$QQ350NGHA.668@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. > > 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 Show quoteHide quote "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" <yeahri***@bingo.com> wrote in message > news:ebveQY4NGHA.2992@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" <dm1608@spam.net> wrote in message > > news:u$QQ350NGHA.668@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. > > > > > > > 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. Show quoteHide quote "Elton W" <Elt***@discussions.microsoft.com> wrote in message news:91FE8331-D392-4E03-8065-D71337584D12@microsoft.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" <yeahri***@bingo.com> wrote in message >> news:ebveQY4NGHA.2992@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" <dm1608@spam.net> wrote in message >> > news:u$QQ350NGHA.668@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. >> > >> > >> >> >> 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 Show quoteHide quote "dm1608" <dm1608@spam.net> wrote in message news:uSOEz$IOGHA.3788@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" <Elt***@discussions.microsoft.com> wrote in message > news:91FE8331-D392-4E03-8065-D71337584D12@microsoft.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" <yeahri***@bingo.com> wrote in message >>> news:ebveQY4NGHA.2992@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" <dm1608@spam.net> wrote in message >>> > news:u$QQ350NGHA.668@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. >>> > >>> > >>> >>> >>> > > > Using the nullable DateTime? time doesn't appear to help. Trying to pass I think what he suggested was instead of the very optimistic filling:> 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. (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); 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! Show quoteHide quote "Rune B" <yeahri***@bingo.com> wrote in message news:%23kp0nYEOGHA.2604@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); > > > >
Show quote
Hide quote
> private Int16 _JobNum; You'll be amazed how quick it will be to code even a 50 field class, > 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. 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-) 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. Show quoteHide quote "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-) > > > You can assign to DateTime.MinValue
HTH Elton Show quoteHide quote "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" <Elt***@discussions.microsoft.com> wrote in message > news:EA8CD012-4D2B-4B93-B0F1-D3786F7D80E2@microsoft.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? > >> > >> > >> > >> > > >
C#/ASP.NET 2.0 - Convert HTML Table to DataGrid/Source
How to set property "ReadOnly" on all TextBoxes on a WebForm Dynamic Textboxes. Composite Server Control and DefaultValue Attribute Type.GetType() Row count for query through SqlDataSource and GridView Datagrid viewstate TreeView web control not displaying how to create graphs in ASP? using ASPNET objects in custom webcontrol |
|||||||||||||||||||||||