2

I have a FormView with an UpdateButton and a SqlDataSource which are below. My updatebutton always update the Sil table with the same values (the values at the first line of my gridview), I couldn't figure out why.

<asp:FormView ID="frm_Benefit" runat="server" DataKeyNames="ID" DataSourceID="sds_Benefits"
            OnItemInserted="frm_Benefit_ItemInserted" OnItemUpdated="frm_Benefit_ItemUpdated">
    <EditItemTemplate>
        <table class="formview">
            <tr>
                <td>
                    Code:
                </td>
                <td>
                    <asp:TextBox ID="CodeTextBox" runat="server" Text='<%# Bind("Code") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Name:
                </td>
                <td>
                    <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    HRName:
                </td>
                <td>
                    <asp:TextBox ID="HRNameTextBox" runat="server" Text='<%# Bind("HRName") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Description:
                </td>
                <td>
                    <asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>'
                        TextMode="MultiLine" MaxLength="200" />
                </td>
            </tr>
            <tr>
                <td>
                    Associated Url:
                </td>
                <td>
                    <asp:TextBox ID="UrlTextBox" runat="server" Text='<%# Bind("Url") %>' MaxLength="100" />
                </td>
            </tr>
            <tr>
                <td>
                    BenefitType:
                </td>
                <td>
                    <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="sds_BenefitTypes"
                        DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("BenefitTypeID") %>'>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    Cost:
                </td>
                <td>
                    <asp:TextBox ID="CostTextBox" runat="server" Text='<%# Bind("Cost") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Cost Type:
                </td>
                <td>
                    <asp:DropDownList ID="CostTypeDropDown" runat="server" DataSourceID="sds_CostTypes"
                        DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("CostTypeID") %>'>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    Quantity Type:
                </td>
                <td>
                    <asp:DropDownList ID="QuantityTypeDropDown" runat="server" DataSourceID="sds_QuantityTypes"
                        DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("QuantityTypeID") %>'>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    Quantity Entry On Add:
                </td>
                <td>
                    <asp:CheckBox ID="QuantityEntryOnAddCheckBox" runat="server" Checked='<%# Bind("QuantityEntryOnAdd") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Quantity Entry On Remove:
                </td>
                <td>
                    <asp:CheckBox ID="QuantityEntryOnRemoveCheckBox" runat="server" Checked='<%# Bind("QuantityEntryOnRemove") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Bonus Ratio:
                </td>
                <td>
                    <asp:TextBox ID="BonusRatioTextBox" runat="server" Text='<%# Bind("BonusRatio") %>'></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    ApplyIncomeTax:
                </td>
                <td>
                    <asp:CheckBox ID="ApplyIncomeTaxCheckBox" runat="server" Checked='<%# Bind("ApplyIncomeTax") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    ApplyStampTax:
                </td>
                <td>
                    <asp:CheckBox ID="ApplyStampTaxCheckBox" runat="server" Checked='<%# Bind("ApplyStampTax") %>' />
                </td>
            </tr>
            <tr>
                <td class="style1">
                    ApplySocialSecurityTax:
                </td>
                <td class="style1">
                    <asp:CheckBox ID="ApplySocialSecurityTaxCheckBox" runat="server" Checked='<%# Bind("ApplySocialSecurityTax") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Applicable Location:
                </td>
                <td>
                    <asp:DropDownList ID="CostTypeDropDown1" runat="server" DataSourceID="sds_Locations"
                        DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("ApplicableLocationID") %>'>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                        Text="Update" Style="display: none" />
                    &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False"
                        CommandName="Cancel" Text="Cancel" Style="display: none" />
                </td>
            </tr>
        </table>
    </EditItemTemplate>
</asp:FormView>

<asp:SqlDataSource ID="sds_Benefits" runat="server"  ConnectionString="<%$ ConnectionStrings:ConnFlexibleBenefitsDB %>"
    SelectCommand="SELECT * FROM [View_Benefits]" 
    UpdateCommand="UPDATE [Sil] SET [Code] = @Code, [HRName] = @HRName WHERE [Id] = @Id">
    <UpdateParameters>
        <asp:Parameter Name="Code" Type="String" />
        <asp:Parameter Name="HRName" Type="String" />
        <asp:Parameter Name="Id" Type="Int32" />
    </UpdateParameters>

</asp:SqlDataSource>

And this is my gridview:

