[英]sqldatasource update command not working and no SQL error
I have a gridview with a data source. 我有一个带有数据源的gridview。 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.
我没有收到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>
Here is the c# that applies the parameters: 这是应用参数的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 that the Debug.WriteLine() is so I can see the output of what should be going to the parameters, here is an example output: 注意Debug.WriteLine()是这样,所以我可以看到应该输入参数的输出,这是示例输出:
Debug output: 4911 调试输出:4911
Debug output: 5.5 7/9/2013 12:00:00 AM changed text 调试输出:5.5 7/9/2013 12:00:00 AM更改了文本
And when I press Update: 当我按更新时:
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. 您不需要在
RowUpdating
事件处理程序中调用Update
和DataBind
方法,因为更新已经在进行,您只需要填充参数值即可。
Another thing that needs attention but it is not relevant to your issue is the use of QueryStringParameter
. 需要注意但与您的问题无关的另一件事是
QueryStringParameter
的使用。 If you don't use query string as a parameter source then it is better to use plain Parameter
. 如果您不使用查询字符串作为参数源,那么最好使用plain
Parameter
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.