sqldatasource更新命令不起作用,没有SQL错误

时间:2013-07-11 14:33:13

标签: c# asp.net

我有一个带有数据源的gridview。当我在数据源上使用更新按钮时它没有更新的某些原因。我没有收到sql错误,直接使用时查询工作正常。

<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>

以下是应用参数的c#:

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认为Debug.WriteLine()是这样我可以看到应该输出参数的输出,这里是一个示例输出:

enter image description here

enter image description here

调试输出: 4911

enter image description here

调试输出: 5.5 7/9/2013 12:00:00 AM 改变了文字

当我按下更新时:

enter image description here

1 个答案:

答案 0 :(得分:1)

您无需在Update事件处理程序中调用DataBindRowUpdating方法,因为更新已经发生,您只需填写参数值。

需要注意但与您的问题无关的另一件事是使用QueryStringParameter。如果不使用查询字符串作为参数源,则最好使用普通Parameter