Search code examples
c#asp.netsqldatasource

DetailView update is not working


I have googled every where but without a clear answer I am trying to update a record using Detail view using the following code:

        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AllowSorting="True" AutoGenerateColumns="False" 
        BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" 
        CellPadding="2" DataKeyNames="Rec_ID" DataSourceID="ContactsMasterDS" 
        ForeColor="Black" GridLines="None" PageSize="3">
        <AlternatingRowStyle BackColor="PaleGoldenrod" />
        <Columns>
            <asp:CommandField ShowSelectButton="True" />
            <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
            <asp:BoundField DataField="Full_Name" HeaderText="Full_Name" SortExpression="Full_Name" />
            <asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
            <asp:BoundField DataField="AgeGroup" HeaderText="AgeGroup" SortExpression="AgeGroup" />
            <asp:BoundField DataField="Nationality" HeaderText="Nationality" SortExpression="Nationality" />
            <asp:BoundField DataField="Occupation" HeaderText="Occupation" SortExpression="Occupation" />
            <asp:BoundField DataField="Resident" HeaderText="Resident" SortExpression="Resident" />
        </Columns>
        <FooterStyle BackColor="Tan" />
        <HeaderStyle BackColor="Tan" Font-Bold="True" />
        <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
        <SortedAscendingCellStyle BackColor="#FAFAE7" />
        <SortedAscendingHeaderStyle BackColor="#DAC09E" />
        <SortedDescendingCellStyle BackColor="#E1DB9C" />
        <SortedDescendingHeaderStyle BackColor="#C2A47B" />
    </asp:GridView>
    <asp:SqlDataSource ID="ContactsMasterDS" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
        SelectCommand="Select * from Contacts"></asp:SqlDataSource>
    <br />
    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
        DataKeyNames="Rec_ID" DataSourceID="ContactsDetailsDS" Height="50px" 
        Width="545px" onitemupdated="DetailsView1_ItemUpdated">
        <Fields>
            <asp:BoundField DataField="Rec_ID" HeaderText="Rec_ID" ReadOnly="True" SortExpression="Rec_ID" />
            <asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
            <asp:BoundField DataField="AgeGroup" HeaderText="AgeGroup" SortExpression="AgeGroup" />
            <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
            <asp:BoundField DataField="Full_Name" HeaderText="Full_Name" SortExpression="Full_Name" />
            <asp:BoundField DataField="DOB" HeaderText="DOB" SortExpression="DOB" />
            <asp:BoundField DataField="Phone_No" HeaderText="Phone_No" SortExpression="Phone_No" />
            <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
            <asp:BoundField DataField="Nationality" HeaderText="Nationality" SortExpression="Nationality" />
            <asp:BoundField DataField="Account_No" HeaderText="Account_No" SortExpression="Account_No" />
            <asp:BoundField DataField="Occupation" HeaderText="Occupation" SortExpression="Occupation" />
            <asp:BoundField DataField="Resident" HeaderText="Resident" SortExpression="Resident" />
            <asp:BoundField DataField="Room_No" HeaderText="Room_No" SortExpression="Room_No" />
            <asp:BoundField DataField="Last_Branch" HeaderText="Last_Branch" SortExpression="Last_Branch" />
            <asp:BoundField DataField="Last_Date" HeaderText="Last_Date" SortExpression="Last_Date" />
            <asp:BoundField DataField="Last_Time" HeaderText="Last_Time" SortExpression="Last_Time" />
            <asp:CheckBoxField DataField="isComplete" HeaderText="isComplete" SortExpression="isComplete" />
            <asp:CommandField ShowEditButton="True" />
        </Fields>
    </asp:DetailsView>
    <asp:SqlDataSource ID="ContactsDetailsDS" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
        SelectCommand="Select * from Contacts where [Rec_ID] = @Rec_ID" 
        UpdateCommand="Update Contacts Set Room_No=@Room_No Where Rec_ID=@Rec_ID" >
        <SelectParameters>
            <asp:ControlParameter ControlID="GridView1" Name="Rec_ID" PropertyName="SelectedValue" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="Room_No" Type="String" />
            <asp:Parameter Name="Rec_ID" Type="String" DefaultValue="0" />
        </UpdateParameters>
    </asp:SqlDataSource>

when I change the where clause to be "Where Rec_ID = 2" it works, but when using parameter it does not do the required update any help please


Solution

  • I found the solution as follows:

    1- in the where clause: Change the Rec_ID into any other name like ID for example.

    before: UpdateCommand="Update Contacts Set Room_No=@Room_No Where [Rec_ID]=@Rec_ID">

    after : UpdateCommand="Update Contacts Set Room_No=@Room_No Where [Rec_ID]=@ID">

    2- in the UpdateParameters get the value of ID as such:

    before: <asp:Parameter Name="Rec_ID" Type="Int32" />

    after: <asp:ControlParameter ControlID="DetailsView1" Name="ID" PropertyName="SelectedValue" />

    this will allow you to keep the Rec_ID as read only or even remove hide it from the DetailsView

    Thanks for all of you