但是当我删除SqlDataSource时,一切工作正常,使用相同的存储过程的更新命令。
请帮我解决这个问题,我在这里严重困扰
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="Title" EnableModelValidation="True"
ForeColor="#333333" GridLines="None"
ShowFooter="True" Width="950" AllowPaging="true" PageSize="10">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<%# Eval("Title") %>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="txtEditTitle" runat="server" Text='<%# Bind("Title") %>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quote">
<ItemTemplate>
<%# Eval("Quote") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditQuote" runat="server" Text='<%# Bind("Quote") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField >
<asp:TemplateField HeaderText="Quote Link">
<ItemTemplate>
<%# Eval("QuoteLink") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditQuoteLink" runat="server" Text='<%# Bind("QuoteLink") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Published">
<ItemTemplate>
<asp:CheckBox ID="chkBox" runat="server" Checked='<%# Convert.ToBoolean(Eval("Published")) %>' Enabled = "false" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="chkEditBox" runat="server" Checked='<%# Bind("Published") %>' Enabled="true" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PublishedDate">
<ItemTemplate>
<%# Convert.ToDateTime(DataBinder.Eval(Container.DataItem,"PublishedDate")).ToString("MM.dd.yyyy") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditPublishedDate" runat="server" Text='<%# Bind("PublishedDate") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Commands">
<ItemTemplate>
<asp:ImageButton runat="server" ID="Edit" ImageUrl="/images/edit.gif" CommandName="Edit" />
<asp:ImageButton runat="server" ID="Delete" ImageUrl="/images/delete.gif" CommandName="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button runat="server" ID="btn1" CommandName="Update" Text="Update" />
<asp:ImageButton runat="server" ID="Cancel" ImageUrl="/images/delete.gif" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" HorizontalAlign="Left" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SiteSqlServer %>"
DeleteCommand="DELETE FROM [Quotes] WHERE [Title] = @Title"
SelectCommand= "SELECT [Title], [Quote], [QuoteLink], [Published], [PublishedDate] FROM [Quotes]"
UpdateCommand="sp_UpdateQuotes" UpdateCommandType="StoredProcedure"
InsertCommand="INSERT INTO [Quotes] ([Title], [Quote], [QuoteLink], [Published], [PublishedDate]) VALUES (@Title, @Quote, @QuoteLink, @Published, @PublishedDate)">
<DeleteParameters>
<asp:Parameter Name="Title" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Quote" Type="String" />
<asp:Parameter Name="QuoteLink" Type="String" />
<asp:Parameter Name="Published" Type="Boolean" />
<asp:Parameter Name="PublishedDate" Type="DateTime" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Quote" Type="String" DefaultValue = "QUotes are simple" />
<asp:Parameter Name="QuoteLink" Type="String" DefaultValue = "QUotes are Linked" />
<asp:Parameter Name="Published" Type="Boolean" DefaultValue = "False" />
<asp:Parameter Name="PublishedDate" Type="DateTime" DefaultValue = "05/15/2011" />
</UpdateParameters>
</asp:SqlDataSource>
ALTER PROCEDURE [dbo].[sp_UpdateQuotes]
@Title varchar(max),
@Quote varchar(max),
@QuoteLink varchar(max),
@Published bit,
@PublishedDate DateTime
AS
BEGIN
UPDATE dbo.Quotes SET
Quote = @Quote,
QuoteLink = @QuoteLink,
Published = @Published,
PublishedDate = @PublishedDate
WHERE Title = @Title
If @Published = 1
BEGIN
UPDATE dbo.Quotes SET Published = 0 WHERE Title <> @Title AND Published = 1
END
WHERE Title = @Title
If @Published = 1
BEGIN
UPDATE dbo.Quotes SET Published = 0 WHERE Title <> @Title AND Published = 1
END
希望这能让你清楚地知道什么是让我感到困扰的更新
答案 0 :(得分:1)
您尚未传递@Title parameter
,这是您存储过程中的主要密钥,并在where Clause
中使用。
如果要查看更新参数中没有标题参数:
<UpdateParameters>
<asp:Parameter Name="Quote" Type="String" />
<asp:Parameter Name="QuoteLink" Type="String" />
<asp:Parameter Name="Published" Type="Boolean" />
<asp:Parameter Name="PublishedDate" Type="DateTime" />
</UpdateParameters>
如果添加标题参数,它将起作用:
<asp:Parameter Name="Title" Type="String" />
答案 1 :(得分:0)
我还遇到了一个没有更新的GridView的问题。在GridView的RowUpdating事件中检查e.Newvalues Dictionary显示记录的旧值被发送到数据库UPDATE查询。 DataKeyNames不是我的问题;我把它设置正确。我的SELECT查询的WHERE子句引用了一个针对我的表单上的TextBox的控件参数。我无意中将此文本框的EnableViewState设置为“False”。因此,GridView在UPDATE发生之前重新绑定。将TextBox上的EnableViewState设置为“True”可以解决问题。
Protected Sub MyGridView_RowUpdating _
(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) _
Handles MyGridView.RowUpdating
' Inspect the parameters being sent to the database for an ASP NET GridView UPDATE.
Dim I As Integer
I = 0
For Each MVO As System.Collections.DictionaryEntry In e.OldValues
If MVO.Value Is DBNull.Value OrElse MVO.Value Is Nothing Then
Debug.Print(I.ToString + ": " + MVO.Key + " Value: ")
Else
Debug.Print(I.ToString + ": " + MVO.Key + " Value: " + MVO.Value.ToString)
End If
I += 1
Next MVO
I = 0
For Each MVN As System.Collections.DictionaryEntry In e.NewValues
If MVN.Value Is DBNull.Value OrElse MVN.Value Is Nothing Then
Debug.Print(I.ToString + ": " + MVN.Key + " Value: ")
Else
Debug.Print(I.ToString + ": " + MVN.Key + " Value: " + MVN.Value.ToString)
End If
I += 1
Next MVN
End Sub