Click here to Skip to main content
15,868,137 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i have a gridview bound to sqldatasource1 and a formview bound to sqldatasource2...but when i select an item in the gridview it does not fill the corresponding record details in the formview.....i am just selecting one record on load & but not changing it..
this is my code ,it works in another project but not in this one

XML
<%@ Page Title="" Language="C#" MasterPageFile="~/admin.master" AutoEventWireup="true" CodeFile="Expense.aspx.cs" Inherits="Expense" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div>&nbsp;<asp:LinkButton ID="LinkButton1" runat="server"
        onclick="LinkButton1_Click" style="text-align: center">Add New Expense</asp:LinkButton>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Label ID="Label1" runat="server" Text="Total Expenses :"></asp:Label>
&nbsp;<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True"></asp:TextBox>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </div>
<div>
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
        AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1"
        ForeColor="#333333" DataKeyNames="ExpenseID" Width="100%"
        onrowdeleted="GridView1_RowDeleted"
        onselectedindexchanged="GridView1_SelectedIndexChanged">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowSelectButton="True" />
            <asp:BoundField DataField="ExpenseID" HeaderText="ExpenseID"
                InsertVisible="False" ReadOnly="True" SortExpression="ExpenseID" />
            <asp:BoundField DataField="ExpenseName" HeaderText="ExpenseName"
                SortExpression="ExpenseName" />
            <asp:BoundField DataField="ExpenseAmount" HeaderText="ExpenseAmount"
                SortExpression="ExpenseAmount" />
            <asp:BoundField DataField="ExpenseDate" HeaderText="ExpenseDate"
                SortExpression="ExpenseDate" />
            <asp:BoundField DataField="ExpenseDescription" HeaderText="ExpenseDescription"
                SortExpression="ExpenseDescription" />
        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        DeleteCommand="DELETE FROM EXPENSE_TBL WHERE (ExpenseID = @ExpenseID)"
        SelectCommand="SELECT * FROM EXPENSE_TBL">
        <DeleteParameters>
            <asp:Parameter Name="ExpenseID" />
        </DeleteParameters>
    </asp:SqlDataSource>
    <asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource2"
        Width="100%" onitemcreated="FormView1_ItemCreated"
        onitemdeleted="FormView1_ItemDeleted" oniteminserted="FormView1_ItemInserted"
        onitemupdated="FormView1_ItemUpdated" DataMember="DefaultView"
        CellPadding="4" ForeColor="#333333">
        <EditItemTemplate>
            ExpenseID:
            <asp:Label ID="ExpenseIDLabel1" runat="server"
                Text='<%# Eval("ExpenseID") %>' />
            <br />
            ExpenseName:
            <asp:TextBox ID="ExpenseNameTextBox" runat="server"
                Text='<%# Bind("ExpenseName") %>' />
            <br />
            ExpenseAmount:
            <asp:TextBox ID="ExpenseAmountTextBox" runat="server"
                Text='<%# Bind("ExpenseAmount") %>' />
            <br />
            ExpenseDate:
            <asp:TextBox ID="ExpenseDateTextBox" runat="server"
                Text='<%# Bind("ExpenseDate") %>' />
                <ajaxToolkit:CalendarExtender ID="CalendarExtender1" TargetControlID="ExpenseDateTextBox" runat="server">
            </ajaxToolkit:CalendarExtender>
            <br />
            ExpenseDescription:
            <asp:TextBox ID="ExpenseDescriptionTextBox" runat="server"
                Text='<%# Bind("ExpenseDescription") %>' />
            <br />
            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
                CommandName="Update" Text="Update" />
            &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server"
                CausesValidation="False" CommandName="Cancel" Text="Cancel" />
        </EditItemTemplate>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <InsertItemTemplate>
            ExpenseName:
            <asp:TextBox ID="ExpenseNameTextBox" runat="server"
                Text='<%# Bind("ExpenseName") %>' />
            <br />
            ExpenseAmount:
            <asp:TextBox ID="ExpenseAmountTextBox" runat="server"
                Text='<%# Bind("ExpenseAmount") %>' />
            <br />
            ExpenseDate:
            <asp:TextBox ID="ExpenseDateTextBox" runat="server"
                Text='<%# Bind("ExpenseDate") %>' />
                 <ajaxToolkit:CalendarExtender ID="CalendarExtender1" TargetControlID="ExpenseDateTextBox" runat="server">
            </ajaxToolkit:CalendarExtender>
            <br />
            ExpenseDescription:
            <asp:TextBox ID="ExpenseDescriptionTextBox" runat="server"
                Text='<%# Bind("ExpenseDescription") %>' />
            <br />
            <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
                CommandName="Insert" Text="Insert" />
            &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server"
                CausesValidation="False" CommandName="Cancel" Text="Cancel" />
        </InsertItemTemplate>
        <ItemTemplate>
            ExpenseID:
            <asp:Label ID="ExpenseIDLabel" runat="server" Text='<%# Eval("ExpenseID") %>' />
            <br />
            ExpenseName:
            <asp:Label ID="ExpenseNameLabel" runat="server"
                Text='<%# Bind("ExpenseName") %>' />
            <br />
            ExpenseAmount:
            <asp:Label ID="ExpenseAmountLabel" runat="server"
                Text='<%# Bind("ExpenseAmount") %>' />
            <br />
            ExpenseDate:
            <asp:Label ID="ExpenseDateLabel" runat="server"
                Text='<%# Bind("ExpenseDate") %>' />

            <br />
            ExpenseDescription:
            <asp:Label ID="ExpenseDescriptionLabel" runat="server"
                Text='<%# Bind("ExpenseDescription") %>' />
            <br />

            <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"
                CommandName="Edit" Text="Edit" />
            &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
                CommandName="Delete" Text="Delete" />
            &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
                CommandName="New" Text="New" />

        </ItemTemplate>
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
    </asp:FormView>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        DeleteCommand="DELETE FROM EXPENSE_TBL WHERE (ExpenseID = @ExpenseID)"
        InsertCommand="INSERT INTO EXPENSE_TBL(ExpenseName, ExpenseAmount, ExpenseDate, ExpenseDescription) VALUES (@ExpenseName, @ExpenseAmount, @ExpenseDate, @ExpenseDescription)"
        SelectCommand="SELECT * FROM EXPENSE_TBL"
        UpdateCommand="UPDATE EXPENSE_TBL SET ExpenseName = @ExpenseName, ExpenseAmount = @ExpenseAmount, ExpenseDate = @ExpenseDate, ExpenseDescription = @ExpenseDescription WHERE (ExpenseID = @ExpenseID)">
        <DeleteParameters>
            <asp:Parameter Name="ExpenseID" />
        </DeleteParameters>
        <SelectParameters>
            <asp:ControlParameter ControlID="GridView1" Name="ExpenseID" PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
        <InsertParameters>
            <asp:Parameter Name="ExpenseName" />
            <asp:Parameter Name="ExpenseAmount" />
            <asp:Parameter Name="ExpenseDate" />
            <asp:Parameter Name="ExpenseDescription" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="ExpenseName" />
            <asp:Parameter Name="ExpenseAmount" />
            <asp:Parameter Name="ExpenseDate" />
            <asp:Parameter Name="ExpenseDescription" />
            <asp:Parameter Name="ExpenseID" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    </ajaxToolkit:ToolkitScriptManager>
    <br />
    </div>
