Click here to Skip to main content
15,607,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
What I'm trying to achieve:

To delete a row from SQL table1 where column1 = @myParameter.
Where @myParameter holds the value of my current comboBox1 selection.
My ComboBox populates fine, and is bound to a DataSet.
This is my code:

using (SqlConnection sqlCon = new SqlConnection(conString))
using (SqlCommand sqlCmd = new SqlCommand(delString, SqlCon)) {

    sqlCmd.Parameters.AddWithValue("@myParameter", SqlDbType.NVarChar).Value = 


When I run the code, the database does not update.

A separate issue is my code to remove the selecteditem from the ComboBox itself.
I use:

var i = comboBox1
comboBox1.DataSource = null;

What should I write in order to re-bind it to the DataSet?

This is how I populate my ComboBox:

DataSet MyDataSet() {

    DataSet ds = new DataSet();

    using (SqlConnection sqlCon = new SqlConnection(connString))
    using (SqlDataAdapter sqlDaAd = new SqlDataAdapter("myStoredProcedure", sqlCon)) {

        sqlDaAd.SelectCommand.CommandType = CommandType.StoredProcedure;
        sqlDaAd.SelectCommand.Parameters.Add("@myParameter", SqlDbType.NVarChar).Value = 

    return ds;


DataSet ds = MyDataSet();
    comboBox1.DataSource = ds.Tables[0];
    comboBox1.DisplayMember = "column3";
    comboBox1.ValueMember = "column2";

Thanks alot for any advice you might be able to give me!

What I have tried:

Spent close to a full day trying different things.
Updated 18-May-21 3:23am
CHill60 18-May-21 8:57am    
Re your first problem "When I run the code, the database does not update." - what is in delString?

var i = comboBox1
comboBox1.DataSource = null;

What on earth did you expect this code to actually do?
Remove an item from itself? You do understand what a combo box list is, don't you?

You don't change the ComboBox: you change the DataTable it is bound to:

private DataTable items = new DataTable();
private void FrmMain_Shown(object sender, EventArgs e)
    items.Rows.Add("", "CodeProject");
    items.Rows.Add("", "The Lounge");
    items.Rows.Add("", "Unanswered Questions");
    BindingSource bs = new BindingSource();
    bs.DataSource = items;
    myComboBox.DataSource = items;
    myComboBox.DisplayMember = "Name";
    myComboBox.ValueMember = "URL";
private void butAdd_Click(object sender, EventArgs e)
    items.Rows.Add("A new URL", "A new name");
private void butRemove_Click(object sender, EventArgs e)
Share this answer
Thank you both for your inputs. As for why I couldn't delete any rows, I figured out what the problem was.

My code that gave value to my parameter for the DELETE statement, returned:


E.g. it returned the object, and not the value as string.

So I changed my code to this:

DataRowView oDataRowView = comboBox1.SelectedItem as DataRowView;
string sValue = string.Empty;

if (oDataRowView != null) {

    sValue = oDataRowView.Row["column1"] as string;
    sqlCmd.Parameters.Add("@myParameter", SqlDbType.NVarChar).Value = sValue = oDataRowView.Row["column1"] as string;
    sqlCmd.CommandType = CommandType.Text;

I still have to tackle the issue OriginalGriff gave me an answer to, but at least this (the main issue) got resolved.

I have a sneaking suspicion that what I pasted above is not as efficient as it could be, although it works.
If anyone sees any bloats, feel free to tell me.
Share this answer
FranzBe 18-May-21 9:47am    
You are binding a DataTable to a ComboBox. The DataTable is a in-memory representation from something in the database. Now when you delete something in the database, the two are out of sync. As OriginalGriff pointed out: you need to update the DataTable you bound your control to. Make some loadData() routine, and call it upon Form_Load _and_ when you delete something.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900