文本框中的更改值未在DataBase中更新

文本框中的更改值未在DataBase中更新

本文介绍了TextBox绑定:文本框中的更改值未在DataBase中更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



问题案例:

adapter.Update调用(dataSet)然后在DataBase中更新数据。



这是我的一些代码。如果需要,可以共享完整的代码。

Dear All,

Problem Case:
When adapter.Update(dataSet) is called then Data is not updated in DataBase.

Here is my some piece of code. Complete code can be shared if demanded.

//Declared at Class level
SqlConnection conn = null;
SqlDataAdapter adptr = null;
DataSet dataSet = new DataSet();

// for Update button click event
private void btn_Update_Click(object sender, EventArgs e)
{
    adptr.UpdateCommand = new SqlCommandBuilder(adptr).GetUpdateCommand();
    adptr.Update(dataSet);
}

//For Fetch button click event //Select command
private void btn_FetchName_Click(object sender, EventArgs e)
{
    textBox_EmpName.DataBindings.Clear();
    dataSet.Clear();
    adptr = new SqlDataAdapter("SELECT EmpId, EmpName FROM EmpBasicInfo WHERE EmpId='" + textBox_EmpId.Text + "'", conn);
    adptr.Fill(dataSet);

    textBox_EmpName.DataBindings.Add(new Binding("Text", dataSet.Tables[0], "EmpName"));
    //MessageBox.Show("Name:" + dataSet.Tables[0].Rows[0][0]);

}

推荐答案

//Alter "btn_FetchName_Click" Event

//Alter Line
textBox_EmpName.DataBindings.Add(new Binding("Text", dataSet1.Tables[0], "EmpName", false, DataSourceUpdateMode.OnPropertyChanged));

//New Line
dataSet1.Tables[0].RowChanged += Form1_RowChanged;


//Add New Event
void Form1_RowChanged(object sender, DataRowChangeEventArgs e)
        {
            if (e.Action == DataRowAction.Change)
            {
                adptr.Update(dataSet1.Tables[0]);
            }
        }




//Alter "btn_Update_Click" Event

//Delete Line
//adptr.Update(dataSet1.Tables[0]);

//Add Line after -- adptr.UpdateCommand = new SqlCommandBuilder(adptr).GetUpdateCommand();
dataSet1.Tables[0].AcceptChanges();




这篇关于TextBox绑定:文本框中的更改值未在DataBase中更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:02