本文介绍了ComboBox值未通过插入数据库中的新记录来更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在数据库中插入一条记录,除了更新 ComboBox
的 DataSource
之外,其他一切工作正常。
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();
推荐答案
尝试使用SqlCommandBuilder。
指向msdn文章
Try to use SqlCommandBuilder.This link to msdn article 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();
}*/
}
}
这篇关于ComboBox值未通过插入数据库中的新记录来更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!