此SqlParameterCollection不包含带有ParameterName’@ UserId’的SqlParameter

我有一个登录页面。 一旦用户成功登录,他们就可以查看和管理他们的个人资料/信息。 这可以通过从数据库中检索数据并在formview上显示来完成。

但是,我的userprofile.aspx.cs文件中出现以下错误:

Exception Details: System.IndexOutOfRangeException: An SqlParameter with ParameterName '@UserId' is not contained by this SqlParameterCollection. Source Error: Line 44: Line 45: // Assign the currently logged on user's UserId to the @UserId parameter Line 46: e.Command.Parameters["@UserId"].Value = currentUserId; Line 47: Line 48: } 

Userprofile.aspx:

   UserId: <asp:Label ID="UserIdLabel1" runat="server" Text='' /> 
Password: <asp:TextBox ID="PasswordTextBox" runat="server" Text='' />
Email: <asp:TextBox ID="EmailTextBox" runat="server" Text='' />
HomeTown: <asp:TextBox ID="HomeTownTextBox" runat="server" Text='' />
HomepageUrl: <asp:TextBox ID="HomepageUrlTextBox" runat="server" Text='' />
Signature: <asp:TextBox ID="SignatureTextBox" runat="server" Text='' />
  UserId: <asp:TextBox ID="UserIdTextBox" runat="server" Text='' />
Password: <asp:TextBox ID="PasswordTextBox" runat="server" Text='' />
Email: <asp:TextBox ID="EmailTextBox" runat="server" Text='' />
HomeTown: <asp:TextBox ID="HomeTownTextBox" runat="server" Text='' />
HomepageUrl: <asp:TextBox ID="HomepageUrlTextBox" runat="server" Text='' />
Signature: <asp:TextBox ID="SignatureTextBox" runat="server" Text='' />
  UserId: <asp:Label ID="UserIdLabel" runat="server" Text='' />
Password: <asp:Label ID="PasswordLabel" runat="server" Text='' />
Email: <asp:Label ID="EmailLabel" runat="server" Text='' />
HomeTown: <asp:Label ID="HomeTownLabel" runat="server" Text='' />
HomepageUrl: <asp:Label ID="HomepageUrlLabel" runat="server" Text='' />
Signature: <asp:Label ID="SignatureLabel" runat="server" Text='' />

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="" onselecting="SqlDataSource1_Selecting" SelectCommand="SELECT UserProfiles.UserId, aspnet_Membership.Password, aspnet_Membership.Email, UserProfiles.HomeTown, UserProfiles.HomepageUrl, UserProfiles.Signature FROM aspnet_Membership INNER JOIN UserProfiles ON aspnet_Membership.UserId = UserProfiles.UserId">

 

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 Guid currentUserId = (Guid)currentUser.ProviderUserKey; // Assign the currently logged on user's UserId to the @UserId parameter e.Command.Parameters["@UserId"].Value = currentUserId; } 

请尝试以下方法:

 DbParameter param = e.Command.CreateParameter(); param.ParameterName = "@UserId"; param.Value = currentUserId; e.Command.Parameters.Add(param); 

我没有测试过这个

创建一个新的SqlParameter并将其添加到集合中。

 SqlParameter param = new SqlParameter("@UserId", currentUserId); e.Command.Parameters.Add(param); 

您必须添加参数

 e.Command.Parameters.AddWithValue("@UserId", currentUserId); 

添加后,您可以通过索引器访问它,如示例所示。


UPDATE

如果您使用的是System.Data.Common命名空间,则AddWithValue方法不可用。 你将不得不做类似的事情

 var param = e.Command.CreateParameter("@UserId", currentUserId); e.Command.Parameters.Add(param); 

这有点复杂,但其优点是您不必隐式创建特定类型的参数,如SqlParamterOleDbParameter

如果您已经在SqlDataSource中列出了参数,那么只需指向它并更改它的值…

 e.Command.Parameters["@UserId"].Value = currentUserId; 

您不需要创建参数,除非您没有在ASP页面中列出它。