-2

How can I use combobox.SelectedValue in SQL query in C#?

using (OracleDB db = new OracleDB())
{
    if (name.Checked)
    {
        DataTable nameDT= db.Select("SELECT name FROM People where name= IN THIS PLACE I NEED SELECTED VALUE FROM COMBOBOX1"), "Osoby");
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0
using (OracleDB db = new OracleDB())
{
    if (name.Checked)
    {
        var sql = String.Format("Select [name] FROM People WHERE [name] = {0}", comboBox1.SelectedItem.ToString())
        var nameDT = db.Select(sql, "Osoby");
    }
}

MSDN: ComboBox.SelectedItem Property

SiD
  • 511
  • 4
  • 15
-1

Use this:

"...where name=" + comboBoxName.SelectedValue.ToString()), "Osoby");
Mike Eason
  • 9,525
  • 2
  • 38
  • 63
  • This should be a comment more than an answer. Please re-word it. – AStopher Dec 16 '14 at 11:45
  • It would have been a comment, however I do not have commenting privileges. Such a simple question requires a simple answer. I will re-word it. – Mike Eason Dec 16 '14 at 11:50
  • Not a C# expert, but do the SQL connection classes not have bind variables? – Rob Baillie Dec 16 '14 at 12:03
  • You can bind to properties in SQL Connection classes, however the UI will not be notified when their properties are changed as they do not implement INotifyPropertyChanged. It might be worth checking this tutorial for more information on INotifyPropertyChanged: [link](http://msdn.microsoft.com/en-us/library/ms743695%28v=vs.110%29.aspx) – Mike Eason Dec 16 '14 at 12:10