|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
non-labor intensive solution to Nulls in legacy DBor should not be allowed in Databases. I have a legacy database and I can't change it. NULLS are a fact of life for me. My question is whether anyone has a less labor intensive solution. In the auto-generated class definition code for the datarow of eacch table is code like the following... Public Property Birthday As Date Get Try Return CType(Me(Me.tableo_Person.BirthdayColumn),Date) Catch e As InvalidCastException Throw New StrongTypingException("Cannot get value because it is DBNull.", e) End Try End Get Set Me(Me.tableo_Person.BirthdayColumn) = value End Set End Property I have been modifying this code to... Public Property Birthday As Date Get Try Return CType(Me(Me.tableo_Person.BirthdayColumn),Date) Catch e As InvalidCastException return nothing End Try End Get Set if value = nothing then Me(Me.tableo_Person.BirthdayColumn) = system.convert.dbnull else Me(Me.tableo_Person.BirthdayColumn) = value endif End Set End Property This code gets generated anytime I have to redrag a datatable into a dataset. Then I have to go thru each field and modify as above again. Is there a smarter way to do this? (I'm normally a foxpro programmer, but this one is going against a SQL server database, if that has any bearing on this question) -- T Ralya, new .NET developer, HMSA Since you're using VB.nET you can easily use IIF with IsDBNull
http://www.knowdotnet.com/articles/handlingnullvalues.html Show quoteHide quote "T Ralya" <TRa***@discussions.microsoft.com> wrote in message system.convert.dbnullnews:A1A63330-E4F1-4827-AF79-FE44E8A1238C@microsoft.com... > I don't want to enter the religious wars about NULLS and whether they should > or should not be allowed in Databases. I have a legacy database and I can't > change it. NULLS are a fact of life for me. > > My question is whether anyone has a less labor intensive solution. > > In the auto-generated class definition code for the datarow of eacch table > is code like the following... > > Public Property Birthday As Date > Get > Try > Return CType(Me(Me.tableo_Person.BirthdayColumn),Date) > Catch e As InvalidCastException > Throw New StrongTypingException("Cannot get value > because it is DBNull.", e) > End Try > End Get > Set > Me(Me.tableo_Person.BirthdayColumn) = value > End Set > End Property > > I have been modifying this code to... > Public Property Birthday As Date > Get > Try > Return CType(Me(Me.tableo_Person.BirthdayColumn),Date) > Catch e As InvalidCastException > return nothing > End Try > End Get > Set > if value = nothing then > Me(Me.tableo_Person.BirthdayColumn) = Show quoteHide quote > else > Me(Me.tableo_Person.BirthdayColumn) = value > endif > End Set > End Property > > This code gets generated anytime I have to redrag a datatable into a > dataset. Then I have to go thru each field and modify as above again. > > Is there a smarter way to do this? (I'm normally a foxpro programmer, but > this one is going against a SQL server database, if that has any bearing on > this question) > -- > T Ralya, new .NET developer, HMSA
Show quote
Hide quote
"T Ralya" <TRa***@discussions.microsoft.com> wrote in message When I generate a strongly-typed DataSet from a database which has columns news:A1A63330-E4F1-4827-AF79-FE44E8A1238C@microsoft.com... >I don't want to enter the religious wars about NULLS and whether they >should > or should not be allowed in Databases. I have a legacy database and I > can't > change it. NULLS are a fact of life for me. > > My question is whether anyone has a less labor intensive solution. > > In the auto-generated class definition code for the datarow of eacch table > is code like the following... > > Public Property Birthday As Date > Get > Try > Return CType(Me(Me.tableo_Person.BirthdayColumn),Date) > Catch e As InvalidCastException > Throw New StrongTypingException("Cannot get value > because it is DBNull.", e) > End Try > End Get > Set > Me(Me.tableo_Person.BirthdayColumn) = value > End Set > End Property which allow NULL, it also generates an Is<columnName>Null method. Do you have such a method? If not, I wonder why it didn't think the Birthday column allowed NULL? John Saunders I hadn't noticed that procedure before, but yes it did generate it. I can
use that to handle the situation ourside of the dataset.vb code. Thanks bunches. Show quoteHide quote "John Saunders" wrote: > "T Ralya" <TRa***@discussions.microsoft.com> wrote in message > news:A1A63330-E4F1-4827-AF79-FE44E8A1238C@microsoft.com... > >I don't want to enter the religious wars about NULLS and whether they > >should > > or should not be allowed in Databases. I have a legacy database and I > > can't > > change it. NULLS are a fact of life for me. > > > > My question is whether anyone has a less labor intensive solution. > > > > In the auto-generated class definition code for the datarow of eacch table > > is code like the following... > > > > Public Property Birthday As Date > > Get > > Try > > Return CType(Me(Me.tableo_Person.BirthdayColumn),Date) > > Catch e As InvalidCastException > > Throw New StrongTypingException("Cannot get value > > because it is DBNull.", e) > > End Try > > End Get > > Set > > Me(Me.tableo_Person.BirthdayColumn) = value > > End Set > > End Property > > When I generate a strongly-typed DataSet from a database which has columns > which allow NULL, it also generates an Is<columnName>Null method. Do you > have such a method? If not, I wonder why it didn't think the Birthday column > allowed NULL? > > John Saunders > > >
Other interesting topics
ObjectSpaces, object persistence
Table relations across datasets Is Whidbey the beginning of the end for the developer ? I want help with the jet 4.0 provider.. Cann't get my only record with OracleDatareader object Using ADO.NET DataSet in Unmanaged C++ COM Code Checking to see if SQL Service is running Several Questions on ADO.NET where can I download the ADO.Net object? Left join, cross-joined view, running forever |
|||||||||||||||||||||||