Solved

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

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now