Solved

how to read fields from a data reader

Posted on 2011-09-25
4
385 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
ID: 36595672
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
ID: 36595681
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
ID: 36595737
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
ID: 36596654
Thanks.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

685 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