Click here to Skip to main content
15,888,224 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I get this error every time I try to change the dropdownlist1 index value which are only two values, sale and rent; "incorrect syntax near '='"

Could someone help me please?
-------------------
string select;
        select = "SELECT * FROM Property";
        select += "WHERE Sale_Rent='" + DropDownList1.SelectedItem.Value.ToString() + "'";
        SqlConnection connect1 = new SqlConnection(connectionString);

        SqlCommand myCommand = new SqlCommand(select, connect1);

       
        SqlDataReader readIt;

        try
        {
            connect1.Open();//make connection

            readIt = myCommand.ExecuteReader();

            while (readIt.Read())
            {
                ListItem newItem = new ListItem();
                newItem.Text = readIt["Address"].ToString();
                newItem.Value = readIt["Property_ID"].ToString();
                box1.Items.Add(newItem);
            }
            readIt.Close();
        }
Posted

The syntax error is because you need a space between "Property" and "WHERE" - at the moment your select statement evaluates to:
"SELECT * FROM PropertyWHERE Sale_Rent='Rent'"


In addition:
1) Tag your question with ASP.NET, rather than C# - a DropDownList with a Value property does not exist outside ASP.NET.
2) Try to get into the habit of using parameterized queries: I know that your existing code has no risk of an SQL Injection attack, but when you re-use this code in a month for you text box, you won't think about it. It is well worth getting into the habit early, and using it at all times.
3) Why didn't you try using the debugger? That would have shown you the problem in thirty seconds!
 
Share this answer
 
Hello,
select += "WHERE Sale_Rent='" + DropDownList1.SelectedItem.Value.ToString() + "'";

change of above line as

select += "WHERE Sale_Rent='" + DropDownList1.SelectedValue.ToString() + "'";
 
Share this answer
 
There is no space between Property and Where in your SQL statement. That must be causing the error.
 
Share this answer
 
Thanks for all your replies, appreciated.

:)
 
Share this answer
 

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