Question: Best way to handle DBNULL in datareaders

R

Ravikanth[MVP]

Hi

Alternative is check before assigning as TextBox value.

TextBox1.Text =myRow["ColumnName"]==DBNull.Value)?"":
(string)myRow["ColumnName"]

HTH
Ravikanth
 
A

Aemca

That was my first solution but thats way 2 much work to type that for each
value i want to use.

So this is not really a better solution for me.

Hi

Alternative is check before assigning as TextBox value.

TextBox1.Text =myRow["ColumnName"]==DBNull.Value)?"":
(string)myRow["ColumnName"]

HTH
Ravikanth

-----Original Message-----
Im looking for the best way to handle DBNULL's in datareaders.

In a project im currently working on im using a lot of optional data with
datareaders.

Using the following syntax im getting errors when the field contains a
DBNULL value
Textbox1.text = myreader(1)

My current solution for this is using the isnull function of MSSQL and
substituting null's with "" there. This means a lot of typing for me, which
is never a good thing.

Does anyone have a better solution to my problem?


.
 
G

Guest

You could make a function which wraps the null and put that in...

Textbox1.text = CleanThis(myreader(1))

Have you tried adding the null to an empty string?

Textbox1.text = "" & myreader(1)

or something like that

It's not clear if you are using getstring or getvalue or what

....?

Aemca said:
That was my first solution but thats way 2 much work to type that for each
value i want to use.

So this is not really a better solution for me.

Hi

Alternative is check before assigning as TextBox value.

TextBox1.Text =myRow["ColumnName"]==DBNull.Value)?"":
(string)myRow["ColumnName"]

HTH
Ravikanth

-----Original Message-----
Im looking for the best way to handle DBNULL's in datareaders.

In a project im currently working on im using a lot of optional data with
datareaders.

Using the following syntax im getting errors when the field contains a
DBNULL value
Textbox1.text = myreader(1)

My current solution for this is using the isnull function of MSSQL and
substituting null's with "" there. This means a lot of typing for me, which
is never a good thing.

Does anyone have a better solution to my problem?


.
 
A

Aemca

any suggestions on how to implement this ?
aka what should be in there

and this is the way that it is implemented, the .getvalue / getstring are
omitted.


You could make a function which wraps the null and put that in...

Textbox1.text = CleanThis(myreader(1))

Have you tried adding the null to an empty string?

Textbox1.text = "" & myreader(1)

or something like that

It's not clear if you are using getstring or getvalue or what

...?

Aemca said:
That was my first solution but thats way 2 much work to type that for each
value i want to use.

So this is not really a better solution for me.

Hi

Alternative is check before assigning as TextBox value.

TextBox1.Text =myRow["ColumnName"]==DBNull.Value)?"":
(string)myRow["ColumnName"]

HTH
Ravikanth


-----Original Message-----
Im looking for the best way to handle DBNULL's in
datareaders.

In a project im currently working on im using a lot of
optional data with
datareaders.

Using the following syntax im getting errors when the
field contains a
DBNULL value
Textbox1.text = myreader(1)

My current solution for this is using the isnull
function of MSSQL and
substituting null's with "" there. This means a lot of
typing for me, which
is never a good thing.

Does anyone have a better solution to my problem?


.
 
P

PJ

You can simply use .ToString() on varchar fields.

dr(0).ToString()

That will return an empty string if it's a null varchar field.

Aemca said:
any suggestions on how to implement this ?
aka what should be in there

and this is the way that it is implemented, the .getvalue / getstring are
omitted.


You could make a function which wraps the null and put that in...

Textbox1.text = CleanThis(myreader(1))

Have you tried adding the null to an empty string?

Textbox1.text = "" & myreader(1)

or something like that

It's not clear if you are using getstring or getvalue or what

...?

Aemca said:
That was my first solution but thats way 2 much work to type that for each
value i want to use.

So this is not really a better solution for me.


Hi

Alternative is check before assigning as TextBox value.

TextBox1.Text =myRow["ColumnName"]==DBNull.Value)?"":
(string)myRow["ColumnName"]

HTH
Ravikanth


-----Original Message-----
Im looking for the best way to handle DBNULL's in
datareaders.

In a project im currently working on im using a lot of
optional data with
datareaders.

Using the following syntax im getting errors when the
field contains a
DBNULL value
Textbox1.text = myreader(1)

My current solution for this is using the isnull
function of MSSQL and
substituting null's with "" there. This means a lot of
typing for me, which
is never a good thing.

Does anyone have a better solution to my problem?


.
 
A

Aemca

PJ said:
You can simply use .ToString() on varchar fields.

dr(0).ToString()

That will return an empty string if it's a null varchar field.

Aemca said:
any suggestions on how to implement this ?
aka what should be in there

and this is the way that it is implemented, the .getvalue / getstring are
omitted.


You could make a function which wraps the null and put that in...

Textbox1.text = CleanThis(myreader(1))

Have you tried adding the null to an empty string?

Textbox1.text = "" & myreader(1)

or something like that

It's not clear if you are using getstring or getvalue or what

...?

That was my first solution but thats way 2 much work to type that
for
each
value i want to use.

So this is not really a better solution for me.


Hi

Alternative is check before assigning as TextBox value.

TextBox1.Text =myRow["ColumnName"]==DBNull.Value)?"":
(string)myRow["ColumnName"]

HTH
Ravikanth


-----Original Message-----
Im looking for the best way to handle DBNULL's in
datareaders.

In a project im currently working on im using a lot of
optional data with
datareaders.

Using the following syntax im getting errors when the
field contains a
DBNULL value
Textbox1.text = myreader(1)

My current solution for this is using the isnull
function of MSSQL and
substituting null's with "" there. This means a lot of
typing for me, which
is never a good thing.

Does anyone have a better solution to my problem?


.
 
A

Aemca

thx :)

PJ said:
You can simply use .ToString() on varchar fields.

dr(0).ToString()

That will return an empty string if it's a null varchar field.

Aemca said:
any suggestions on how to implement this ?
aka what should be in there

and this is the way that it is implemented, the .getvalue / getstring are
omitted.


You could make a function which wraps the null and put that in...

Textbox1.text = CleanThis(myreader(1))

Have you tried adding the null to an empty string?

Textbox1.text = "" & myreader(1)

or something like that

It's not clear if you are using getstring or getvalue or what

...?

That was my first solution but thats way 2 much work to type that
for
each
value i want to use.

So this is not really a better solution for me.


Hi

Alternative is check before assigning as TextBox value.

TextBox1.Text =myRow["ColumnName"]==DBNull.Value)?"":
(string)myRow["ColumnName"]

HTH
Ravikanth


-----Original Message-----
Im looking for the best way to handle DBNULL's in
datareaders.

In a project im currently working on im using a lot of
optional data with
datareaders.

Using the following syntax im getting errors when the
field contains a
DBNULL value
Textbox1.text = myreader(1)

My current solution for this is using the isnull
function of MSSQL and
substituting null's with "" there. This means a lot of
typing for me, which
is never a good thing.

Does anyone have a better solution to my problem?


.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top