</asp:Content>
Posted
Updated 16-Apr-14 2:48am
v3
Comments
Ganesh KP 13-Apr-14 22:44pm    
Is the selectionChangedEvent is firing? Put a break point and check it once, if the event is firing and whether the data is coming i.e selected record.
Suk@nta 14-Apr-14 13:43pm    
please give your selectionChangedEvent function code and also check whether its fires on row selection or not
Pawan Wagh 14-Apr-14 14:37pm    
I have solved it myself.......
The select command of sqldatasource2 should be this..
SelectCommand="SELECT * FROM EXPENSE_TBL WHERE ExpenseID=@ExpenseID"
inorder to change record according to selection from gridview....
Rahul VB 16-Apr-14 8:45am    
Very nice brother, post it in the "Add your solution here" section and then accept your own answer. You will be awarded with points. Well done.
- Rahul
Ganesh KP 17-Apr-14 0:03am    
good, so you solved your own question. As @Rahul suggested don't leave the question unanswered, better to give a clear and clarity answer.

1 solution

SQL
The select command of sqldatasource2 should be this..
SelectCommand="SELECT * FROM EXPENSE_TBL WHERE ExpenseID=@ExpenseID"
inorder to change record according to selection from gridview

else it will select all records from database but display only the last record of data table very time selection is changed in gridview
WHERE ExpenseID=@ExpenseID passes the selected record ID to datasource
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900