Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
int divide = 100;
       string strSQLconnection = ("Data Source");
       SqlConnection sqlConnection = new SqlConnection(strSQLconnection);
       sqlConnection.Open();
       SqlCommand sqlCommand = new SqlCommand("SELECT Discount * UnitPrice /" + divide + "FROM Items.Product where ProductName LIKE" + "'" + item3 + "'", sqlConnection);
       decimal answer = Convert.ToDecimal(sqlConnection.ExecuteScalar());


ExecuteScalar is seriously giving me a headache it gives me the following error Error 5 'System.Data.SqlClient.SqlConnection' does not contain a definition for 'ExecuteScalar' and no extension method 'ExecuteScalar' accepting a first argument of type 'System.Data.SqlClient.SqlConnection' could be found (are you missing a using directive or an assembly reference?

i tried to solve this but im out. HELP
Posted

1 solution

You are trying to execute the ExecuteScalar on your connection instead of your command. You need to change that line to be:

C#
sqlCommand.ExecuteScalar()


To add to this, I would recommend that you change your naming conventions to be a bit clearer. For example, you have strSQLconnection representing the connection string while sqlConnection represents the SQL Connection object. That is confusing. It also doesn't follow good guidelines for naming conventions, which will cause you problems down the road. I would suggest something like connectionString for your connection string and sqlConnection for your SQL connection. That makes it a bit easier to understand what the job is for each.
 
Share this answer
 
v2
Comments
morojele 3-Jun-12 18:53pm    
THANKS. YOU ARE A LIFE SAVER. I TOTALLY MISSED THAT, THANK YOU
Tim Corey 3-Jun-12 18:54pm    
Not a problem. Having someone else read through your code sometimes exposes those problems that you otherwise miss. That is one of the benefits of pair programming.
Tim Corey 3-Jun-12 20:05pm    
Just a quick question - is there a reason you voted 3/5 for the answer (if that was you)?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900