<asp:GridView ID="grd_Benefits" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
    CssClass="gridTable" DataSourceID="sds_Benefits" OnRowCommand="grd_Benefits_RowCommand">
    <RowStyle CssClass="gridRow" />
    <FooterStyle CssClass="gridFooter" />
    <SelectedRowStyle CssClass="gridSelectedRow" />
    <HeaderStyle CssClass="gridHeader" />
    <AlternatingRowStyle CssClass="gridAlternatingRow" />
    <EmptyDataTemplate>
        No records found.
    </EmptyDataTemplate>
    <Columns>
        <asp:BoundField DataField="Code" HeaderText="Code" SortExpression="Code" />
        <asp:BoundField DataField="HRName" HeaderText="HRName" SortExpression="HRName" />
        <asp:TemplateField HeaderText="Benefit Type" SortExpression="BenefitType.Name">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("BenefitTypeName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Cost" HeaderText="Cost" SortExpression="Cost" />
        <asp:TemplateField HeaderText="Cost Type">
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("CostTypeName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Quantity Type">
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Bind("QuantityTypeName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lbSelect" runat="server" CausesValidation="False" CommandName="ViewRecord"
                    Text="&lt;img border='0' alt='Edit' src='../images/zoom.gif' /&gt;" CommandArgument='<%# Container.DataItemIndex %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ShowHeader="False">
            <HeaderTemplate>
                <asp:LinkButton ID="lbInsert" runat="server" CausesValidation="false" CommandName="NewRecord"
                    Text="&lt;img border='0' alt='New' src='../images/new.gif'  /&gt;" CommandArgument='<%# Container.DataItemIndex %>'
                    ForeColor="White"></asp:LinkButton>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:LinkButton ID="lbEdit" runat="server" CausesValidation="False" CommandName="EditRecord"
                    Text="&lt;img border='0' alt='Edit' src='../images/edit.gif' /&gt;" CommandArgument='<%# Container.DataItemIndex %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

RowCommand:

    protected void grd_Benefits_RowCommand(object sender, GridViewCommandEventArgs e)
    {            
        if (e.CommandName == "ViewRecord")
        {
            grd_Benefits.SelectedIndex = int.Parse(e.CommandArgument.ToString());
            frm_Benefit.ChangeMode(FormViewMode.ReadOnly);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowFormViewView", "<script type=\"text/javascript\">showFormViewView();</script>");
        }
        else if (e.CommandName == "NewRecord")
        {
            frm_Benefit.ChangeMode(FormViewMode.Insert);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowFormViewInsert", "<script type=\"text/javascript\">showFormViewInsert();</script>");
        }
        else if (e.CommandName == "EditRecord")
        {
            grd_Benefits.SelectedIndex = int.Parse(e.CommandArgument.ToString());
            frm_Benefit.ChangeMode(FormViewMode.Edit);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowFormViewEdit", "<script type=\"text/javascript\">showFormViewEdit();</script>");
        }
    }
2
  • Please show your GridView Markup. Relevant sections only. Commented Jul 22, 2013 at 7:23
  • @FlopScientist I pasted it as it is, because I didn't risk deleting something which may cause problems. Edit button opens the formview above, if you need I can copy may girdview's rowCommand function too. Commented Jul 22, 2013 at 7:58

1 Answer 1

3

Seeing your Markups and taking it as it is, the issue is with the FormView.

You have not set the AllowPaging="true" for FormView. So every time the FormView displays, it will just Display only the First Row of all the data records retrieved.

[ Note that Form View displays only one single record at a time ].

So every time you are selecting 'Edit' in Form View, you are actually editing the First record only ( which is indeed the First row of GridView).

Set the AllowPaging property to true for FormView.

<asp:FormView ID="frm_Benefit" runat="server" DataKeyNames="CustomerID"
 DataSourceID="sds_Benefits" OnItemUpdated="frm_Benefit_ItemUpdated" 
AllowPaging="true">

After that using the Paging index, navigate to any record you want to change and edit it as seen below. enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

thanks my mistake was that I didn't realize the formview was showing the first record, actually I am converting someone elses code, and normally formview should show the clicked item automatically and I think that I have some problem with the click events somewhere. So I need to find it without the pageview, I am checking for why it is not working at the moment
I have added my rowcommand, if you have time can you check that too ? Normally that should be working.
are you implementing this way: Select a row in GridView. This selected row will be displayed in FormView , which you will edit then ?
yes this is exactly what I am tring to do. I am looking for what am I missing, my codes are almost the same with the working codes, but mine is not working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.