
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
>.