Dur*_*rga 4 .net c# ado.net dataadapter
我正在使用Paging来显示数据datagridview,但是当我尝试使用updatebutton数据更新任何数据时,应该更新在datagridview数据库中以及数据库中.
但我得到这个错误:
在传递带有已修改行的DataRow集合时,Update需要有效的UpdateCommand
发生在这一行:
adp1.Update(dt);//here I am getting error
Run Code Online (Sandbox Code Playgroud)
下面是代码
public partial class EditMediClgList : Form
{
public EditMediClgList()
{
InitializeComponent();
try
{
con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb");
con.Open();
}
catch (Exception err)
{
MessageBox.Show("Error:" +err);
}
cmd1 = new OleDbCommand("Select * from MedicalColeges order by MedicalClgID", con);
ds = new DataSet();
adp1 = new OleDbDataAdapter(cmd1);
adp1.Fill(ds, "MedicalColeges");
dataGridView1.DataSource = ds;
// Get total count of the pages;
this.CalculateTotalPages();
// Load the first page of data;
this.dataGridView1.DataSource = GetCurrentRecords(1, con);
}
private void CalculateTotalPages()
{
int rowCount = ds.Tables["MedicalColeges"].Rows.Count;
this.TotalPage = rowCount / PageSize;
if (rowCount % PageSize > 0) // if remainder is more than zero
{
this.TotalPage += 1;
}
}
private DataTable GetCurrentRecords(int page, OleDbConnection con)
{
dt = new DataTable();
if (page == 1)
{
cmd2 = new OleDbCommand("Select TOP " + PageSize + " * from MedicalColeges ORDER BY MedicalClgID", con);
// CurrentPageIndex++;
}
else
{
int PreviouspageLimit = (page - 1) * PageSize;
cmd2 = new OleDbCommand("Select TOP " + PageSize +
" * from MedicalColeges " +
"WHERE MedicalClgID NOT IN " +
"(Select TOP " + PreviouspageLimit + " MedicalClgID from MedicalColeges ORDER BY MedicalClgID) ", con); // +
//"order by customerid", con);
}
try
{
// con.Open();
this.adp1.SelectCommand = cmd2;
this.adp1.Fill(dt);
txtPaging.Text = string.Format("page{0} of {1} pages", this.CurrentPageIndex, this.TotalPage);
}
finally
{
// con.Close();
}
return dt;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
adp1.Update(dt);//here I am getting error
}
catch (Exception err)
{
MessageBox.Show(err.Message.ToString());
}
}
}
Run Code Online (Sandbox Code Playgroud)
您仅OleDbDataAdapter使用Select命令创建了:
adp1 = new OleDbDataAdapter(cmd1);
Run Code Online (Sandbox Code Playgroud)
OleDbDataAdapter需要有效的Update,Insert, Delete命令被用来保存这样的数据:
adp1.Update(dt);//here I am getting error
Run Code Online (Sandbox Code Playgroud)
您只需要使用一个OleDbCommandBuilder将为您生成命令:
adp1 = new OleDbDataAdapter();
adp1.SelectCommand = cmd1; // cmd1 is your SELECT command
OleDbCommandBuilder cb = new OleDbCommandBuilder(adp1);
Run Code Online (Sandbox Code Playgroud)
编辑
由于您更改了OleDbDataAdapter运行时的Select命令以进行分页,因此每次保存数据时都需要初始化:
private void button1_Click(object sender, EventArgs e)
{
try
{
adp1.SelectCommand = cmd1; // cmd1 is your SELECT command
OleDbCommandBuilder cb = new OleDbCommandBuilder(adp1);
adp1.Update(dt); //here I hope you won't get error :-)
}
catch (Exception err)
{
MessageBox.Show(err.Message.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16477 次 |
| 最近记录: |