Reputation: 11
I am getting an error at the ExecuteNonQuery
and really don't know why. I spent a lot of time searching the web and realized that User
has to be between []
, but it hasn't solved my problem.
else {
DataTable table = new DataTable();
string query = "SELECT * FROM [User] WHERE Email = '" + tbMail.Text + "'";
OleDbDataAdapter adapter = new OleDbDataAdapter(query, connectionString);
int count = adapter.Fill(table);
if (count != 0) {
MessageBox.Show("This email is already in use", "Email in use", MessageBoxButtons.OK, MessageBoxIcon.Information);
} else {
OleDbConnection connection = new OleDbConnection(connectionString);
OleDbCommand insertCommand = new OleDbCommand();
adapter = new OleDbDataAdapter();
string encryptedPassword = Convert.ToBase64String(System.Security.Cryptography.MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(tbPass.Text)));
connection.Open();
string command = "INSERT INTO [User] (Username, Password, Email) VALUES('" + tbUser.Text + "', '" + encryptedPassword + "', " + tbMail.Text + ")";
insertCommand.Connection = connection;
insertCommand.CommandText = command;
adapter.InsertCommand = insertCommand;
adapter.InsertCommand.ExecuteNonQuery();
connection.Close();
}
}
The error is:
There was an error parsing the query. [Token line number,Token line offset,,Token in error,,]"
Upvotes: 1
Views: 1365
Reputation: 22945
Your email value must be between quotes, just like username and password.
Another point is that you should use sql parameters to prevent sql injection attacks.
Upvotes: 2