Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# sqldatasource
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 30-Jun-12 19:46pm
Comments
AshishChaudha at 1-Jul-12 0:59am
   
have debug your getUser() method, are you getting the current user logged in??

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

add select parameter called UserId in your aspx
 
<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
 
    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();
 
    }
  Permalink  
v3
Comments
junetanweiting at 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.
Damith Weerasinghe at 1-Jul-12 1:24am
   
Sorry, I have updated my answer, need to change ("@UserId") to ["@UserId"]
junetanweiting at 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.]
junetanweiting at 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!
Damith Weerasinghe at 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
0 Marcin Kozub 310
1 OriginalGriff 248
2 Sergey Alexandrovich Kryukov 215
3 Richard MacCutchan 202
4 Praneet Nadkar 197
0 OriginalGriff 8,048
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 1 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100