Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9252485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:57:23+00:00 2026-06-18T10:57:23+00:00

I am trying to return a data object from my database so that i

  • 0

I am trying to return a data object from my database so that i can access (for example) a customer ID within my ASP.NET website. Upon a customer logging in the object is returned. However, i am getting the error:

   'Invalid attempt to read when no data is present.' 

I have completed an sql query on the database (Executing my stored procedure) which returns the correct information, so i know it is there. I can only presume that there is something wrong with the following method:

    using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {
            using (SqlCommand sqlComm = new SqlCommand("Select_Customer_By_UserName_And_Password", sqlConn))
            {
                sqlComm.Connection.Open();
                try
                {
                    sqlComm.CommandType = CommandType.StoredProcedure;
                    sqlComm.Parameters.Add("@Username", SqlDbType.NVarChar, 25).Value = pUsername;
                    sqlComm.Parameters.Add("@Password", SqlDbType.NVarChar, 25).Value = pPassword;

                    using (SqlDataReader sqlDR = sqlComm.ExecuteReader(CommandBehavior.SingleRow))
                    {
                        if (sqlDR.HasRows)
                        {
                            //Creating the new object to be returned by using the data from the database.
                            return new Customer
                            {
                                CustomerID = Convert.ToInt32(sqlDR["CustomerID"])
                            };
                        }
                        else
                            return null;
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    sqlComm.Connection.Close();
                }
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:57:24+00:00Added an answer on June 18, 2026 at 10:57 am

    You need to call sqlDR.Read(), otherwise the “record pointer” will to point to a record. HasRows only indicates there are actually rows you can read. To read each row (or just the first one), you need to call Read once or in a while loop.

    For example:

    if (reader.HasRows)
    {
        while (reader.Read())
            ...
    }
    

    Your code should read:

    using (SqlDataReader sqlDR = sqlComm.ExecuteReader(CommandBehavior.SingleRow))
    {
        if (sqlDR.Read())
        {
            //Creating the new object to be returned by using the data from the database.
            return new Customer
            {
                CustomerID = Convert.ToInt32(sqlDR["CustomerID"])
            };
        }
        else
            return null;
    }
    

    By the way: congrats on using using and parameterized queries!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read data from a service (returns JSON object) and create an
I'm trying to create a procedure that will enter data and then return a
Im trying to return the value that a $ajax call returns, from a function
I am trying to create a pageing and sorting object data source that before
I'm trying to reduce the code duplication that exists throughout my asp.net web forms.
I'm trying to make a query that must to retrieve some data from my
I'm trying to return data as an array to my .ajax function on success
I am trying to return a new copy of the data in a C++
I'm trying to get my function to return the data it got into another
I'm trying to filter the data fusion tables return. This is my code: //

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.