Question: Best way to handle DBNULL in datareaders

  • Thread starter Thread starter Ravikanth[MVP]
  • Start date Start date
R

Ravikanth[MVP]

Hi

Alternative is check before assigning as TextBox value.

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

HTH
Ravikanth
 
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?


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


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


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


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


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


.
 
Back
Top