我有一个名为UserProfile.aspx
. 用户登录后,他们可以在 UserProfile 中查看和编辑他们的详细信息。我使用了 aSqlDataSource
并将数据绑定到 a DetailsView
。在详细信息视图中,它从 3 个表中检索数据,并且选择查询工作正常并且可以成功显示数据。
但是,当我更新信息并点击此错误中的Update
按钮时:DetailsView
异常详细信息:System.Data.SqlClient.SqlException:
必须声明标量变量“@UserId”。
以下是我获取当前登录用户(内部UserProfile.aspx
)的代码:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString1 %>"
SelectCommand="SELECT aspnet_Membership.Email, Details.CustName, Details.CustNum, Details.CustRole, Details.CustStatus, Details.PName, Details.PEmail, Details.PRole, Details.WedDate, aspnet_Users.UserName FROM Details INNER JOIN aspnet_Membership ON Details.UserId = aspnet_Membership.UserId INNER JOIN aspnet_Users ON aspnet_Membership.UserId = aspnet_Users.UserId WHERE Details.UserId = @UserId"
onselecting="SqlDataSource1_Selecting"
UpdateCommand="UPDATE Details SET CustName = @CustName, CustNum = @CustNum, CustRole = @CustRole, CustStatus = @CustStatus, PName = @PName, PEmail = @PEmail, PRole = @PRole, WedDate = @WedDate WHERE [UserId] = @UserId">
<SelectParameters>
<asp:Parameter Name="UserId" type="String" />
</SelectParameters>
编辑
<UpdateParameters>
<asp:Parameter Name="UserId" type="String" />
<asp:Parameter Name="CustName" type="String" />
<asp:Parameter Name="CustNum" type="String" />
<asp:Parameter Name="CustRole" type="String" />
<asp:Parameter Name="CustStatus" type="String" />
<asp:Parameter Name="PName" type="String" />
<asp:Parameter Name="PEmail" type="String" />
<asp:Parameter Name="PRole" type="String" />
<asp:Parameter Name="WedDate" type="String" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<asp:DetailsView ID="DetailsView1" runat="server"
AutoGenerateRows="False"
DataSourceID="SqlDataSource1"
Height="29px"
Width="476px">
<Fields>
<asp:BoundField DataField="UserName" HeaderText="UserName"
SortExpression="UserName" ReadOnly="True" />
<asp:BoundField DataField="Email" HeaderText="Email"
SortExpression="Email" />
<asp:BoundField DataField="CustName" HeaderText="CustName"
SortExpression="CustName" />
<asp:BoundField DataField="CustNum" HeaderText="CustNum"
SortExpression="CustNum" />
<asp:BoundField DataField="CustRole" HeaderText="CustRole"
SortExpression="CustRole" />
<asp:BoundField DataField="CustStatus" HeaderText="CustStatus"
SortExpression="CustStatus" />
<asp:BoundField DataField="PName" HeaderText="PName"
SortExpression="PName" />
<asp:BoundField DataField="PEmail" HeaderText="PEmail"
SortExpression="PEmail" />
<asp:BoundField DataField="PRole" HeaderText="PRole"
SortExpression="PRole" />
<asp:BoundField DataField="WedDate" HeaderText="WedDate"
SortExpression="WedDate" />
<asp:CommandField ShowEditButton="True" />
</Fields>
<PagerTemplate>
<br />
</PagerTemplate>
</asp:DetailsView>
这是UserProfile.aspx.cs
文件:
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
// Get a reference to the currently logged on user
MembershipUser currentUser = Membership.GetUser();
// Determine the currently logged on user's UserId value
// Assign the currently logged on user's UserId to the @UserId parameter
//access the parameter value using e.Command.Parameters
//programmatically set the @UserId:
e.Command.Parameters["@UserId"].Value = currentUser.ProviderUserKey.ToString();
Response.Write(currentUser.ProviderUserKey.ToString());
//When I run the webform, the UserId of the current logged-in user had displayed,
// the @UserId was not empty.
}
我在想也许更新查询无法检测到@UserId
。但既然@UserId
在SELECT
查询中工作,为什么在UPDATE
,它不是???