Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem with ComboBox Selected.Item

Problem with ComboBox Selected.Item

Scheduled Pinned Locked Moved C#
help
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kallileo
    wrote on last edited by
    #1

    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...

    A D D R 4 Replies Last reply
    0
    • K kallileo

      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...

      A Offline
      A Offline
      AkmalSyed
      wrote on last edited by
      #2

      Plz. use "comboBox1.SelectedValue" rather than comboBox1.SelectedItem;.

      1 Reply Last reply
      0
      • K kallileo

        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...

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        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.

        K 1 Reply Last reply
        0
        • D DaveyM69

          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.

          K Offline
          K Offline
          kallileo
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • K kallileo

            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...

            D Offline
            D Offline
            D i x y
            wrote on last edited by
            #5

            Use this comboBox1.SelectedItem.ToString();

            1 Reply Last reply
            0
            • K kallileo

              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...

              R Offline
              R Offline
              Ravenet
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups