Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to read fields from a data reader

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

916 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