Link to home
Start Free TrialLog in
Avatar of John500
John500Flag for United States of America

asked on

Proper way to extract data from a SqlDataReader object

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!
Avatar of Daniel Reynolds
Daniel Reynolds
Flag of United States of America image

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

}
Avatar of John500

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Daniel Reynolds
Daniel Reynolds
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of John500

ASKER

Yea, got it :)  Thanks