datagrid更新命令不起作用?我错过了什么吗?
我正在使用 Datagrid 和 SQLDataSource 来绑定一张表中的数据。为了启用更新,我编写了我错误的更新查询。
update mytable field1=@field1, field2=@field2 where ID=@ID
当我单击编辑时,我进入编辑模式。当我提交更改时,更改未提交,并且我没有收到任何错误。我需要在其他地方改变一些东西吗?我没有修改此示例中的任何 C# 代码。
编辑: 如果我省略 where ID=@ID,则整个列已成功更改,但它不适用于 ID=@ID。这里有什么问题吗?
编辑:代码
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT [my_id], [ID], [FirstName], [LastName], [ApplicationUserName] FROM [member_tbl]"
UpdateCommand="UPDATE [member_tbl] SET [my_id] = @my_id, [FirstName] = @FirstName, [LastName] = @LastName, [ApplicationUserName] = @ApplicationUserName where ID = @ID">
<UpdateParameters>
<asp:Parameter Name="my_id" Type="Int32"/>
<asp:Parameter Name="FirstName" />
<asp:Parameter Name="LastName" />
<asp:Parameter Name="ApplicationUserName" />
<asp:Parameter Name="id" />
</UpdateParameters>
</asp:SqlDataSource>
I am using Datagrid and SQLDataSource to bind data from just one table. In order to enable update, I have write update query which I wrong like this.
update mytable field1=@field1, field2=@field2 where ID=@ID
When I click edit, i get into edit mode. When I submit a change, the change is not submitted and I get no error. Do I need to change something somewhere else? I did not modify any C# code in this example.
Edit:
If I omit where ID=@ID, then the whole column is successfully changed but it does not work with ID=@ID. What is wrong here?
Edit: Code
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT [my_id], [ID], [FirstName], [LastName], [ApplicationUserName] FROM [member_tbl]"
UpdateCommand="UPDATE [member_tbl] SET [my_id] = @my_id, [FirstName] = @FirstName, [LastName] = @LastName, [ApplicationUserName] = @ApplicationUserName where ID = @ID">
<UpdateParameters>
<asp:Parameter Name="my_id" Type="Int32"/>
<asp:Parameter Name="FirstName" />
<asp:Parameter Name="LastName" />
<asp:Parameter Name="ApplicationUserName" />
<asp:Parameter Name="id" />
</UpdateParameters>
</asp:SqlDataSource>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回答我自己:我发现为了让它工作,ID 列必须是主键(或者可能是唯一列),否则它将不起作用并且不会给你任何错误,这是有点奇怪。
Answering myself: I found that in order for this to work, the ID column must be the primary key (or may be unique column), otherwise it will not work and wont give you any error, which is a bit strange.