Solved

Proper way to extract data from a SqlDataReader object

Posted on 2013-06-27
4
489 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
[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
  • 2
  • 2
4 Comments
 
LVL 17

Expert Comment

by:Daniel Reynolds
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:
Daniel Reynolds 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

734 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