locked
select field with dropdownlist RRS feed

  • Question

  • User-646447061 posted

    i have code like this : SqlCommand cmd = new SqlCommand("select * from employe where name = '" + txtsearch.Text + "' ", con);

    and i want to change field name, field name get from dropdownlist1 value..

    i try to write code like this but not work : SqlCommand cmd = new SqlCommand("select * from employe where '" + dropdownlist1.selectedvalue + "' = '" + txtsearch.Text + "' ", con);

    Friday, February 5, 2016 11:59 AM

Answers

  • User1577371250 posted

    Hi,

    try this

     SqlCommand cmd = new SqlCommand(string.Format("select * from employe where {0} = '{1}'", dropdownList1.SelectedValue, txtsearch.Text), con);

    Your query has extra quote for the column name.

    SqlCommand cmd = new SqlCommand("select * from employe where " + dropdownlist1.selectedvalue + " = '" + txtsearch.Text + "' ", con);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 5, 2016 12:37 PM

All replies

  • User1577371250 posted

    Hi,

    try this

     SqlCommand cmd = new SqlCommand(string.Format("select * from employe where {0} = '{1}'", dropdownList1.SelectedValue, txtsearch.Text), con);

    Your query has extra quote for the column name.

    SqlCommand cmd = new SqlCommand("select * from employe where " + dropdownlist1.selectedvalue + " = '" + txtsearch.Text + "' ", con);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 5, 2016 12:37 PM
  • User-646447061 posted

    hahah, thank you .. =D

    Friday, February 5, 2016 2:36 PM