Problem with ComboBox Selected.Item
-
I have a ComboBox that is populated with values from a MS Access table. Next I use the below code to delete the record that is selected in the comboBox. private void button3_Click(object sender, EventArgs e) { OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Magazi.mdb"); conn.Open(); OleDbCommand cmd = new OleDbCommand("DELETE FROM Suppliers WHERE Name=@Name", conn); OleDbParameter parDel = new OleDbParameter(); parDel.ParameterName = "@Name"; parDel.Value = comboBox1.SelectedItem; cmd.Parameters.Add(parDel); cmd.ExecuteNonQuery(); conn.Close(); } For some reason I get an "OleDbException was unhandled" message...
-
I have a ComboBox that is populated with values from a MS Access table. Next I use the below code to delete the record that is selected in the comboBox. private void button3_Click(object sender, EventArgs e) { OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Magazi.mdb"); conn.Open(); OleDbCommand cmd = new OleDbCommand("DELETE FROM Suppliers WHERE Name=@Name", conn); OleDbParameter parDel = new OleDbParameter(); parDel.ParameterName = "@Name"; parDel.Value = comboBox1.SelectedItem; cmd.Parameters.Add(parDel); cmd.ExecuteNonQuery(); conn.Close(); } For some reason I get an "OleDbException was unhandled" message...
-
I have a ComboBox that is populated with values from a MS Access table. Next I use the below code to delete the record that is selected in the comboBox. private void button3_Click(object sender, EventArgs e) { OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Magazi.mdb"); conn.Open(); OleDbCommand cmd = new OleDbCommand("DELETE FROM Suppliers WHERE Name=@Name", conn); OleDbParameter parDel = new OleDbParameter(); parDel.ParameterName = "@Name"; parDel.Value = comboBox1.SelectedItem; cmd.Parameters.Add(parDel); cmd.ExecuteNonQuery(); conn.Close(); } For some reason I get an "OleDbException was unhandled" message...
-
SelectedItem.ToString(); might solve your problem. If you're adding objects rather than strings so your comboBox make sure you've overridden the ToString method.
Ok. I have tried some things and this what I get... Using the below code and I have also display member, value member and SelectedValue of the comBox set to Name. private void button3_Click(object sender, EventArgs e) { OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Magazi.mdb"); conn.Open(); OleDbCommand cmd = new OleDbCommand("DELETE FROM Suppliers WHERE Name=@Name", conn); OleDbParameter parDel = new OleDbParameter(); parDel.ParameterName = "@Name"; parDel.Value = comboBox1.SelectedValue.ToString(); cmd.Parameters.Add(parDel); cmd.ExecuteNonQuery(); conn.Close(); } When I try to select a value from comboBox I get this message: Column 'Name' is constrained to be unique. Value 'Champion' is already present.
-
I have a ComboBox that is populated with values from a MS Access table. Next I use the below code to delete the record that is selected in the comboBox. private void button3_Click(object sender, EventArgs e) { OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Magazi.mdb"); conn.Open(); OleDbCommand cmd = new OleDbCommand("DELETE FROM Suppliers WHERE Name=@Name", conn); OleDbParameter parDel = new OleDbParameter(); parDel.ParameterName = "@Name"; parDel.Value = comboBox1.SelectedItem; cmd.Parameters.Add(parDel); cmd.ExecuteNonQuery(); conn.Close(); } For some reason I get an "OleDbException was unhandled" message...
-
I have a ComboBox that is populated with values from a MS Access table. Next I use the below code to delete the record that is selected in the comboBox. private void button3_Click(object sender, EventArgs e) { OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Magazi.mdb"); conn.Open(); OleDbCommand cmd = new OleDbCommand("DELETE FROM Suppliers WHERE Name=@Name", conn); OleDbParameter parDel = new OleDbParameter(); parDel.ParameterName = "@Name"; parDel.Value = comboBox1.SelectedItem; cmd.Parameters.Add(parDel); cmd.ExecuteNonQuery(); conn.Close(); } For some reason I get an "OleDbException was unhandled" message...
Hi Guy I seen your code in above. i seen may be ur combobox haven't a selected value may be pass null value, so error will raise. and second may be comboBox haven't records. so verify both condition after get comboBox1.SelectedItem. when directly to use comboBox value we must do validations.that's give error free to us. thanks
Cheers RRave MCTS,MCPD