VB.Net, Classes and ADO.Net guidelines??? 
Author Message
 VB.Net, Classes and ADO.Net guidelines???

Hi,

I'm probably not the first to ask this question, but I've tried to
search Google for answers, but I can't figure out what to search for.

I have a class:

   Public Class User
       Public FirstName As String
       Public LastName As String
   End Class

LastName doesn't always have a value in the database.

If I fill User.LastName from a datareader and the value is DbNull, then
i get an error. Here's what I do:

   User.LastName = dr("LastName")

Then I thought about doing this:

   Public Class User
       Public FirstName
       Public LastName
   End Class

This way it can hold any value (Object) or DbNull, but as far as I can
understand, using object is both slower and takes up more memory.

What is the best approach (guideline) to do this?

Thanks!

M O J O



Thu, 24 Mar 2005 16:30:15 GMT  
 VB.Net, Classes and ADO.Net guidelines???
Hi,
There are a couple of things that might help here.  

You could fix the problem on the database side:

1)Fix your table to not allow nulls(where it makes sense
like in a LastName column); and possibly add a default
value of '' so if LastName is not supplied it defaults to
an empty string instead of Null.

2)Fix it in the Select query: So in Sql Server you might
issue this statement:

Select
  IsNull(FirstName,'') As FirstName,
  IsNull(LastName,'') As LastName
From
  Users

Or you could fix it in the VB code side like this:
myUser.LastName = IIf(TypeOf dr("LastName") Is DBNull, "",
dr("LastName"))

Personally, I prefer fixing the table to not allow Nulls
in fields where it makes sense to do so, like someone's
name, title, phone number, ssn, etc.  It makes coding
against the table easier, you don't have to modify your
SQL or your VB code to handle for Nulls because there
aren't any.  

-chris

Quote:
>-----Original Message-----
>Hi,

>I'm probably not the first to ask this question, but I've
tried to
>search Google for answers, but I can't figure out what to
search for.

>I have a class:

>   Public Class User
>       Public FirstName As String
>       Public LastName As String
>   End Class

>LastName doesn't always have a value in the database.

>If I fill User.LastName from a datareader and the value
is DbNull, then
>i get an error. Here's what I do:

>   User.LastName = dr("LastName")

>Then I thought about doing this:

>   Public Class User
>       Public FirstName
>       Public LastName
>   End Class

>This way it can hold any value (Object) or DbNull, but as
far as I can
>understand, using object is both slower and takes up more
memory.

>What is the best approach (guideline) to do this?

>Thanks!

>M O J O

>.



Fri, 25 Mar 2005 01:37:19 GMT  
 VB.Net, Classes and ADO.Net guidelines???

Quote:

> 1)Fix your table to not allow nulls
> 2)Fix it in the Select query
> Or you could fix it in the VB code side like this:
> myUser.LastName = IIf(TypeOf dr("LastName") Is DBNull, "",
> dr("LastName"))

One more way:

myUser.LastName = "" & dr("LastName")

The concatenation is cheap in performance and coerces any null
values to be empty strings.

