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. i set combobox data source value and cleare item also but just add a new item combo list is add new list in old list my code is

i set combobox data source value and cleare item also but just add a new item combo list is add new list in old list my code is

Scheduled Pinned Locked Moved C#
security
10 Posts 4 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.
  • M Offline
    M Offline
    Member 10669301
    wrote on last edited by
    #1

    namespace AddItemInComboBox { public partial class Form1 : Form { const string conString = "Data Source=ABHINANDANCOMPU;Initial Catalog=Color;Integrated Security=True"; public SqlConnection con = new SqlConnection(conString); public SqlDataAdapter da = new SqlDataAdapter(); public DataSet ds = new DataSet(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { cmbCompany.DataSource = null; ComboBoxAddData("companyName", "companyId"); } private void ComboBoxAddData(string DisplayMember, string ValueMember) { cmbCompany.DataSource = null; cmbCompany.Items.Clear(); DataTable dt = new DataTable(); SqlCommand cmd = new SqlCommand("SELECT [companyId],[companyName] FROM tblMasterCompany", con); con.Open(); da = new SqlDataAdapter(cmd); da.Fill(ds, "tblMasterCompany"); cmbCompany.DataSource = ds.Tables["tblMasterCompany"]; this.cmbCompany.ValueMember = "companyId"; MessageBox.Show("Add SuccessFully"); da.Dispose(); con.Close(); } private void btnSave_Click(object sender, EventArgs e) { con.Open(); SqlCommand cmd = new SqlCommand("INSERT INTO tblMasterCompany ([companyName] ,[address] ,[city]) VALUES ('" + txtCompanyName.Text + "', '" + txtAddress.Text + "', '" + txtCity.Text + "')", con); cmd.ExecuteNonQuery(); MessageBox.Show("Save "); con.Close(); Form1_Load(sender, e); } } }

    OriginalGriffO A 2 Replies Last reply
    0
    • M Member 10669301

      namespace AddItemInComboBox { public partial class Form1 : Form { const string conString = "Data Source=ABHINANDANCOMPU;Initial Catalog=Color;Integrated Security=True"; public SqlConnection con = new SqlConnection(conString); public SqlDataAdapter da = new SqlDataAdapter(); public DataSet ds = new DataSet(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { cmbCompany.DataSource = null; ComboBoxAddData("companyName", "companyId"); } private void ComboBoxAddData(string DisplayMember, string ValueMember) { cmbCompany.DataSource = null; cmbCompany.Items.Clear(); DataTable dt = new DataTable(); SqlCommand cmd = new SqlCommand("SELECT [companyId],[companyName] FROM tblMasterCompany", con); con.Open(); da = new SqlDataAdapter(cmd); da.Fill(ds, "tblMasterCompany"); cmbCompany.DataSource = ds.Tables["tblMasterCompany"]; this.cmbCompany.ValueMember = "companyId"; MessageBox.Show("Add SuccessFully"); da.Dispose(); con.Close(); } private void btnSave_Click(object sender, EventArgs e) { con.Open(); SqlCommand cmd = new SqlCommand("INSERT INTO tblMasterCompany ([companyName] ,[address] ,[city]) VALUES ('" + txtCompanyName.Text + "', '" + txtAddress.Text + "', '" + txtCity.Text + "')", con); cmd.ExecuteNonQuery(); MessageBox.Show("Save "); con.Close(); Form1_Load(sender, e); } } }

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      First off, format that code so we can tell what is going on: edit the question, ands use <pre>...</pre> tags round it to preserve the formatting. Then try explaining in more detail in the question itself what you are doing to cause the problem and what effects it is having: Remember that we can't see your screen, access your HDD, or read your mind!

      Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      M 2 Replies Last reply
      0
      • OriginalGriffO OriginalGriff

        First off, format that code so we can tell what is going on: edit the question, ands use <pre>...</pre> tags round it to preserve the formatting. Then try explaining in more detail in the question itself what you are doing to cause the problem and what effects it is having: Remember that we can't see your screen, access your HDD, or read your mind!

        Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

        M Offline
        M Offline
        Member 10669301
        wrote on last edited by
        #3

        sir i want refresh list in combo box after create a new item

        OriginalGriffO 1 Reply Last reply
        0
        • M Member 10669301

          sir i want refresh list in combo box after create a new item

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          Please, read what I said in the previous message, and do that. I am not looking at your code while you make it difficult for me! :laugh:

          Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            First off, format that code so we can tell what is going on: edit the question, ands use <pre>...</pre> tags round it to preserve the formatting. Then try explaining in more detail in the question itself what you are doing to cause the problem and what effects it is having: Remember that we can't see your screen, access your HDD, or read your mind!

            Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

            M Offline
            M Offline
            Member 10669301
            wrote on last edited by
            #5

            private void ComboBoxAddData(string DisplayMember, string ValueMember) { //

            combo box cmbcomapny is not refresh Item list It just Add Item Again and again

            cmbCompany.DataSource = null; cmbCompany.Items.Clear(); DataTable dt = new DataTable(); SqlCommand cmd = new SqlCommand("SELECT [companyId],[companyName] FROM tblMasterCompany", con); con.Open(); da = new SqlDataAdapter(cmd); da.Fill(ds, "tblMasterCompany"); cmbCompany.DataSource = ds.Tables["tblMasterCompany"]; this.cmbCompany.DisplayMember = "companyName"; this.cmbCompany.ValueMember = "companyId"; MessageBox.Show("Add SuccessFully"); da.Dispose(); con.Close(); }

            M 1 Reply Last reply
            0
            • M Member 10669301

              private void ComboBoxAddData(string DisplayMember, string ValueMember) { //

              combo box cmbcomapny is not refresh Item list It just Add Item Again and again

              cmbCompany.DataSource = null; cmbCompany.Items.Clear(); DataTable dt = new DataTable(); SqlCommand cmd = new SqlCommand("SELECT [companyId],[companyName] FROM tblMasterCompany", con); con.Open(); da = new SqlDataAdapter(cmd); da.Fill(ds, "tblMasterCompany"); cmbCompany.DataSource = ds.Tables["tblMasterCompany"]; this.cmbCompany.DisplayMember = "companyName"; this.cmbCompany.ValueMember = "companyId"; MessageBox.Show("Add SuccessFully"); da.Dispose(); con.Close(); }

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              Is it displaying the data from the database or the name of the dataitem. Check the spelling (capitalisation) of your display and value member fields.

              Never underestimate the power of human stupidity RAH

              M OriginalGriffO 2 Replies Last reply
              0
              • M Mycroft Holmes

                Is it displaying the data from the database or the name of the dataitem. Check the spelling (capitalisation) of your display and value member fields.

                Never underestimate the power of human stupidity RAH

                M Offline
                M Offline
                Member 10669301
                wrote on last edited by
                #7

                private void ComboBoxAddData(string DisplayMember, string ValueMember) { // - > COMMENT LINE FOR DETAILING OUR CODE // PROBLEM IS combo box cmbcomapny is not refresh Item list AFTER WE SET NULL VALUE IN + // + COMBO BOX DATA SOUCE // IT NOT CLEAR OLD LIST AND ADD NEW LIST BELOW THEIR WHEN THIS FUNCTION CALL // cmbCompany IS A COMBO BOX // cmd IS A SQL COMMAND WHICH SELECT TWO FILED 1 -> COMPANYID AND 2-> COMPANY NAME FROM tblMasterCompany (TABLE NAME) // con IS A CONNECTION cmbCompany.DataSource = null; cmbCompany.Items.Clear(); SqlCommand cmd = new SqlCommand("SELECT [companyId],[companyName] FROM tblMasterCompany", con); con.Open(); da = new SqlDataAdapter(cmd); da.Fill(ds, "tblMasterCompany"); cmbCompany.DataSource = ds.Tables["tblMasterCompany"]; this.cmbCompany.DisplayMember = "companyName"; this.cmbCompany.ValueMember = "companyId"; MessageBox.Show("Add SuccessFully"); da.Dispose(); con.Close(); }

                1 Reply Last reply
                0
                • M Mycroft Holmes

                  Is it displaying the data from the database or the name of the dataitem. Check the spelling (capitalisation) of your display and value member fields.

                  Never underestimate the power of human stupidity RAH

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  Down vote countered.

                  Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  M 1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Down vote countered.

                    Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                    M Offline
                    M Offline
                    Mycroft Holmes
                    wrote on last edited by
                    #9

                    OriginalGriff wrote:

                    Down vote countered

                    And then some - I have the power thanks

                    Never underestimate the power of human stupidity RAH

                    1 Reply Last reply
                    0
                    • M Member 10669301

                      namespace AddItemInComboBox { public partial class Form1 : Form { const string conString = "Data Source=ABHINANDANCOMPU;Initial Catalog=Color;Integrated Security=True"; public SqlConnection con = new SqlConnection(conString); public SqlDataAdapter da = new SqlDataAdapter(); public DataSet ds = new DataSet(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { cmbCompany.DataSource = null; ComboBoxAddData("companyName", "companyId"); } private void ComboBoxAddData(string DisplayMember, string ValueMember) { cmbCompany.DataSource = null; cmbCompany.Items.Clear(); DataTable dt = new DataTable(); SqlCommand cmd = new SqlCommand("SELECT [companyId],[companyName] FROM tblMasterCompany", con); con.Open(); da = new SqlDataAdapter(cmd); da.Fill(ds, "tblMasterCompany"); cmbCompany.DataSource = ds.Tables["tblMasterCompany"]; this.cmbCompany.ValueMember = "companyId"; MessageBox.Show("Add SuccessFully"); da.Dispose(); con.Close(); } private void btnSave_Click(object sender, EventArgs e) { con.Open(); SqlCommand cmd = new SqlCommand("INSERT INTO tblMasterCompany ([companyName] ,[address] ,[city]) VALUES ('" + txtCompanyName.Text + "', '" + txtAddress.Text + "', '" + txtCity.Text + "')", con); cmd.ExecuteNonQuery(); MessageBox.Show("Save "); con.Close(); Form1_Load(sender, e); } } }

                      A Offline
                      A Offline
                      AshishvermaMCA
                      wrote on last edited by
                      #10

                      If you want to refresh the page, You can Try this. under the function of ComboBoxAddData() call Form1_Load(null,null). Now You can bind the your ComboBox will null values. Plz check it.

                      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