Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP .Net Get Current User ID from SQL Server Compact database

I have a page that I'm trying to identify the current user's id key so that I can insert additional information for a new table. I've included the code for the page that works in SQL Server Express, but I think that there is a reference issue on the binding to identify the @UserID.

Any thoughts?

<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" Title="Untitled Page" %>

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Configuration" %>

<%@ Import Namespace="System.Data.SqlServerCe" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.WebControls.WebParts" %>
<%@ Import Namespace="System.Xml.Linq" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Security.Permissions" %>

<script language="c#" runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {


    }

    protected void UserProfileDataSource_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {

        //MembershipUser user = Membership.GetUser();
        //string UserID = user.ProviderUserKey.ToString();
        //e.Command.Parameters["@UserId"].Value = UserID;

        // 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;
    }

    protected void UserProfile_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        SettingsUpdatedMessage.Visible = true;
    }
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
    <h2>Update Your Settings</h2>
    <p>
        <asp:Label ID="SettingsUpdatedMessage" runat="server" Text="Your settings have been updated." EnableViewState="false" Visible="false"></asp:Label>
    </p>

    <!-- 
             ProviderName="System.Data.SqlServerCe.4.0" -->
    <asp:DetailsView ID="UserProfile" runat="server" 
        AutoGenerateRows="False" DataKeyNames="UserId" 
        DataSourceID="UserProfileDataSource" DefaultMode="Edit" 
        onitemupdated="UserProfile_ItemUpdated">
        <Fields>
            <asp:BoundField DataField="HomeTown" HeaderText="HomeTown" 
                SortExpression="HomeTown" />
            <asp:BoundField DataField="HomepageUrl" HeaderText="HomepageUrl" 
                SortExpression="HomepageUrl" />
            <asp:BoundField DataField="Signature" HeaderText="Signature" 
                SortExpression="Signature" />
            <asp:CommandField ShowEditButton="True" />
        </Fields>
    </asp:DetailsView>

    <asp:SqlDataSource ID="UserProfileDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" 
             ProviderName="<%$ ConnectionStrings:DefaultConnection.ProviderName %>"
        SelectCommand="SELECT [UserId], [HomeTown], [HomepageUrl], [Signature] FROM [UserProfiles] WHERE ([UserId] = @UserId)" 
        onselecting="UserProfileDataSource_Selecting" UpdateCommand="UPDATE UserProfiles SET
    HomeTown = @HomeTown,
    HomepageUrl = @HomepageUrl,
    Signature = @Signature
WHERE UserId = @UserId
">
        <SelectParameters>
            <asp:Parameter Name="UserId" Type="Object" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="HomeTown" />
            <asp:Parameter Name="HomepageUrl" />
            <asp:Parameter Name="Signature" />
            <asp:Parameter Name="UserId" />
        </UpdateParameters>
    </asp:SqlDataSource>
</asp:Content>

I keep getting the error:

System.ArgumentException: No mapping exists from DbType Object to a known SqlDbType.

I think it is related to the UserProfileDataSource_Selecting, but I'm not sure. I can't find an equivalent SQL Server CE reference to use.

like image 331
user2511441 Avatar asked May 01 '26 01:05

user2511441


1 Answers

Based on Managed Data Type Mappings (SQL Server Compact) by comparison to SQL Server Data Type Mappings it seems that Object is not a valid DbType and can not be mapped to SqlDbType in SQL Server Compact (but is valid in SqlServer).

Since Userid is a Guid, try to replace:

<SelectParameters>
   <asp:Parameter Name="UserId" Type="Object" />
</SelectParameters>
<UpdateParameters>
   <asp:Parameter Name="HomeTown" />
   <asp:Parameter Name="HomepageUrl" />
   <asp:Parameter Name="Signature" />
   <asp:Parameter Name="UserId" />
</UpdateParameters>

With :

<SelectParameters>
   <asp:Parameter Name="UserId" DbType="Guid" />
</SelectParameters>
<UpdateParameters>
   <asp:Parameter Name="HomeTown" />
   <asp:Parameter Name="HomepageUrl" />
   <asp:Parameter Name="Signature" />
   <asp:Parameter Name="UserId" DbType="Guid" />
</UpdateParameters>
like image 133
Chris Avatar answered May 02 '26 16:05

Chris