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

  • SEARCH
  • Home
  • 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 6535893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:25:37+00:00 2026-05-25T10:25:37+00:00

Trying to do a recordset, I just want one column of data, but this

  • 0

Trying to do a recordset, I just want one column of data, but this code is giving me an error.. I’m an ASP.NET newb, can anyone help?:

System.Data.SqlClient.SqlException: Invalid column name
‘CustomerName’.

 using (SqlConnection con = new SqlConnection(DB.GetDBConn()))
 {
  con.Open();
  using (IDataReader dr = DB.GetRS("select CustomerName from Customer where CustomerID=" + Customer.CustomerID, con))
   {
       string CustomerName = "CustomerName";                    
   }
 }

 String EncCustomerName = Encrypt(CustomerName.Replace(".", "").Replace("-", ""),"1");

Question #2: How do I bind the database content to the CustomerName string? It seems like its only returning “CustomerName” as the value for CustomerName string.. I would like it to return the database data for CustomerName string.. Help?

Suggested to use a ExecuteScalar, so i modified the request to this

   using (var con = new SqlConnection(DB.GetDBConn()))
   using (var cmdContrib = new SqlCommand("SELECT CustomerName FROM Customer WHERE   CustomerID=" + ThisCustomer.CustomerID, con))

   {

        con.Open();
        string CustomerName = cmdContrib.ExecuteScalar();
   } 

And i Get this error:
“string CustomerName = cmdCust.ExecuteScalar();”

CS0266: Cannot implicitly convert type ‘object’ to ‘string’. An explicit conversion exists (are you missing a cast?)

  • 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-05-25T10:25:37+00:00Added an answer on May 25, 2026 at 10:25 am

    To answer your second question:

    // Set it here so you can access it outside the scope of the using statement
    string CustomerName = "";
    
    using (SqlConnection con = new SqlConnection(DB.GetDBConn()))
    {
        con.Open();
        using (IDataReader dr = DB.GetRS("select CustomerName from Customer where CustomerID=" + Customer.CustomerID, con))
        {
            while (dr.Read())
                CustomerName = dr["CustomerName"].ToString();
            }                    
        }
     }
    

    If you’re sure you’ll only get one CustomerName result, using a DataReader is a bit of an overkill.

    SqlCommand.ExecuteScalar Example

    string CustomerName = "";
    
    using (SqlConnection con = new SqlConnection(DB.GetDBConn()))
    {
    
        SqlCommand cmd = new SqlCommand("SELECT CustomerName FROM Customer WHERE CustomerID = " + Customer.CustomerID, con);
        cmd.CommandType = CommandType.Text;
    
        con.Open();
    
        CustomerName = Convert.ToString(cmd.ExecuteScalar());
    
    }
    

    SqlCommand.ExecuteScalar Method

    Additional Info

    ExecuteScalar returns an object, so you’ll need to convert the returned value to the proper type (in this case, string).

    Also, you should declare your CustomerName value outside of the using blocks (as I did in my example) – otherwise it will be scoped to the using blocks and not available outside of them.

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

Sidebar

Related Questions

What I'm trying to achieve must be simple but I just can't get it
Just want to see if I understand this correctly. CNAME record specifies an alias,
I almost have the data I want...but need help filtering it. (pic at bottom)
I'm trying to process the following in one sql statement - I want to
I'm trying to make my Listview show only one record just like how DetailsView
I'm trying to join multiple tables, but one of the tables has multiple records
Sorry I'm a bit new to this so just trying to get my head
Hey guys, I am just trying to pull all the records from my database
I am trying to load a recordset into an array. I am using the
I'm trying to get an SPF record set up on our domain, but it

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.