开发者

Update is not working on GridView

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>
            <EditItemTemp开发者_开发技巧late>
                <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 the where Clause.

If you want to see that there is no Title Parameter in Update Parameters:

<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>

If you add the Title Parameter, it will work:

    <asp:Parameter Name="Title" Type="String" />


I also had a problem with a GridView that was not updating. Inspection of the e.Newvalues Dictionary in the GridView's RowUpdating event showed that the old values for the record were being sent to the database UPDATE query. DataKeyNames was not my problem; I had it set correctly. The WHERE clause of my SELECT query referenced a control parameter against a TextBox on my form. I had inadvertently set EnableViewState for this textbox to "False". Because of this, the GridView was rebinding itself before the UPDATE occurred. Setting EnableViewState on the TextBox to "True" fixed the problem.

    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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