Hi I have the following coding whereby I am trying to show Properties that have their price within the from and to drop down menus. However it only works well when the from price is set to 0. when I do price from 1000 to 800000, properties within that range are not shown.

SqlConnection con = new SqlConnection(connString);
        con.Open();
        string sql = "SELECT RefNo, Price, Location, Type, Image_Path1 FROM Properties WHERE  Basis = '" + basis.SelectedItem + "'and Location = '" + location.SelectedItem + "'  and Type = '" + type.SelectedItem + "' and Price > '" + from.SelectedItem + "' and Price < '" + to.SelectedItem + "' ";
        SqlCommand cmd = new SqlCommand(sql, con);

        try
        {
            datalist1.DataSource = cmd.ExecuteReader();
            datalist1.DataBind();
            cmd.Connection.Dispose();
            con.Close();
        }

Dani AI

Generated

Short summary and likely cause: is getting no rows when the "from" value is nonzero because the drop‑down values and the database column are not being compared as the same numeric type. was right to ask about the column type, and was right to suggest parameters — both are essential to fix this reliably.

What to do (practical steps)

  • Put plain numeric strings (no commas/currency) into the drop‑down Value attributes and read SelectedValue on the server side. The SelectedValue property is the correct place to get the raw value from a ListControl (SelectedValue docs).
  • Parse the SelectedValue into an int/decimal in C# and validate that min <= max.
  • Use parameterized SQL and pass numeric parameters (SqlParameter with the proper SqlDbType) instead of concatenating strings. Using parameters prevents type-mismatch surprises and SQL injection; see the guidance for adding parameters (AddWithValue / parameters docs).

Troubleshooting notes

  • If the Price column is a text/varchar field with formatting (commas, currency symbols), comparisons will fail or conversions will error. Best fix: change the column to a numeric type. If that is not possible, normalize values (remove commas) or use TRY_CONVERT/CAST in SQL (see ) — but converting at query time is slower and fragile.
  • When debugging, log the parsed numeric values and parameter types rather than building a raw SQL string. Also check for the simple case of swapped bounds (from > to) which would return no rows.

Recommended Answers

All 2 Replies

What type is Price and what values do you have in from.SelectedItem/to.SelectedItem? I'd take a look at what the sql variable looks like before you call ExecuteReader().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.