user2102572
user2102572

Reputation: 71

How to use Combobox's values in Query

Im using Combobox filled with Tables Names. When I made Insert command and determine the table name by getting the values of the Combobox it didn't work

           private void Form2_Load(object sender, EventArgs e)
    {
        Conn.Open();
            foreach (DataRow dr in dt.Rows)
            {
                comboBox1.Items.Add(dr["TABLE_NAME"].ToString());
            }
        Conn.Close();  

    }

//cmd = new SqlCommand("insert into dbo."+ comboBox1.SelectedItems.ToString()     +"(Phone, Email, Address) values('" + txt1.Text + "','" + txt2.Text + "','" + txt3.Text + "')", Conn);

Upvotes: 1

Views: 223

Answers (1)

Camilo Aguilar
Camilo Aguilar

Reputation: 129

Combobox article

You can find there a very good resource for everything for combobox. You can use the datasource property and let know the values and display for what you need.

comboBox1.datasource = datatable;
comboBox1.displayname = "ColumnNameForDisplayName";
comboBox1.valuename = "ColumnsNameForValueName";

Upvotes: 1

Related Questions