FWIW, I like to hide my datareader code in the object itself when
possible. This makes changes to my object (adding, renaming, and
removing properties more manageable and allows me to set private
properties as well as public ones. I can reuse the code in any
number of places where the object is instantiated and read in
from a data source. Example:

Public Class User
     Public FirstName As String
     Public LastName As String
     Public Function LoadFromDataReader(ByRef dr As SqlDataReader) As
Boolean
        Try
                FirstName = "" & dr.Item("FirstName")
                LastName  = "" & dr.Item("LastName")
                Return True
        Catch
                Return False
        End Try
     End Function
End Class

I've been known to write similar "Load" functions for deserializing
the object from text files, XML documents, or the HTTP Request
object (from forms not using ASP.NET's server-side fields).

You can implement the above function as a constructor with an
SqlDataReader as a parameter, but the error-catching isn't as
obvious as the stand-alone function (you'd have to pass another
ByRef parameter for the status or set a public property (e.g.,
"IsLoaded" or "IsValid") as a side-effect of the constructor and
then test that result in the calling code.

--
Richard Tallent



Fri, 25 Mar 2005 02:30:41 GMT  
 VB.Net, Classes and ADO.Net guidelines???
Thank you guys!

I couldn't figure it out and now I see the possiblilities.

Thanks again.

M O J O

Quote:


>> 1)Fix your table to not allow nulls
>> 2)Fix it in the Select query
>> Or you could fix it in the VB code side like this:
>> myUser.LastName = IIf(TypeOf dr("LastName") Is DBNull, "",
>> dr("LastName"))

> One more way:

> myUser.LastName = "" & dr("LastName")

> The concatenation is cheap in performance and coerces any null
> values to be empty strings.

> FWIW, I like to hide my datareader code in the object itself when
> possible. This makes changes to my object (adding, renaming, and
> removing properties more manageable and allows me to set private
> properties as well as public ones. I can reuse the code in any
> number of places where the object is instantiated and read in
> from a data source. Example:

> Public Class User
>     Public FirstName As String
>     Public LastName As String
>     Public Function LoadFromDataReader(ByRef dr As SqlDataReader) As
> Boolean
>     Try
>         FirstName = "" & dr.Item("FirstName")
>         LastName  = "" & dr.Item("LastName")
>         Return True
>     Catch
>         Return False
>     End Try
>     End Function
> End Class

> I've been known to write similar "Load" functions for deserializing
> the object from text files, XML documents, or the HTTP Request
> object (from forms not using ASP.NET's server-side fields).

> You can implement the above function as a constructor with an
> SqlDataReader as a parameter, but the error-catching isn't as
> obvious as the stand-alone function (you'd have to pass another
> ByRef parameter for the status or set a public property (e.g.,
> "IsLoaded" or "IsValid") as a side-effect of the constructor and
> then test that result in the calling code.

> --
> Richard Tallent



Fri, 25 Mar 2005 03:30:11 GMT  
 VB.Net, Classes and ADO.Net guidelines???
What did the error message read??

Anyway, some suggestion:

On Error Resume Next
User.LastName = dr("LastName")
If Err.Number Then Uesr.LastName = Nothing

If it returns DBNull then you get an empty string in User.LastName



Quote:
> Hi,

> I'm probably not the first to ask this question, but I've tried to
> search Google for answers, but I can't figure out what to search for.

> I have a class:

>    Public Class User
>        Public FirstName As String
>        Public LastName As String
>    End Class

> LastName doesn't always have a value in the database.

> If I fill User.LastName from a datareader and the value is DbNull, then
> i get an error. Here's what I do:

>    User.LastName = dr("LastName")

> Then I thought about doing this:

>    Public Class User
>        Public FirstName
>        Public LastName
>    End Class

> This way it can hold any value (Object) or DbNull, but as far as I can
> understand, using object is both slower and takes up more memory.

> What is the best approach (guideline) to do this?

> Thanks!

> M O J O



Sun, 27 Mar 2005 21:16:15 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. ADO.NET with ASP.NET using VB.NET

2. Accessing vb.net class from non.vb.net apps

3. Debug VB.NET DLL(Class Library) from ASP.NET page

4. Use a VB.NET Class Library in C++.NET

5. Using Namespace Aliases in VB.NET and coding guidelines

6. VB and .Net Naming Guidelines

7. Coding Guidelines for VB.NET

8. ADO.Net to fetch ActiveDirectory this function bugs in Asp.net but works in Vb.n

9. what create table using ADO.NET to VB.NET

10. VB.Net => Ado.Net = Stored Procedure

11. Memory leak! (ADO.NET, VB.NET)

12. vb.net / ado.net design question

 

 
Powered by phpBB® Forum Software