views:

1337

answers:

1

Hi all, I am binding a detailsview with objectdatasource which gets the select parameter from the querystring. The detailsview shows the desired record, but when I try to update it, my update method gets the old values for the record (and hence no update). here is my detailsview code:

<asp:DetailsView ID="dvUsers" runat="server" Height="50px" Width="125px" 
        AutoGenerateRows="False" DataSourceID="odsUserDetails" 
        onitemupdating="dvUsers_ItemUpdating">
        <Fields>
               <asp:CommandField ShowEditButton="True" />
            <asp:BoundField DataField="Username" HeaderText="Username" SortExpression="Username"
                ReadOnly="true" />
            <asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" />
            <asp:BoundField DataField="Email" runat="server" HeaderText="Email" SortExpression="Email" />
            <asp:BoundField DataField="IsActive" HeaderText="Is Active" SortExpression="IsActive" />
            <asp:BoundField DataField="IsOnline" HeaderText="Is Online" SortExpression="IsOnline"
                ReadOnly="true" />
            <asp:BoundField DataField="LastLoginDate" HeaderText="Last Login" SortExpression="LastLoginDate"
                ReadOnly="true" />
            <asp:BoundField DataField="CreateDate" HeaderText="Member Since" SortExpression="CreateDate"
                ReadOnly="true" />
            <asp:TemplateField HeaderText="Membership Ends" SortExpression="ExpiryDate">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ExpiryDate") %>'></asp:TextBox>
                    <cc1:CalendarExtender ID="TextBox1_CalendarExtender" runat="server" Enabled="True"
                        TargetControlID="TextBox1">
                    </cc1:CalendarExtender>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ExpiryDate") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("ExpiryDate") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Fields>

and here is the objectdatasource code:

    <asp:ObjectDataSource ID="odsUserDetails" runat="server" SelectMethod="GetAllUserDetailsByUserId"
        TypeName="QMS_BLL.Membership" UpdateMethod="UpdateUserForClient">
        <UpdateParameters>
            <asp:Parameter Name="User_ID" Type="Int32" />
            <asp:Parameter Name="firstName" Type="String" />
            <asp:Parameter Name="lastName" Type="String" />
            <asp:SessionParameter Name="updatedByUser" SessionField="userId" DefaultValue="1" />
            <asp:Parameter Name="expiryDate" Type="DateTime" />
            <asp:Parameter Name="Email" Type="String" />
            <asp:Parameter Name="isActive" Type="String" />
        </UpdateParameters>
        <SelectParameters>
            <asp:QueryStringParameter DefaultValue="1" Name="User_ID" QueryStringField="User_ID"
                Type="Int32" />
        </SelectParameters>
    </asp:ObjectDataSource>

Is the OnItemUpdating method still required when you have your custom BLL method called on insertevent? (which is being executed fine in my case but updating with the old values) or am I missing something else?

Also I tried to provide an OnItemUpdating method and in there I tried to capture the contents of the textboxes (the new values). I got an exception: "Specified argument was out of the range of valid values. Parameter name: index"

when I tried to do:

TextBox txtFirstName = (TextBox)dvUsers.Rows[1].Cells[1].Controls[0];

Any help will be most appreciated.

A: 

Check if Newvalues available in the ItemUpdating using....

protected void dtvVendor_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
    //TextBox txtFirstName = (TextBox)DetailsView1.FindControl("txtFirstName");
    //e.NewValues["firstName"] = txtFirstName.Text;
    String firstName = e.NewValues["firstName"];// check if here ur new firstName is available
}

Update: Missing property in your DetailsView

DataKeyNames="User_ID"

Muhammad Akhtar
I cannot use FinControl ("") as I dont know the name of the control.Any other ways of doing the same thing?
Ali
show ur code behind
Muhammad Akhtar
I have update my ansser, check now
Muhammad Akhtar
Well at the moment, I am only changing the mode of the detailsview to edit on page load. There is no OnItemUpdating method. As asked in original post, do I need to specify OnItemUpdating ?This is in my page load method:if (!Page.IsPostBack) { dvUsers.ChangeMode(DetailsViewMode.Edit); dvUsers.DataBind(); }
Ali
don't need to call this method dvUsers.DataBind(); in code behind
Muhammad Akhtar
I get the null reference exception ("Object reference not set to an instance of an object.") When attempting to check:string firstName = e.NewValues["firstName"].ToString();
Ali
no you don't need to specify OnItemUpdating
Muhammad Akhtar
you can convert bound field to templated field and then will get in onupdating.
Muhammad Akhtar