how to read fields from a data reader

Posted on 2011-09-25
Last Modified: 2012-05-12
Hello experts,
Can anyone tell me what I'm doing wrong with this data reader? I'm getting a result back when I query the database directly. Browser error is Invalid attempt to read when no data is present.

protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack) 
            Session["uid"] = Request.QueryString["uid"];

            SqlCommand command;
            SqlConnection conn;
            SqlDataReader dr = null;

            string sqlString = @" SELECT first_name + ' ' + last_name full_name FROM people WHERE network_account = @network_account";

            using (conn = new SqlConnection(ConfigurationManager.ConnectionStrings["AdHocReportingConnectionString"].ConnectionString.ToString()))
                using (command = conn.CreateCommand())
                    command.CommandText = sqlString;
                    command.CommandType = CommandType.Text;
                    command.Parameters.AddWithValue("@network_account", Session["uid"].ToString());

                    using (dr = command.ExecuteReader(CommandBehavior.SingleResult))
                        if (dr.HasRows)
                            Session["FullName"] = dr["full_name"].ToString();

        this.Label1.Text = Session["FullName"].ToString();

Open in new window

Question by:dpicco
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
LVL 18

Expert Comment

by:Gary Davis
ID: 36595672
After the ExecuteReader, dr has the value, not the row since you are using SingleResult.

Gary Davis
LVL 18

Expert Comment

by:Gary Davis
ID: 36595681
Also, don't use the "using" in this case and also you will not be getting back a datareader.
LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 500 total points
ID: 36595737
dr = command.ExecuteReader(CommandBehavior.SingleResult);
   if (dr.HasRows)
   dr.Read(); // Retrieves the line
// or

Open in new window

Using is useless, since you are not the one who instantiate the DataReader, it is done instide of the SqlCommand object.

Note that there are 2 ways of retrieving a value. The one you were using, and a series that work with individual types (GetString, GetInt32, GetDateTime...).

For such a simple operation, it does not make much difference, but if you were looping through a DataReader that returns many rows, calling Get... is faster because you are dealing with typed data instead of Object data that needs to be converted to the proper type in order to be used.

Author Closing Comment

ID: 36596654

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

636 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