vb.net 除非指定了 DeleteCommand,否则数据源“SqlDataSource1”不支持删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15948897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deleting is not supported by data source 'SqlDataSource1' unless DeleteCommand is specified
提问by Brian
I have an ASP button link in GridView1that should delete an item from a db table on click, and I am getting this error message:
我有一个 ASP 按钮链接,GridView1点击后应该从数据库表中删除一个项目,我收到以下错误消息:
Deleting is not supported by data source 'SqlDataSource1' unless DeleteCommand is specified.
This is the code:
这是代码:
VB.NET
网络
Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName.Equals("Delete") Then
Dim rowIndex As Integer = Convert.ToInt32(e.CommandArgument)
Dim rowID As String = e.CommandArgument.ToString()
Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True;")
Dim cmd As New SqlCommand("DELETE content WHERE content_id=@rowID", conn)
cmd.Parameters.AddWithValue("@rowID", rowID)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End If
End Sub
ASP
ASP
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" ViewStateMode="Enabled">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="content_name" HeaderText="Content Name" SortExpression="content_name">
</asp:BoundField>
<asp:BoundField DataField="content_type" HeaderText="Content Type" SortExpression="content_type">
</asp:BoundField>
<asp:buttonfield buttontype="Link" commandname="Delete" text="Delete">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:buttonfield>
<asp:TemplateField HeaderText=" ">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="lnkDownload_Click" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
From what I read you need to specify a DELETE command for SQLDataSource1.
根据我的阅读,您需要为SQLDataSource1.
How do I do that in my case?
在我的情况下我该怎么做?
采纳答案by Minus
You have to put in the asp code :
你必须输入asp代码:
<asp:ButtonColumn CommandName="Delete" Text="Delete"></asp:ButtonColumn>
sincerely.
真挚地。
回答by teardrop
if you change it to something like... CommandName="DeleteRow"or something.. and then change it to if(e.CommandName == "DeleteRow")you will bypass that problem.
如果您将其更改为类似……CommandName="DeleteRow"或其他内容……然后将其更改为if(e.CommandName == "DeleteRow")您将绕过该问题。
回答by pStan
Just a quick answer on how I did this.
只是快速回答我是如何做到这一点的。
If you choose "ShowDeleteButton" under the command field properties of the Select field "Delete" will show on the left of each row on the gridview. However, when you do that there is an expectation that the sql Data Source will have a a delete command. If not, you get the nasty message about the delete command not being specified.
如果在选择字段的命令字段属性下选择“ShowDeleteButton”,则“删除”将显示在网格视图的每一行的左侧。但是,当您这样做时,预计 sql 数据源将具有删除命令。如果没有,您会收到有关未指定删除命令的令人讨厌的消息。
Since I wanted the BusinessObject to handle the delete rather than the sql data source, I instead used the select, just like you would to select a row, then I changed the "SelectText" under command field properties to say "Delete". That allowed me to use the RowCommand event to catch the delete action. From there I just grabbed the row info I needed, and passed it to the business object for the delete.
由于我希望 BusinessObject 处理删除而不是 sql 数据源,因此我改为使用选择,就像您选择一行一样,然后我将命令字段属性下的“SelectText”更改为“删除”。这允许我使用 RowCommand 事件来捕获删除操作。从那里我刚刚获取了我需要的行信息,并将其传递给业务对象以进行删除。
Note that when you do it that way the CommandName is still "Select" even though the commandtext is "Delete". So you check as follows:
请注意,当您这样做时,即使命令文本是“删除”,CommandName 仍然是“选择”。所以你检查如下:
if (e.CommandName.ToString() == "Select")
Hope that helps someone. Good luck!
希望能帮助某人。祝你好运!
回答by omar nazzal
you need to change it to something Else Like
你需要把它改成别的东西
<asp:buttonfield buttontype="Link" commandname="Del" text="Delete">
and then change it to (e.CommandName == "Del")because "DELETE" Reserved to SQlDataSource Control
然后将其更改为(e.CommandName == "Del")因为“DELETE”保留给 SQlDataSource 控件
回答by ganesh parve
This error occurs because of you write command name delete. so you write something like Delete Record. and when you use SQLDataSource that time command should be not same as delete or update. Please use alternative name.
发生此错误是因为您写入命令名称删除。所以你写了一些类似删除记录的东西。并且当您使用 SQLDataSource 时,该时间命令不应与删除或更新相同。请使用备用名称。

