Reputation: 287
I had a login page. once user successfuly logged in, they can view and manage their profile/information. This would be done by retrieving data from database and display on a formview.
However this following error appeared inside my userprofile.aspx.cs file:
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:
<asp:FormView ID="FormView1" runat="server"
DataSourceID="SqlDataSource1" DataKeyNames="UserId">
<EditItemTemplate>
UserId:
<asp:Label ID="UserIdLabel1" runat="server" Text='<%# Eval("UserId") %>' />
<br />
Password:
<asp:TextBox ID="PasswordTextBox" runat="server"
Text='<%# Bind("Password") %>' />
<br />
Email:
<asp:TextBox ID="EmailTextBox" runat="server" Text='<%# Bind("Email") %>' />
<br />
HomeTown:
<asp:TextBox ID="HomeTownTextBox" runat="server"
Text='<%# Bind("HomeTown") %>' />
<br />
HomepageUrl:
<asp:TextBox ID="HomepageUrlTextBox" runat="server"
Text='<%# Bind("HomepageUrl") %>' />
<br />
Signature:
<asp:TextBox ID="SignatureTextBox" runat="server"
Text='<%# Bind("Signature") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
UserId:
<asp:TextBox ID="UserIdTextBox" runat="server" Text='<%# Bind("UserId") %>' />
<br />
Password:
<asp:TextBox ID="PasswordTextBox" runat="server"
Text='<%# Bind("Password") %>' />
<br />
Email:
<asp:TextBox ID="EmailTextBox" runat="server" Text='<%# Bind("Email") %>' />
<br />
HomeTown:
<asp:TextBox ID="HomeTownTextBox" runat="server"
Text='<%# Bind("HomeTown") %>' />
<br />
HomepageUrl:
<asp:TextBox ID="HomepageUrlTextBox" runat="server"
Text='<%# Bind("HomepageUrl") %>' />
<br />
Signature:
<asp:TextBox ID="SignatureTextBox" runat="server"
Text='<%# Bind("Signature") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
UserId:
<asp:Label ID="UserIdLabel" runat="server" Text='<%# Eval("UserId") %>' />
<br />
Password:
<asp:Label ID="PasswordLabel" runat="server" Text='<%# Bind("Password") %>' />
<br />
Email:
<asp:Label ID="EmailLabel" runat="server" Text='<%# Bind("Email") %>' />
<br />
HomeTown:
<asp:Label ID="HomeTownLabel" runat="server" Text='<%# Bind("HomeTown") %>' />
<br />
HomepageUrl:
<asp:Label ID="HomepageUrlLabel" runat="server"
Text='<%# Bind("HomepageUrl") %>' />
<br />
Signature:
<asp:Label ID="SignatureLabel" runat="server" Text='<%# Bind("Signature") %>' />
<br />
</ItemTemplate>
</asp:FormView>
</p>
<p>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SecurityTutorialsConnectionString %>"
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">
</asp:SqlDataSource>
</p>
<p>
</p>
</asp:Content>
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;
}
Upvotes: 3
Views: 37090
Reputation: 4868
If you already have the parameter listed in your SqlDataSource, then simply point to it and change it's value ...
e.Command.Parameters["@UserId"].Value = currentUserId;
You do not need to create a parameter, unless you have not listed it back in your ASP page.
Upvotes: 0
Reputation: 112372
You must add the parameter with
e.Command.Parameters.AddWithValue("@UserId", currentUserId);
Once you have added it you could access it through the indexer as in your example.
If you are working with the System.Data.Common
namespace, the AddWithValue
method is not available. You will have to do something like
var param = e.Command.CreateParameter("@UserId", currentUserId);
e.Command.Parameters.Add(param);
This is a little bit more complicated but has the advantage that you do not have to implicitly create a parameter of a specific type like SqlParamter
or OleDbParameter
.
Upvotes: 0
Reputation: 26386
Try the following:
DbParameter param = e.Command.CreateParameter();
param.ParameterName = "@UserId";
param.Value = currentUserId;
e.Command.Parameters.Add(param);
I didn't test this though
Upvotes: 5
Reputation: 3312
Create a new SqlParameter and add it to the collection.
SqlParameter param = new SqlParameter("@UserId", currentUserId);
e.Command.Parameters.Add(param);
Upvotes: 7