DataReader, hardcoding column number. why not col name?

  • Thread starter Thread starter Michelle Stone
  • Start date Start date
M

Michelle Stone

I am new to asp.net

after filling a DataReader object with some records from
a table, i want to check if the sPassword field of the
first record matches what the user has entereed. I use
the following:

if (reader.GetSqlString (4) == PasswordEdit.Text)
Response.Write ("pass");
else
Response.Write ("Sorry, wrong password");

I have hardcoded the column number (of the field
sPassword) as "4". But another day I may insert another
column at column#3, so sPassword will be come column#5.
Any way i can hardcode the name 'sPassword'?

While dealing with tables I can do .Table["Author"].
There has to be a similar way with columns, is it not?

thanks...

michelle stone
 
Michelle,

Im assuming youre not using stored procedures. Otherwise theres no issue.
To your question though, you can access each field by column name like this:
reader("columnName"), but two things will happen:

1 - you will incur a performance penalty
2 - if your datatypes dont match, you will get errors.
 
Look at the GetOrdinal method or the Column, it gives you the performance of
the Numerical indices but you retain the flexibility you desire.

Cheers

Bill
 
Back
Top