• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

How do I use a SqlDataReader to get two columns of data?

If I wanted the password associated with a userName, this would work:

cmd = new SqlCommand("Select pw from [userTable] where login='" + userName + "'", conn);

if (reader.Read())
{
    lookupPassword = (string)reader[0];
}

But what if I wanted the password and a second column of information?  Hw would I get this from the reader?  What if my query were:

cmd = new SqlCommand("Select pw, roleID from [userTable] where login='" + userName + "'", conn);


The first row of data would still be reader[0].  But how would I differentiate between the pw and roleID columns?

Thanks,
newbieweb
0
newbieweb
Asked:
newbieweb
2 Solutions
 
Lara FEACommented:
I think this is
 lookupRole = (string)reader[1];

more details on sqlreader
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx
0
 
OnthraxCommented:
You can use the title of the column to fetch the column

e.g. reader("pw") and reader("roleID")

(vb style)

Hope this helps..
0
 
newbiewebSr. Software EngineerAuthor Commented:
Thanks.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now