Solved

Proper way to extract data from a SqlDataReader object

Posted on 2013-06-27
4
484 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: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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
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…

813 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

10 Experts available now in Live!

Get 1:1 Help Now