Incorrect syntax near the keyword 'User'
Solution 1
"User" is a reserved word in SQL Server, so you have to use a delimited identifier to refer to your table. Try
SqlCommand command4 = new SqlCommand("SELECT * FROM [User]", conn);
instead... or rename the table to something which isn't reserved.
(I'd also strongly advise you to keep the data access out of your UI code, dispose of connections properly etc... but that's a different matter.)
Solution 2
User
is a built-in function in SQL Server. You need to surround the name with square brackets: [User]
. This goes for all table names and other user-defined names that happen to collide with keywords, reserved words or built-in names, so I suspect you will need to write [Order]
as well, since ORDER
is an SQL keyword.
Paul Moldovan
Updated on June 24, 2022Comments
-
Paul Moldovan almost 2 years
The error is:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Incorrect syntax near the keyword 'User'.The code is:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApplication1 { public partial class Form1 : Form { SqlConnection conn; SqlDataAdapter GameDA; SqlDataAdapter DetailDA; DataSet DetailDS; SqlCommandBuilder cmdBuilder; SqlDataAdapter UserDA; SqlDataAdapter AdministratorDA; SqlDataAdapter OrderDA; DataSet OrderDS; SqlCommandBuilder cmdBuilder2; public Form1() { InitializeComponent(); conn = new SqlConnection("Data Source=HOME-AC284121FE\\SQLEXPRESS;Initial Catalog=GameShop;Integrated Security=SSPI;"); SqlCommand command1 = new SqlCommand("SELECT * FROM Game", conn); GameDA = new SqlDataAdapter(command1); SqlCommand command2 = new SqlCommand("SELECT * FROM Detail WHERE GameID = @GameID", conn); command2.Parameters.Add(new SqlParameter("@GameID", SqlDbType.Int)); DetailDA = new SqlDataAdapter(command2); SqlCommand command3 = new SqlCommand("SELECT * FROM Administrator", conn); AdministratorDA = new SqlDataAdapter(command3); SqlCommand command4 = new SqlCommand("SELECT * FROM User", conn); UserDA = new SqlDataAdapter(command4); SqlCommand command5 = new SqlCommand("SELECT * FROM Order WHERE UserID = @UserID", conn); command5.Parameters.Add(new SqlParameter("@UserID", SqlDbType.Int)); OrderDA = new SqlDataAdapter(command5); cmdBuilder2 = new SqlCommandBuilder(OrderDA); cmdBuilder = new SqlCommandBuilder(DetailDA); } private void Form1_Load(object sender, EventArgs e) { DetailDS = new DataSet(); OrderDS = new DataSet(); GameDA.Fill(DetailDS, "Game"); **UserDA.Fill(OrderDS, "User"); // <-- Error** AdministratorDA.Fill(OrderDS, "Administrator"); comboBoxGame.DisplayMember = "Name"; comboBoxGame.ValueMember = "GameID"; comboBoxGame.DataSource = DetailDS.Tables["Game"]; dataGridView.DataSource = DetailDS.Tables["Detail"]; dataGridView.Columns["GameID"].Visible = false; dataGridView.Columns["DetailID"].Visible = false; } private void comboBoxGame_SelectedIndexChanged(object sender, EventArgs e) { if (comboBoxGame.SelectedValue != null) if (DetailDS.Tables.Contains("Detail")) { DetailDS.Tables["Detail"].Clear(); } DetailDA.SelectCommand.Parameters[0].Value = comboBoxGame.SelectedValue; DetailDA.Fill(DetailDS, "Detail"); } private void buttonExit_Click(object sender, EventArgs e) { this.Close(); } } }