System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'User'

ygzmglkc picture ygzmglkc · May 25, 2015 · Viewed 10.4k times · Source
Baglanti.FnkBaglan();
SqlCommand KayitSorgulaUsername = new SqlCommand("SELECT Username FROM User Where Username= @Username AND Username IS NOT NULL ", Baglanti.baglan);
SqlCommand KayitSorgulaMail = new SqlCommand("SELECT Mail FROM DatabaseProje.User Where Mail= @Mail  AND Username IS NOT NULL ", Baglanti.baglan);
KayitSorgulaUsername.Parameters.AddWithValue("@Username", TxtUyeOlUsername.Text);
KayitSorgulaMail.Parameters.AddWithValue("@Mail", TxtUyeOlMail.Text);
SqlDataReader VeriOkuUsername = KayitSorgulaUsername.ExecuteReader();
SqlDataReader VeriOkuMail = KayitSorgulaMail.ExecuteReader();

System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'User'.

This error appears after I entered the values to textboxes. How can I fix this error?

Answer

Ehsan Sajjad picture Ehsan Sajjad · May 25, 2015

User is a reserved word in sql so use it with square brackets, so that it is considered as table name:

SELECT Username FROM [User]