1

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);
3
  • what error do you get? Commented Feb 23, 2013 at 15:53
  • exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll Commented Feb 23, 2013 at 16:46
  • Except that you have to use SelectedItem and not SelectedItems (that does not exist), your code to read the selected value in the combobox seems right. I suppose you have some problem with the connection (is it open when you execute the command?) or with escaping (if you have an ' in your txt controls your sqlString does not work. To find what is happening I suggest you to analyze the InnerException of your Exception, because SqlException is to generic. Hope to help. Commented Feb 23, 2013 at 18:33

1 Answer 1

1

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";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.