Solved

Proper way to extract data from a SqlDataReader object

Posted on 2013-06-27
4
479 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Yea, got it :)  Thanks
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

14 Experts available now in Live!

Get 1:1 Help Now