2

I have a gridview with a data source. FOr some reason when I use the update button on the data source it is not updating. I am not getting a sql error and the query works fine when used directly.

<asp:GridView ID="viewStoryTime" runat="server" AllowPaging="True" AutoGenerateColumns="False"
     DataSourceID="SqlDataSource10" DataKeyNames="NonScrumStoryId, PK_DailyTaskHours" BackColor="#DEBA84"
     BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2"
     Width="525px" OnRowEditing="viewStoryTime_OnRowEditing" OnRowCancelingEdit="viewStoryTime_OnRowCancelingEdit" OnRowUpdating="viewStoryTime_OnRowUpdating" OnRowUpdated="viewStoryTime_OnRowUpdated" >
     <Columns>
          <asp:BoundField DataField="Hours" HeaderText="Hours" SortExpression="Hours" />
          <asp:BoundField DataField="Notes" HeaderText="Notes" SortExpression="Notes" />
          <asp:BoundField DataField="ActivityDate" HeaderText="Date" SortExpression="ActivityDate" DataFormatString="{0:MM/dd/yyyy}" />
          <asp:CommandField ShowEditButton="True" />
          </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource10" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
     SelectCommand="SELECT [DailyTaskHours].[PK_DailyTaskHours], [DailyTaskHours].[NonScrumStoryId], [DailyTaskHours].[Hours], [DailyTaskHours].[Notes], [DailyTaskHours].[ActivityDate] FROM [NonScrumStory], [DailyTaskHours] WHERE [DailyTaskHours].[NonScrumStoryId] = @nonScrumStoryId AND [NonScrumStory].[PK_NonScrumStory] = @nonScrumStoryId"
     UpdateCommand="UPDATE [DailyTaskHours] SET [Hours] = @setEditHoursParam, [ActivityDate] = @setEditActivityDateParam, [Notes] = @setEditNotesParam WHERE [PK_DailyTaskHours] = @setDailyPKParam">
     <SelectParameters>
          <asp:QueryStringParameter Name="nonScrumStoryId" Type="String" />
     </SelectParameters>
     <UpdateParameters>
           <asp:QueryStringParameter Name="setEditHoursParam" Type="String" />
           <asp:QueryStringParameter Name="setEditActivityDateParam" Type="String" />
           <asp:QueryStringParameter Name="setEditNotesParam" Type="String" />
           <asp:QueryStringParameter Name="setDailyPKParam" Type="String" />
     </UpdateParameters>
</asp:SqlDataSource>

Here is the c# that applies the parameters:

protected void viewStoryTime_OnRowEditing(object sender, GridViewEditEventArgs e)
{
    SqlDataSource10.UpdateParameters["setDailyPKParam"].DefaultValue = viewStoryTime.DataKeys[e.NewEditIndex].Values["PK_DailyTaskHours"].ToString();
    System.Diagnostics.Debug.WriteLine(viewStoryTime.DataKeys[e.NewEditIndex].Values["PK_DailyTaskHours"].ToString());

}

protected void viewStoryTime_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
    SqlDataSource10.UpdateParameters["setEditHoursParam"].DefaultValue = e.NewValues[0].ToString();
    SqlDataSource10.UpdateParameters["setEditActivityDateParam"].DefaultValue = e.NewValues[2].ToString();
    SqlDataSource10.UpdateParameters["setEditNotesParam"].DefaultValue = e.NewValues[1].ToString();
    System.Diagnostics.Debug.WriteLine(e.NewValues[0].ToString());
    System.Diagnostics.Debug.WriteLine(e.NewValues[2].ToString());
    System.Diagnostics.Debug.WriteLine(e.NewValues[1].ToString());
    SqlDataSource10.Update();
    SqlDataSource10.DataBind();
}

Nopte that the Debug.WriteLine() is so I can see the output of what should be going to the parameters, here is an example output:

enter image description here

enter image description here

Debug output: 4911

enter image description here

Debug output: 5.5 7/9/2013 12:00:00 AM changed text

And when I press Update:

enter image description here

1 Answer 1

1

You don't need to call Update and DataBind methods in RowUpdating event handler because update is already happening and there you only need to fill values for parameters.

Another thing that needs attention but it is not relevant to your issue is the use of QueryStringParameter. If you don't use query string as a parameter source then it is better to use plain Parameter.

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

Comments

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.