Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi here's my scenario right now:

Recently i change my codes from oleB to my sql

Here's my previous code

string sql = "SELECT COUNT(*) FROM account WHERE [Access_Control] = @code";
OleDbCommand com = new OleDbCommand(sql, con);
com.Connection = con;
com.CommandText = sql;
com.Parameters.Clear();
com.Parameters.AddWithValue("@code", metroaccesscontroltxtBox.Text);
int numRecords = (int)com.ExecuteScalar();

if (numRecords == 1 )
{
groupBox1.Enabled = true;
metroaccesscontroltxtBox.ReadOnly = true;
}


All my codes work, After changing all my codes like
OleDbCmd to MySqlCmd , oledbdataadapter to mysqldataadapter

i got error
error in your Sql Syntax, check the manual that corresponds to your MySql Server version for the right syntax to use near "" at line blabla
Posted
Updated 18-Nov-15 20:31pm
v3

1 solution

You need to remove the brackets in the SQL statement
C#
string sql = "SELECT COUNT(*) FROM account WHERE Access_Control = @code"
;

If you need use a keyword in MySQL you wrap it in two ` `
like
`Table`

(Don't know what this character is called, though)

[UPDATE]
Just for the fun of it, test this
C#
com.Parameters.Add("@code", SqlDbType.VarChar);
com.Parameters["@code"].Value = metroaccesscontroltxtBox.Text;

instead of
C#
com.Parameters.AddWithValue("@code", metroaccesscontroltxtBox.Text);
 
Share this answer
 
v3
Comments
Rencyrence 19-Nov-15 2:32am    
I still got error, after trying this. it shows "Specified cast not valid" btw my tables are all varchar, i dont know if i have a problem with counting values
Rencyrence 19-Nov-15 2:33am    
Please check again my codes I added more for you to understand, thanks
George Jonsson 19-Nov-15 2:53am    
I just added an alternative approach to the solution.
Rencyrence 19-Nov-15 2:51am    
I tried to google the new error and http://stackoverflow.com/questions/28263029/specified-cast-is-not-valid-error-in-c-sharp helps me also, thanks for the help
George Jonsson 19-Nov-15 2:54am    
OK. Good I could help you on the way.

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