Solved

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

Posted on 2010-09-22
3
255 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
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET MVC - Views 3 38
How to filter by key press ? 6 54
TFS 2015 Access denied. 1 29
Starting to use Git with Visual  Studio Online 1 18
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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

685 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