gpt4 book ai didi

c# - 数据源不支持更新,除非指定了 UpdateCommand

转载 作者:行者123 更新时间:2023-11-30 20:08:40 25 4
gpt4 key购买 nike

我想有条件地调用 sqldatasource 上的更新语句。我有一个分配给它的 gridview 和 sqldatasource。我处理 gridview_updating 事件。如果我不为其分配 sqldatasource.updatecommand(当某些条件不满足时),我会收到错误消息:“除非指定了 UpdateCommand,否则数据源不支持更新。”

如何抑制此错误消息?

提前致谢:)

编辑:

这是我的gridview命令事件:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{


if (e.CommandName == "Update")
{
if(someconditionmatch)
sqldatasource1.UpdateCommand = "some update query";
else
{
//do nothing...but when do do nothing...it throws error that some update command has to compulsorily assigned...This is my problem...which updateCommand should I assign if I do not want to update anything???
}
}
}

最佳答案

您可以在 RowUpdating 事件处理程序中尝试它,而不是在 RowCommand 事件处理程序中处理它,因为您可以将 Cancel 属性设置为 true:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
if(someconditionmatch)
sqldatasource1.UpdateCommand = "some update query";
else
e.Cancel = true; //Cancels the impending update.
}

我不知道您是否必须在 RowCommand 事件处理程序中使用具有某个值的 UpdateCommand - 如果您这样做,您始终可以将 UpdateCommand 设置为您用于条件的那个(假设您只有一个更新命令) 在 RowCommand 事件处理程序(或 SqlDataSource 的标记)中,然后像这样执行 RowUpdating:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
if (!someconditionmatch)
e.Cancel = true;
}

希望以上内容对您有所帮助,或者至少让您指明了正确的方向。

关于c# - 数据源不支持更新,除非指定了 UpdateCommand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6854722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com