Solved

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

Posted on 2010-09-22
3
258 Views
Last Modified: 2012-05-10
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
Comment
Question by:newbieweb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 11

Accepted Solution

by:
Larissa T earned 400 total points
ID: 33737673
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
 
LVL 13

Assisted Solution

by:Onthrax
Onthrax earned 100 total points
ID: 33738875
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
 

Author Closing Comment

by:newbieweb
ID: 33738885
Thanks.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question