Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi experts,

I have a webform called UserProfile.aspx. After user logged-in, they can view their particulars at UserProfile. I used SqlDatasource and was binde with DetailsView.
In the detailsview, it retrieved data from 2 table; aspnet_Membership and Details table respectively.

Following is the code which I get the current logged-in user(inside 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);
}


and here is my SQL SELECT query:
SELECT Details.CustName, Details.UserId, Details.CustNum, Details.CustRole, Details.CustStatus, Details.PName, Details.PEmail, Details.PRole, Details.WedDate, aspnet_Membership.Password, aspnet_Membership.Email FROM Details INNER JOIN aspnet_Membership ON Details.UserId = aspnet_Membership.UserId WHERE (Details.UserId = @UserId)


and this line from the select statement seem to have problem:
WHERE (Details.UserId = @UserId)
they could not detect whose is the currentUser and when i execute the query in the query builder, a dialog-box pop up and prompt to enter value for its parameter. and it was stated that "@UserId" was null which I have no idea why is it so when I had already passed the value.

Please give me suggestions and solutions on how to solve this as I already at my wits, I had researched about this the whole day but it was no luck..
Posted
Comments
AshishChaudha 1-Jul-12 0:59am    
have debug your getUser() method, are you getting the current user logged in??

1 solution

add select parameter called UserId in your aspx

C#
<asp:sqldatasource id="SqlDataSource1" runat="server" xmlns:asp="#unknown">
    ConnectionString="<%$ ConnectionStrings:Con_StringConnectionString %>"  
    SelectCommand="SELECT Details.CustName, Details.UserId, Details.CustNum, Details.CustRole, Details.CustStatus, Details.PName, Details.PEmail, Details.PRole, Details.WedDate, aspnet_Membership.Password, aspnet_Membership.Email FROM Details INNER JOIN aspnet_Membership ON Details.UserId = aspnet_Membership.UserId WHERE (Details.UserId = @UserId)">
	<selectparameters>
		<asp:parameter name="UserId" type="String" />
		<selectparameters>
	</selectparameters></selectparameters></asp:sqldatasource>



and then

C#
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    // Get a reference to the currently logged on user
    MembershipUser currentUser = Membership.GetUser();
    e.Command.Parameters["@UserId"].Value = currentUser.ProviderUserKey.ToString();

}
 
Share this answer
 
v3
Comments
mathidioticz 1-Jul-12 1:19am    
Hi, thanks for your reply.
I had adding your suggestions to my code, however for the "e.Command.Parameters("@UserId").Value = currentUser.ProviderUserKey;" lines, it occured an error.
- non-invocable member 'System.Data.Common.Dbcommand.Parameters' cannot be use like a method.
DamithSL 1-Jul-12 1:24am    
Sorry, I have updated my answer, need to change ("@UserId") to ["@UserId"]
mathidioticz 1-Jul-12 1:40am    
However, another error appeared.
After I logged-in, and click the "UserProfile" hyperlink to direct me to UserProfile page, this error appeared:
Exception Details: System.InvalidCastException: Object must implement IConvertible.

I just researched on this problem.. would it be parse the UserId to string??

This is the first 3 lines from stack trace:
[InvalidCastException: Object must implement IConvertible.]
System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +9509637
System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType) +5089538

[InvalidCastException: Failed to convert parameter value from a Guid to a String.]
mathidioticz 1-Jul-12 1:47am    
Damith!!! I solved the error!
e.Command.Parameters["@UserId"].Value = currentUser.ProviderUserKey.ToString();

I parse the UserId to String and the error was solved.
Thanks for your great help, you saved my life!! (:
Thanks a million!
DamithSL 1-Jul-12 4:42am    
Great! Happy to hear that you able to find a solution to cast exception. Updated answer with your finding, in future it will help someone else like you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900