我有一个名为UserProfile.aspx
的网络表单。用户登录后,他们可以在UserProfile上查看和编辑他们的详细信息。我使用SqlDataSource
并将数据绑定到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
中,它不是?
答案 0 :(得分:0)
您需要在Update参数中指定UserId作为参数,并且Select和Update是两个独立的命令。 UserId用于Update WHERE子句,因此需要提供。
<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>
答案 1 :(得分:0)
您需要在UpdateParameters和SelectParameters中声明UserId,如:
<asp:Parameter Name="UserId" type="String" />