Hey guys,
I have a GridView which is binded to SqlDataSource, everything works fine, just update does not works, i am calling stored procedure, and all parameters and its dataTypes are proper, actually none of the events work on that update button, i tried to change the CommandName to “Change” and created OnCommand Event, and wrote Response Message in the code. but nothing happens on the update button, cancel button works fine, and also delete works, there is just a problem with update, even if the procedure has some issue atleast event should raised, i even checked in Sql Profiler, but the update procedure never hits Sql Server 2008…
But when i remove SqlDataSource then everything works fine also the update command with same stored procedure.
Please help me out for this, i have stuck here badly
<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
END
Hope this would give you clear idea about what is messing me for update
You have not passed the
@Title parameter, which is your primary Key in your stored procedure and is used in thewhere Clause.If you want to see that there is no Title Parameter in Update Parameters:
If you add the Title Parameter, it will work: