错误:更新要求当传递的DataRow集合与已修改行有效的UpdateCommand [英] Error : Update requires a valid UpdateCommand when passed DataRow collection with modified rows
问题描述
我使用分页显示在 datagridview的
,但是当我尝试更新与 updatebutton
的任何数据数据数据应该更新,在 datagridview的
,以及在数据库中。
不过,我得到这个错误:
更新要求当传递的DataRow集合有效的UpdateCommand 与修改的行
这恰好在这条线:
adp1.Update(DT); //这里我得到错误
下面是code
公共部分类EditMediClgList:表
{
公共EditMediClgList()
{
的InitializeComponent();
尝试
{
CON =新的OleDbConnection(@供应商= Microsoft.Jet.OLEDB.4.0;数据源= DB1.MDB);
con.Open();
}
赶上(例外错误)
{
的MessageBox.show(错误:+ ERR);
}
CMD1 =新的OleDbCommand(SELECT * FROM MedicalColeges为了通过MedicalClgID,CON);
DS =新的DataSet();
ADP1 =新OleDbDataAdapter的(CMD1);
adp1.Fill(DS,MedicalColeges);
dataGridView1.DataSource = DS;
//获取页面的总数;
this.CalculateTotalPages();
//加载数据的第一页;
this.dataGridView1.DataSource = GetCurrentRecords(1,CON);
}
私人无效CalculateTotalPages()
{
INT rowCount等= ds.Tables [MedicalColeges] Rows.Count。
this.TotalPage = rowCount时/每页;
如果(rowCount等%每页大于0)//如果余数是大于零
{
this.TotalPage + = 1;
}
}
私人数据表GetCurrentRecords(INT页,OleDbConnection的CON)
{
DT =新的DataTable();
如果(页面== 1)
{
CMD2 =新的OleDbCommand(选择TOP+每页+*从MedicalColeges ORDER BY MedicalClgID,CON);
// CurrentPageIndex ++;
}
其他
{
INT previouspageLimit =(页 - 1)*每页;
CMD2 =新的OleDbCommand(选择TOP+每页+
从MedicalColeges *+
WHERE MedicalClgID NOT IN+
(选择TOP+ previouspageLimit +MedicalClgID从MedicalColeges ORDER BY MedicalClgID),CON); // +
//以便通过客户ID,CON);
}
尝试
{
// con.Open();
this.adp1.SelectCommand = CMD2;
this.adp1.Fill(DT);
txtPaging.Text =的String.Format(第{0} {1}页,this.CurrentPageIndex,this.TotalPage);
}
最后
{
// con.Close();
}
返回DT;
}
私人无效的button1_Click(对象发件人,EventArgs的)
{
尝试
{
adp1.Update(DT); //这里我得到错误
}
赶上(例外错误)
{
的MessageBox.show(err.Message.ToString());
}
}
}
您已经创建了 OleDbDataAdapter的
与选择
唯一的命令:
ADP1 =新OleDbDataAdapter的(CMD1);
OleDbDataAdapter的
需要有效的更新
,插入,
删除
命令可以用来保存数据是这样的:
adp1.Update(DT); //这里我得到错误
您只需要使用 OleDbCommandBuilder
这会为你的命令:
ADP1 =新OleDbDataAdapter的();
adp1.SelectCommand = CMD1; // CMD1是你的SELECT命令
OleDbCommandBuilder CB =新OleDbCommandBuilder(ADP1);
修改
既然你改变的选择命令 OleDbDataAdapter的
在运行时进行分页,你需要的是什么,你保存数据初始化每次:
私人无效的button1_Click(对象发件人,EventArgs的)
{
尝试
{
adp1.SelectCommand = CMD1; // CMD1是你的SELECT命令
OleDbCommandBuilder CB =新OleDbCommandBuilder(ADP1);
adp1.Update(DT); //这里我希望你不会得到错误:-)
}
赶上(例外错误)
{
的MessageBox.show(err.Message.ToString());
}
}
I am using Paging to show data in datagridview
, but when i try to Update any data with updatebutton
data should be updated In datagridview
as well as in database.
But I get this error:
Update requires a valid UpdateCommand when passed DataRow collection with modified rows
which happens on this line:
adp1.Update(dt);//here I am getting error
Below is the code
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());
}
}
}
You have Created the OleDbDataAdapter
with a Select
command only:
adp1 = new OleDbDataAdapter(cmd1);
OleDbDataAdapter
requires valid Update
, Insert,
Delete
commands to be used to save the data like this:
adp1.Update(dt);//here I am getting error
You just need to use a OleDbCommandBuilder
that will generate the commands for you:
adp1 = new OleDbDataAdapter();
adp1.SelectCommand = cmd1; // cmd1 is your SELECT command
OleDbCommandBuilder cb = new OleDbCommandBuilder(adp1);
EDIT
Since you change the Select command of the OleDbDataAdapter
at runtime for paging, what your need is to initialize each time you save data:
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());
}
}
这篇关于错误:更新要求当传递的DataRow集合与已修改行有效的UpdateCommand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!