使用插入数据库中的新记录不会更新ComboBox值

时间:2012-04-01 16:28:18

标签: c# winforms combobox data-binding

我在数据库中插入记录,除了更新DataSource的{​​{1}}外,一切正常。

这里是刷新组合框的代码:

ComboBox

以上代码对我不起作用。

有人可以帮我解决如何使用数据库中插入的新值更新Combobox吗?

编辑:

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

EDIT1:

    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();
            }
        }

2 个答案:

答案 0 :(得分:0)

您应该尝试在设置新值之前将DataSource设置为null来重置绑定:

...
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();
...

答案 1 :(得分:0)

尝试使用SqlCommandBuilder。 此链接指向msdn文章http://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.update.aspx

我更新了你的代码。这对我有用。

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();
        }*/
    }
}