Click here to Skip to main content
15,068,988 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to count number of an operation in table1 from database and seted column1 and column2 like research keys .
when I click research button it shows me this error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) FROM DATABASE.TABLE1 WHERE COLUMN1  = 'KEY1' AN' at line 1

at this line "

int countOERATION = Convert.ToInt32(cmdOPERATION.ExecuteScalar());

I already used this code before with different table and it worked correctly !

has any one got this before !?
Or close , Thank you for any help .

What I have tried:

using (var cmdOPERATION = new MySqlCommand("SELECT  COUNT (*) FROM DATABASE.TABLE1 WHERE COLUMN1  = '" + KEY1 + "' AND COLUMN2 =  '" + KEY2 + "' ;", CONNECTION))

               int countOPERATION = Convert.ToInt32(cmdOPERATION.ExecuteScalar());                                             }
Updated 6-Feb-17 20:44pm
OriginalGriff 6-Feb-17 5:50am
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalization if you want to be taken seriously.
EM_Y 6-Feb-17 5:54am
Thank you i will fixe that :)

There are a number of problems here:
1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
2) The error message does not match your code as shown: it refers to TABLE1, while your code refers to TALE1

So check your code: at a guess you have missed out the opening "(" from your command in the code. But fix the concatenation, not only in that code but the whole of your application - or your best mate will be able to delete your whole DB just to see the look on your face...
Do not build query strings like that. You're setting yourself up for SQL injection. Use parameterized queries instead.
EM_Y 6-Feb-17 6:04am
thank you I will try it !
but why it works for me in other tables !?
I tried with entity query and its working for me .

baseEntities dc = new baseEntities();

var getcount = dc.table.Where(b => b.column1 == key1 && b.column2 == key2).Count(); 
                            int count = Convert.ToInt32(getcount);

thank you all for your suggestions :)

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