Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I keep getting the error "Incorrect syntax near '='"
Below is my code.
I can't find fault in it could someone please take a look.

Thanks.

private void Information_Load(object sender, EventArgs e)
        {
            string dbs = "Data Source=OMEGA;Initial Catalog=Military;Integrated Security=true;";
            string sql = "SELECT * From Personal WHERE UserId = " + comboBox1.Text;
            using (SqlConnection connection = new SqlConnection(dbs))
            {
                SqlDataAdapter da = new SqlDataAdapter(new SqlCommand(sql, connection));
                DataSet ds = new DataSet("Image");
                ds.Tables.Add("Image");
                da.Fill(ds.Tables["Image"]);
                listBox1.DataSource = ds.Tables["Image"];
                listBox1.DisplayMember = "UserID";
            }

        }
Posted
Comments
CPallini 19-May-11 14:16pm    
Wow, you're posting here top secret military code?
:-D
yesotaso 19-May-11 14:59pm    
Not only military... look at data source!!

1 solution

Your problem is because you are not using quotes around your UserId.

So, change the following line:

string sql = "SELECT * From Personal WHERE UserId = " + comboBox1.Text;


To:

string sql = "SELECT * From Personal WHERE UserId = '" + comboBox1.Text + "'";


Assuming you have complete control over the values that are placed into the combobox.

Personally, I would use SqlParameters for this, as it would help prevent SQL injection issues.
 
Share this answer
 
Comments
fjdiewornncalwe 19-May-11 14:15pm    
Likely the issue and a great suggestion. +5
WurmInfinity 19-May-11 14:33pm    
fixed thanks

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