3

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?)

10
  • Try running the query "select CustomerName from Customer" directly in Management Studio. I bet you'll find some kind of spelling error (Sincerely not insulting your intelligence, that kind of stuff happens). Commented Sep 3, 2011 at 6:31
  • you're right, i was not thinking right. But now it wont give me the dynamic data.. it returns "CustomerName" as the value for the CustomerName variable, but i want the data from the database, why is it not returning the dynamic data? Commented Sep 3, 2011 at 6:37
  • You're explicitly setting CustomerName to "CustomerName". You need to access the actual value of the CustomerName column from dr (something like string CustomerName = dr["CustomerName"];) Commented Sep 3, 2011 at 6:45
  • @jadarnel27: Okay i did that, and it says.... ERROR CS0266: Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?) Commented Sep 3, 2011 at 6:48
  • string CustomerName = dr["CustomerName"].ToString(); I'm on the East Coast (US) and it's 3 AM. I'm going to sleep =) good luck! Commented Sep 3, 2011 at 6:50

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

what should i use instead, if all i need is one result?
I'm not sure what DB is, but you could use ADO.NET and the SqlCommand.ExecuteScalar method. Let me know if you need an example.
i found an example,thank you.. but its still giving me the same error.. i will post the new code
0

It means that either CustomerName or CustomerID is not a valid column within your database. Check your table again.

2 Comments

It is a valid column name, thats the thing.
@Wilkins Is there any blank in these name?
0
  1. Make sure you are trying to connect correct database.
  2. See CustomerName column should be in Customer table. check spelling also

1 Comment

Add your schema name like dbo.customer.
0

First, debug and check the value of:

DB.GetDBConn()

You will verify that you are going to the same in Studio as you are in the program.

I think it is the spelling somewhere between the db and your code.

Once you get past the error, you need to fix this:

{
       string CustomerName = "CustomerName";                    
   }

You are not accessing the reader, try some kind of tutorial for that stuff.

1 Comment

I don't really use readers, but I'm guessing dr["CustomerName"] will give you a result - what do you want to do with it?
0

Try doing a select * from customer where ... and put a breakpoint on your using datareader statement. Then use quick-watch on the datareader object to investigate the columns exposed in the recordset.

Or you could run the select statement on your db of choice to ensure that the column name is the same.

I agree with Madhur above, your column name is not spelled correctly. Or you are not connecting to the correct db.

Hope this helps

2 Comments

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
Please refer to jadarnel27's comment above. You need to change your string assignment for customer name. Please use string customername = dr.GetString(dr.GetOrdinal("CustomerName")); or something like that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.