views:

18

answers:

1

How do I get the old values in a gridview row in the row_updating event?

I'm using a sqldatasource control to populate the grid and want to write the update code in the RowUpdating event.

There are 10 fields displayed in the grid.

I can get the value of the first field using

string old_Category = e.OldValues[0].ToString();.

But it doesn't work for index items greater than 0. This doesn't work

string old_Category = e.OldValues[1].ToString();

it returns an error message

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

even though there are 10 columns in the grid.

======================================== Here's the gridview markup

            <asp:GridView 
                ID="GridView1" 
                runat="server" 
                AutoGenerateColumns="False" 
                DataSourceID="SqlDataSource1" 
                BorderWidth="0px"
                BorderStyle="None" 
                Width="100%" 
                CellPadding="2" 
                PageSize="25" 
                OnRowDeleting="ProjectListGridView_RowDeleting" 
                onrowdatabound="GridView1_RowDataBound" 
                onrowupdating="GridView1_RowUpdating" onrowediting="GridView1_RowEditing" >
                <Columns>
                    <asp:CommandField ShowEditButton="True" />
                    <asp:TemplateField HeaderText="WeekEnding" SortExpression="WeekEnding">

                        <ItemTemplate>
                            <asp:Label ID="lblWeekEnding" runat="server" Text='<%# Bind("WeekEnding") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Project" SortExpression="Project">
                       <EditItemTemplate>

                            <asp:DropDownList 
                                ID="DropDownList1" 
                                runat="server" 
                                DataSourceID="sdsDdlProjectsEdit" 
                                DataTextField="ProjectName" 
                                DataValueField="ProjectID" 
                                AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged" 

                                >
                            </asp:DropDownList>

                            <asp:SqlDataSource 
                                ID="sdsDdlProjectsEdit" 
                                runat="server" 
                                ConnectionString="<%$ ConnectionStrings:ttuser %>" 
                                SelectCommand="SELECT ProjectID,ProjectName FROM dbo.aspnet_starterkits_Projects">
                            </asp:SqlDataSource>

                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("Project") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Category" SortExpression="Category">

                        <EditItemTemplate>


                            <asp:DropDownList 
                                ID="DropDownList2" 
                                runat="server" 
                                DataSourceID="sdsDDL2" 
                                DataTextField="CategoryName" 
                                DataValueField="CategoryID">
                            </asp:DropDownList>

                            <asp:SqlDataSource 
                            runat="server" 
                            ID="sdsDDL2"
                            ConnectionString="<%$ ConnectionStrings:ttuser %>"
                            SelectCommand="SELECT [CategoryID], [CategoryName],[ProjectID] FROM dbo.aspnet_starterkits_ProjectCategories WHERE ([ProjectID]=@ProjectID)">

                             <SelectParameters>
                                 <asp:Parameter Name="ProjectID" />
                             </SelectParameters>

                            </asp:SqlDataSource>



                        </EditItemTemplate>

                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("Category") %>'></asp:Label>
                        </ItemTemplate>

                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Sun" SortExpression="Sun">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtSunEdit" runat="server" Text='<%# Eval("Sun") %>' Columns="2"></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>

                            <asp:Label ID="lblSun" runat="server" Text='<%# Bind("Sun") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Mon" SortExpression="Mon">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtMonEdit" runat="server" Text='<%# Eval("Mon") %>' Columns="2"></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>

                            <asp:Label ID="Label4" runat="server" Text='<%# Bind("Mon") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Tue" SortExpression="Tue">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtTueEdit" runat="server" Text='<%# Eval("Tue") %>' Columns="2"></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label5" runat="server" Text='<%# Bind("Tue") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Wed" SortExpression="Wed">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtWedEdit" runat="server" Text='<%# Eval("Wed") %>' Columns="2"></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label6" runat="server" Text='<%# Bind("Wed") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Thu" SortExpression="Thu">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtThuEdit" runat="server" Text='<%# Eval("Thu") %>' Columns="2"></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label7" runat="server" Text='<%# Bind("Thu") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Fri" SortExpression="Fri">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtFriEdit" runat="server" Text='<%# Eval("Fri") %>' Columns="2"></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label8" runat="server" Text='<%# Bind("Fri") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Sat" SortExpression="Sat">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtSatEdit" runat="server" Text='<%# Eval("Sat") %>' Columns="2"></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label9" runat="server" Text='<%# Bind("Sat") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

and here is the sqldatasource

                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:ttuser %>" 
SelectCommand="SELECT
   @WeekEnding   WeekEnding
  ,Project
  ,ProjectID
  ,Category
  ,CategoryID
  ,isnull([1], 0)  Sun
  ,isnull([2], 0)  Mon
  ,isnull([3], 0)  Tue
  ,isnull([4], 0)  Wed
  ,isnull([5], 0)  Thu
  ,isnull([6], 0)  Fri
  ,isnull([7], 0)  Sat
 from 
(select P.ProjectName Project,P.ProjectID, CAT.CategoryName Category,CAT.CategoryID, Datepart(dw, TE.TimeEntryDate) DOW, TE.TimeEntryDuration Hours
 FROM 
dbo.aspnet_starterkits_TimeEntry TE inner join
dbo.aspnet_starterkits_ProjectCategories CAT on
TE.CategoryID=CAT.CategoryID inner join
dbo.aspnet_starterkits_Projects P on
CAT.ProjectID=P.ProjectID
Where (TE.TimeEntryDate between dateadd(dd, -6, @WeekEnding) and @WeekEnding) AND
TE.TimeEntryUserID=(SELECT UserId FROM dbo.aspnet_Users WHERE UserName=@UserName)) Source
  pivot (max(Hours)for DOW in ([1],[2],[3],[4],[5],[6],[7]) ) as pvt" OldValuesParameterFormatString="original_{0}" 




                        <SelectParameters>
                            <asp:ControlParameter ControlID="WeekEnding2" Name="WeekEnding" 
                                PropertyName="Text" />
                            <asp:ControlParameter ControlID="UserList" Name="UserName" 
                                PropertyName="SelectedValue" />
                        </SelectParameters>



                    </asp:SqlDataSource>
A: 

Just a long shot really. Have you tried setting the DataKeyNames property?

<asp:gridview id="GridView1" 
...
          DataKeyNames="ProjectID"
/>

From msdn

Use the OldValues property (dictionary) to access the original values of the fields in the row to update. This dictionary contains all fields in the row except the key fields. The key fields are defined in the DataKeyNames property of a GridView control.

David
I tried adding DataKeyNames="ProjectID" and it still didn't work.
Joshua Slocum