Solved

how to read fields from a data reader

Posted on 2011-09-25
4
374 Views
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())
                {
                    conn.Open();
                    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

0
Comment
Question by:dpicco
  • 2
4 Comments
 
LVL 18

Expert Comment

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

Gary Davis
0
 
LVL 18

Expert Comment

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

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
Comment Utility
dr = command.ExecuteReader(CommandBehavior.SingleResult);
   if (dr.HasRows)
   {
   dr.Read(); // Retrieves the line
 
   Session["FullName"]=dr.GetString(0);
// or
   Session["FullName"]=dr["FullName"].ToString();
   dr.Close();
}

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.
0
 
LVL 3

Author Closing Comment

by:dpicco
Comment Utility
Thanks.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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!
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

11 Experts available now in Live!

Get 1:1 Help Now