Solved

Proper way to extract data from a SqlDataReader object

Posted on 2013-06-27
4
483 Views
Last Modified: 2013-06-27
I would like to know the best way to get the data from the SqlDataReader below:


            SqlDataReader qvr= null;
            q_Command.CommandText = "select * from users where username = @username ";
            q_Command.Parameters.Clear();
            q_Command.Parameters.Add("@username", SqlDbType.VarChar).Value = SqlUserName;
            qvr = q_Command.ExecuteReader();
            qvr.Read();

At this point, how would I assign the contents of qvr to various text boxes on my form?  I have ten boxes I need to populate with the contents of qvr.

Thanks!
0
Comment
Question by:John500
  • 2
  • 2
4 Comments
 
LVL 17

Expert Comment

by:xDJR1875
ID: 39282891
while (qvr.Read())
{
      mytextbox1.Text = qvr[0].ToString();  // you could also say qvr["columnname"].ToString();
      mytextbox2.Text = qvr[1].ToString();  // index is order of the fields returned in query

}
0
 

Author Comment

by:John500
ID: 39283009
Thanks!

So in other words it would be 0 - 10 using your lines below:

mytextbox1.Text = qvr[0].ToString();  // you could also say qvr["columnname"].ToString();
mytextbox2.Text = qvr[1].ToString();  // index is order of the fields returned in query

Yes?
0
 
LVL 17

Accepted Solution

by:
xDJR1875 earned 500 total points
ID: 39283058
If there are 10 columns in the query.

Better example is "Select username, fname, lname, title, building from users"

qvr[0].ToString()     == qvr["username"].ToString();
qvr[1].ToString()     == qvr["fname"].ToString();
qvr[2].ToString()     == qvr["lname"].ToString();
qvr[3].ToString()     == qvr["title"].ToString();
qvr[4].ToString()     == qvr["building"].ToString();

Make sense?
0
 

Author Comment

by:John500
ID: 39283210
Yea, got it :)  Thanks
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get the web content with different encodings 2 28
Need help on C# Linq query on object 6 53
Error on link 14 38
Video Player 2017 5 14
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

12 Experts available now in Live!

Get 1:1 Help Now