Solved

how to read fields from a data reader

Posted on 2011-09-25
4
386 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
[X]
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
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

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 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