DataReader problem

D

David Young

I hope someone can help. I'm using a SqlDataReader to pull some data from a
Sql 2000 database. Most of the columns are varchar's of varing lengths and
one of the columns is defined as a char(2). Using the datareader I can get
the values of the varchar columns, but the char column returns empty.

Example:

myObj.prop1 = Convert.ToString(dr["VARCHAR_COL_1"]);
myObj.prop2 = Convert.ToString(dr["VARCHAR_COL_2"]);
myObj.prop3 = Convert.ToString(dr["CHAR_COL_1"]);

I know there's a value in the char column on the db, the first two
properties get set, but the third does not. I haven't check to see if it's
null or just an empty string, My property being assigned to is a string.

David
 
W

W.G. Ryan eMVP

When you say empty, are you getting an exception or is it just String.Empty?
The reason I ask is b/c if you were converting a DbNull.value, it should
throw an exception.
Is this the exact code?

As an aside, I'd use the Getxxx version for (ie GetSqlString) and I'd use
either the GetOrdinal method, use an Bill vaughn's method an use an enum or
use an index instead. At each pass you are having to resolve the column
index here if you are using column names.
 
D

David Young

Thanks Billl,
I am checking for DBNull before assignment. I just didn't show that.
Anyway. It's solved now. No great mystery, two fields later, I was
overwriting the objects property with an empty string. Once I found and
deleted that, it works fine.

On a side note, so you advocate using dr.GetString() and pass an ordinal as
opposed to my method? Is the extra time needed to convert column name to
ordinal index that much of a trade off?


W.G. Ryan eMVP said:
When you say empty, are you getting an exception or is it just String.Empty?
The reason I ask is b/c if you were converting a DbNull.value, it should
throw an exception.
Is this the exact code?

As an aside, I'd use the Getxxx version for (ie GetSqlString) and I'd use
either the GetOrdinal method, use an Bill vaughn's method an use an enum or
use an index instead. At each pass you are having to resolve the column
index here if you are using column names.

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
David Young said:
I hope someone can help. I'm using a SqlDataReader to pull some data
from
a
Sql 2000 database. Most of the columns are varchar's of varing lengths and
one of the columns is defined as a char(2). Using the datareader I can get
the values of the varchar columns, but the char column returns empty.

Example:

myObj.prop1 = Convert.ToString(dr["VARCHAR_COL_1"]);
myObj.prop2 = Convert.ToString(dr["VARCHAR_COL_2"]);
myObj.prop3 = Convert.ToString(dr["CHAR_COL_1"]);

I know there's a value in the char column on the db, the first two
properties get set, but the third does not. I haven't check to see if it's
null or just an empty string, My property being assigned to is a string.

David
 
D

David Young

You know what, never mind that question. After thinking about it, it's not
only about the time is it? I can see your point. Let's say at some point,
one of the column names changes. Not the data, just the name of the column.
By using the ordinals, nothing in my code has to change. If I used the
column names, then I have to go back and touch my code. Which makes my
approach problematic.

Thanks,
Dave

David Young said:
Thanks Billl,
I am checking for DBNull before assignment. I just didn't show that.
Anyway. It's solved now. No great mystery, two fields later, I was
overwriting the objects property with an empty string. Once I found and
deleted that, it works fine.

On a side note, so you advocate using dr.GetString() and pass an ordinal as
opposed to my method? Is the extra time needed to convert column name to
ordinal index that much of a trade off?


W.G. Ryan eMVP said:
When you say empty, are you getting an exception or is it just String.Empty?
The reason I ask is b/c if you were converting a DbNull.value, it should
throw an exception.
Is this the exact code?

As an aside, I'd use the Getxxx version for (ie GetSqlString) and I'd use
either the GetOrdinal method, use an Bill vaughn's method an use an enum or
use an index instead. At each pass you are having to resolve the column
index here if you are using column names.

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
David Young said:
I hope someone can help. I'm using a SqlDataReader to pull some data
from
a
Sql 2000 database. Most of the columns are varchar's of varing
lengths
and
one of the columns is defined as a char(2). Using the datareader I
can
get
the values of the varchar columns, but the char column returns empty.

Example:

myObj.prop1 = Convert.ToString(dr["VARCHAR_COL_1"]);
myObj.prop2 = Convert.ToString(dr["VARCHAR_COL_2"]);
myObj.prop3 = Convert.ToString(dr["CHAR_COL_1"]);

I know there's a value in the char column on the db, the first two
properties get set, but the third does not. I haven't check to see if it's
null or just an empty string, My property being assigned to is a string.

David
 

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