ComboBox value is not getting updated with new record inserted in Database

删除回忆录丶 提交于 2019-12-13 03:38:31

问题


I am inserting a record in Database and everything is working fine except updating the DataSource for ComboBox.

Here my code for refreshing the combobox:

nStateTableAdapter1.Adapter.Update(stateCodeDataSet, "NState");
cmbStateCode.DataSource = nStateBindingSource1.DataSource;
cmbStateCode.DisplayMember = "NState.CountryCode";
cmbStateCode.ValueMember = "NState.CountryCode";
cmbStateCode.Refresh();

Above code is not working for me.

Can anyone help me how to update the Combobox with the new Value inserted in the Database?

EDIT :

    private void btnSave_Click(object sender, EventArgs e)
    {
        if (cmbStateCode.Text.ToString().Trim() == "" && txtCountryName.Text.ToString().Trim() == "")
        {
            MessageBox.Show("Please enter a valid data.", "Office Automation System", MessageBoxButtons.OK, MessageBoxIcon.None);
        }
        else
        {
            btnSave.Enabled = false;
            btnEdit.Enabled = true;
            try
            {
                string Query;
                sqlCon.Open();
                if (isEditMode)
                    Query = "UPDATE NState SET CountryName='" + txtCountryName.Text.ToString().Trim() + "' WHERE CountryCode='" + cmbStateCode.Text + "'";
                else
                    Query = "INSERT INTO NState VALUES ('" + cmbStateCode.Text + "','" + txtCountryName.Text.ToString().Trim() + "')";
                SqlCommand sqlCmd = new SqlCommand(Query, sqlCon);
                sqlCmd.ExecuteNonQuery();
                cmbStateCode.DropDownStyle = ComboBoxStyle.DropDownList;
                MessageBox.Show("Record saved successfully.", "Office Automation System", MessageBoxButtons.OK, MessageBoxIcon.None);
            }
            catch
            {
                MessageBox.Show("Error occured while saving record.\nPlease check the StateCode for duplicate.", "Office Automation System", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {
                sqlCon.Close();
            }
            try
            {
                sqlCon.Open();
                fillStateInfo();
                nStateTableAdapter1.Adapter.Update(stateCodeDataSet, "NState");
                cmbStateCode.DataSource = nStateBindingSource1.DataSource;
                cmbStateCode.DisplayMember = "NState.CountryCode";
                cmbStateCode.ValueMember = "NState.CountryCode";
                cmbStateCode.Refresh();
            }
            catch (Exception ex)
            {
            }
            finally
            {
                sqlCon.Close();
            }
        }

EDIT1 :

  sqlCon.Open();
  SqlDataAdapter da = new SqlDataAdapter("select * from NState", sqlCon);
  SqlCommandBuilder builder = new SqlCommandBuilder(da);
  if (isEditMode)
      nStateTableAdapter1.Adapter.UpdateCommand = builder.GetUpdateCommand();
  else
      nStateTableAdapter1.Adapter.InsertCommand = builder.GetInsertCommand();
  nStateTableAdapter1.Adapter.Update(stateCodeDataSet, "NState");
  fillStateInfo();

回答1:


Try to use SqlCommandBuilder. This link to msdn article http://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.update.aspx

And I updated your code. It's works for me.

private void btnSave_Click(object sender, EventArgs e)
{
    if (cmbStateCode.Text.ToString().Trim() == "" && txtCountryName.Text.ToString().Trim() == "")
    {
        MessageBox.Show("Please enter a valid data.", "Office Automation System", MessageBoxButtons.OK, MessageBoxIcon.None);
    }
    else
    {
        btnSave.Enabled = false;
        btnEdit.Enabled = true;
        // update your DataSet directly instead of this
        /*try
        {
            string Query;
            sqlCon.Open();
            if (isEditMode)
                Query = "UPDATE NState SET CountryName='" + txtCountryName.Text.ToString().Trim() + "' WHERE CountryCode='" + cmbStateCode.Text + "'";
            else
                Query = "INSERT INTO NState VALUES ('" + cmbStateCode.Text + "','" + txtCountryName.Text.ToString().Trim() + "')";
            SqlCommand sqlCmd = new SqlCommand(Query, sqlCon);
            sqlCmd.ExecuteNonQuery();
            cmbStateCode.DropDownStyle = ComboBoxStyle.DropDownList;
            MessageBox.Show("Record saved successfully.", "Office Automation System", MessageBoxButtons.OK, MessageBoxIcon.None);
        }
        catch
        {
            MessageBox.Show("Error occured while saving record.\nPlease check the StateCode for duplicate.", "Office Automation System", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        finally
        {
            sqlCon.Close();
        }*/

        SqlCommandBuilder builder = new SqlCommandBuilder(nStateTableAdapter1);
        if (isEditMode)
        {
            // update DataSet
            nStateTableAdapter1.UpdateCommand = builder.GetUpdateCommand();
        }           
        else
        {
            // insert value to DataSet
            nStateTableAdapter1.InsertCommand = builder.GetUpdateCommand();
        }

        nStateTableAdapter1.Adapter.Update(stateCodeDataSet, "NState");

        // it's not necessary, ComboBox will have a new values  
        /*try
        {
            sqlCon.Open();
            fillStateInfo();
            nStateTableAdapter1.Adapter.Update(stateCodeDataSet, "NState");
            cmbStateCode.DataSource = nStateBindingSource1.DataSource;
            cmbStateCode.DisplayMember = "NState.CountryCode";
            cmbStateCode.ValueMember = "NState.CountryCode";
            cmbStateCode.Refresh();
        }
        catch (Exception ex)
        {
        }
        finally
        {
            sqlCon.Close();
        }*/
    }
}



回答2:


You should try to reset the binding by setting the DataSource to null before setting a new value:

...
sqlCon.Open();
fillStateInfo();
nStateTableAdapter1.Adapter.Update(stateCodeDataSet, "NState");
cmbStateCode.DataSource = null; // reset binding
cmbStateCode.DataSource = nStateBindingSource1.DataSource;
cmbStateCode.DisplayMember = "NState.CountryCode";
cmbStateCode.ValueMember = "NState.CountryCode";
cmbStateCode.Refresh();
...


来源:https://stackoverflow.com/questions/9966078/combobox-value-is-not-getting-updated-with-new-record-inserted-in-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!