数据源'SqlDataSource1'不支持删除,除非指定了DeleteCommand [英] Deleting is not supported by data source 'SqlDataSource1' unless DeleteCommand is specified
本文介绍了数据源'SqlDataSource1'不支持删除,除非指定了DeleteCommand的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 GridView1
中有一个ASP按钮链接,它应该在点击时从数据库表中删除一个项目,并且收到此错误消息:
除非指定了DeleteCommand,否则数据源'SqlDataSource1'不支持删除。
这是代码:
<保护子GridView1_RowCommand(sender As Object,e As GridViewCommandEventArgs)处理GridView1.RowCommand
受保护的子GridView1_RowCommand b
$ b如果e.CommandName.Equals(Delete)然后
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。打开()
cmd.ExecuteNonQuery()
conn.Close()
End If
End Sub
ASP
< asp:GridView ID =GridView1runat =serverAu toGenerateColumns =FalseCellPadding =4DataSourceID =SqlDataSource1ForeColor =#333333GridLines =NoneViewStateMode =Enabled>
< AlternatingRowStyle BackColor =WhiteForeColor =#284775/>
<列>
< asp:BoundField DataField =content_nameHeaderText =Content NameSortExpression =content_name>
< / asp:BoundField>
< asp:BoundField DataField =content_typeHeaderText =Content TypeSortExpression =content_type>
< / asp:BoundField>
< asp:buttonfield buttontype =Linkcommandname =Deletetext =Delete>
< ItemStyle HorizontalAlign =CenterVerticalAlign =Middle/>
< / asp:buttonfield>
< asp:TemplateField HeaderText =>
< ItemTemplate>
< asp:LinkButton ID =lnkDownloadrunat =serverText =DownloadOnClick =lnkDownload_Click>< / asp:LinkButton>
< / ItemTemplate>
< / asp:TemplateField>
< /列>
< EditRowStyle BackColor =#999999/>
< FooterStyle BackColor =#5D7B9DFont-Bold =TrueForeColor =White/>
< HeaderStyle BackColor =#5D7B9DFont-Bold =TrueForeColor =White/>
< RowStyle BackColor =#F7F6F3ForeColor =#333333/>
< SelectedRowStyle BackColor =#E2DED6Font-Bold =TrueForeColor =#333333/>
< SortedAscendingCellStyle BackColor =#E9E7E2/>
< SortedAscendingHeaderStyle BackColor =#506C8C/>
< SortedDescendingCellStyle BackColor =#FFFDF8/>
< SortedDescendingHeaderStyle BackColor =#6F8DAE/>
< / asp:GridView>
从我读到的内容中,您需要为 SQLDataSource1指定一个DELETE命令
。
在我的情况下,我该如何做?
解决方案您必须输入以下代码:
< asp:ButtonColumn CommandName =DeleteText = 删除 >< / ASP:ButtonColumn>
真诚。
I have an ASP button link in GridView1
that should delete an item from a db table on click, and I am getting this error message:
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: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
.
How do I do that in my case?
解决方案
You have to put in the asp code :
<asp:ButtonColumn CommandName="Delete" Text="Delete"></asp:ButtonColumn>
sincerely.
这篇关于数据源'SqlDataSource1'不支持删除,除非指定了DeleteCommand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文