Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


how to read fields from a data reader

Posted on 2011-09-25
Medium Priority
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 2000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

688 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