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
-
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); } } }
-
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); } } }
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 – ∞)
-
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 – ∞)
sir i want refresh list in combo box after create a new item
-
sir i want refresh list in combo box after create a new item
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 – ∞)
-
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 – ∞)
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(); }
-
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(); }
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
-
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
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(); }
-
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
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 – ∞)
-
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 – ∞)
OriginalGriff wrote:
Down vote countered
And then some -
I have the power
thanksNever underestimate the power of human stupidity RAH
-
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); } } }
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.