|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
DataReader problemI 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 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. Show quote "David Young" <REMOVE_THIS.dmy75***@yahoo.com> wrote in message news:Om8BxYTNFHA.3296@TK2MSFTNGP15.phx.gbl... > 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 > > 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? Show quote "W.G. Ryan eMVP" <WilliamR***@gmail.com> wrote in message news:%23MnkH8TNFHA.3984@TK2MSFTNGP12.phx.gbl... > 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" <REMOVE_THIS.dmy75***@yahoo.com> wrote in message > news:Om8BxYTNFHA.3296@TK2MSFTNGP15.phx.gbl... > > 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 > > > > > > 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 Show quote "David Young" <REMOVE_THIS.dmy75***@yahoo.com> wrote in message news:%23$9yD$TNFHA.3356@TK2MSFTNGP12.phx.gbl... > 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" <WilliamR***@gmail.com> wrote in message > news:%23MnkH8TNFHA.3984@TK2MSFTNGP12.phx.gbl... > > 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" <REMOVE_THIS.dmy75***@yahoo.com> wrote in message > > news:Om8BxYTNFHA.3296@TK2MSFTNGP15.phx.gbl... > > > 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 > > > > > > > > > > > > Ok, never mind. It was just me being an idiot. Problem solved :)
Show quote "David Young" <REMOVE_THIS.dmy75***@yahoo.com> wrote in message news:Om8BxYTNFHA.3296@TK2MSFTNGP15.phx.gbl... > 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 > > |
|||||||||||||||||||||||