tags:

views:

184

answers:

2

I have a simple gridview (well it is now) it populates, I can edit it. but when it comes to updating I just cant get it to work.

The following code creates a gridview which searches for users whose name starts with a "c" (Thats part of my filtering I stripped out)

The problem is when I click the update button it won't update. The Stored procedure gets called but all parameters passed to it are null. therefore the database is not updated.

I know its a simple problem with a simple solution but I just can't see it.

I tried using ExtractValuesFromCell within the onupdating event and still only got nulls. I had to add the CausesValidation="false" because without it the update events were not even called.

Any and all help is appreciated

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AutoGenerateColumns="False" DataSourceID="SqlDataSource3"
    DataKeyNames="UserId">
    <Columns>
        <asp:CommandField ShowEditButton="True" CausesValidation="false" />
        <asp:BoundField DataField="UserName" HeaderText="UserName" 
            SortExpression="UserName" />
        <asp:BoundField DataField="MobileAlias" HeaderText="MobileAlias" 
            SortExpression="MobileAlias" />
        <asp:BoundField DataField="DistrictId" HeaderText="DistrictId" 
            SortExpression="DistrictId" />
        <asp:BoundField ReadOnly="true" DataField="UserId" HeaderText="UserId" 
            SortExpression="UserId" />
    </Columns>
</asp:GridView>    <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
    ConnectionString="<%$ ConnectionStrings:REMConnectionString_development_dev %>" 
    SelectCommand="LoadUser" SelectCommandType="StoredProcedure" 
    UpdateCommand="UpdateUser" UpdateCommandType="StoredProcedure" 
    onupdating="SqlDataSource3_Updating">
    <SelectParameters>
        <asp:Parameter DefaultValue="c" Name="UserName" Type="String" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="UserId" Type="String" />
        <asp:Parameter Name="DistrictID" Type="Int32" />
        <asp:Parameter Name="UserName" Type="String" />
        <asp:Parameter Name="MobileAlias" Type="String" />
    </UpdateParameters>
</asp:SqlDataSource>

the only code in the codebehind is

protected void SqlDataSource3_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
}

It is there so I can drop in a breakpoint and inspect the parameters, which are all there but with null values

A: 

Something else has to be at work here. I replicated the code you provided and when I break down the event arguments I see the values and it all works correctly. Could you edit your post and include all the code?

Justin C
You are correct, I found the cause of the problem but it raises another problem. The cause of the problem was this line in the page_load method on the master page 'ID = "REM";' This was to give the request entries a better name than ctl00 eg: ctl00$DistrictSelectorContent$District changes to REM$DistrictSelectorContent$District in the Request object. Not much of an improvement, but to my mind less clumsy than the default. Why it broke the update function I have no idea. Obviously something to do with the viewstate. The question now is how do I implement an alternative to 'ID="REM"'?
DeveloperChris
A: 

Ok I have had a chance to do some more research and confirmed the issue.

Setting the Master Page ID in the Page_load function is the issue. It needs to be set in the Page_Init.

This page provides a good explanation
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=469

So by moving ID= "REM"; from page_load to Page_init the problem magically went away.

Unfortunately it is common to advise setting the ID in Page_Load which is what I relied on and turned out to be bad advice.

DC

DeveloperChris