best way to move a possibly null value into object from Db

S

Smokey Grindle

I am using .NET 2.0 with this and have loaded a row into a data table... now
I want to move the info in that row into the fields of an object... what is
the best way to do this for fields that I know could be null... right now
say I have it set up like this

Object has fields
Private m_IsNotANull as string
private m_CouldBeNull as nullable(of datetime)

' now load row into this
m_IsNotANull = dt.row(0)("MyString").tostring
' ok this works easily, now how about null ones?
m_CouldBeNull = ???

I know I can check if the current value is null and do stuff like this

if dt.row(0)("CouldBeNullDate") isnot DBNull.value
m_CouldBeNull = diretcast(dt.row(0)("CouldBeNull"),datetime)
else
m_CouldBeNull = nothing
end if

but is that the easiest way to do it? what alternatives are out there?
thanks!
 
M

Mr. Arnold

Smokey Grindle said:
I am using .NET 2.0 with this and have loaded a row into a data table...
now I want to move the info in that row into the fields of an object...
what is the best way to do this for fields that I know could be null...
right now say I have it set up like this

Object has fields
Private m_IsNotANull as string
private m_CouldBeNull as nullable(of datetime)

' now load row into this
m_IsNotANull = dt.row(0)("MyString").tostring
' ok this works easily, now how about null ones?
m_CouldBeNull = ???

I know I can check if the current value is null and do stuff like this

if dt.row(0)("CouldBeNullDate") isnot DBNull.value
m_CouldBeNull = diretcast(dt.row(0)("CouldBeNull"),datetime)
else
m_CouldBeNull = nothing
end if

but is that the easiest way to do it? what alternatives are out there?
thanks!

You might be able to do this.
If Not IsDBNull(Object) Then

do not null

else

do is null

endif
 
M

Michel Posseth [MCP]

An alternative would be to use the strongtype features of the datarow , and
so use the methods that are then automaticly provided to check for null
values or set null values

regards

Michel
 
C

Cor Ligthert [MVP]

Hi Michel,

From your answer I think that I understand the question (and answer). AFAIK
you can forever write any value to an object. I think that we miss it as
well, because here everything seems to be global declared, something I
seldom do certainly not in a sample because we don't know the types.

dim myObject as object = whatever typecasted thing

I think that we are looking more about what is asked than this simple
answer.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top