Error : Update requires a valid UpdateCommand when passed DataRow collection with modified rows(错误:当传递带有修改行的 DataRow 集合时,更新需要有效的 UpdateCommand)
问题描述
我正在使用 Paging 在 datagridview
中显示数据,但是当我尝试使用 updatebutton
更新任何数据时,应在 datagridview
中更新数据以及在数据库中.
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.
但我收到此错误:
在传递 DataRow 集合时,更新需要有效的 UpdateCommand修改后的行
Update requires a valid UpdateCommand when passed DataRow collection with modified rows
发生在这一行:
adp1.Update(dt);//here I am getting error
下面是代码
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());
}
}
}
推荐答案
您只使用 Select
命令创建了 OleDbDataAdapter
:
You have Created the OleDbDataAdapter
with a Select
command only:
adp1 = new OleDbDataAdapter(cmd1);
OleDbDataAdapter
需要使用有效的 Update
、Insert、
Delete
命令像这样保存数据:
OleDbDataAdapter
requires valid Update
, Insert,
Delete
commands to be used to save the data like this:
adp1.Update(dt);//here I am getting error
您只需要使用 OleDbCommandBuilder
即可为您生成命令:
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);
编辑
由于您在运行时更改了 OleDbDataAdapter
的 Select 命令进行分页,因此您需要在每次保存数据时进行初始化:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:错误:当传递带有修改行的 DataRow 集合时,更新需要有效的 UpdateCommand
基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01