View Single Post
  #1 (permalink)  
Old March 9th, 2013, 12:34 PM
phil5p phil5p is offline
Registered User
Points: 27, Level: 1
Points: 27, Level: 1 Points: 27, Level: 1 Points: 27, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2013
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Storing Additional User Information (C#) Issue

Hi Everyone,

I thought I might try the forum for some help. I used the book beginning asp.net to create my final year project website and thought I was somewhat hot stuff

However, I used the profile function as demonstrated in the book and found I needed some other way of recording a user profile. My issue's with profile were:

I had problems using the query wizard within my website
I could not edit and update the record
I wanted to assign records default values and drop down boxes, from research I couldn't

I wanted to use profile to allow a logged in user to update their profile, they should only see their profile after logged in.

I found the following tutorial online which seems to do everything I required, albeit somewhat simple. http://www.asp.net/web-forms/tutoria...information-cs

I have followed the tutorial as required and cannot seem to get it to work. Ok, so following the tutorial, the first error I get is:

Error 2 The name 'Membership' does not exist in the current context C:\Users\Philip\Desktop\WebSite5\LoggedProfile.asp x.cs 19 38 C:\...\WebSite5\
Error 1 The type or namespace name 'MembershipUser' could not be found (are you missing a using directive or an assembly reference?) C:\Users\Philip\Desktop\WebSite5\LoggedProfile.asp x.cs 19 9 C:\...\WebSite5\

To combat this I add: using System.Web.Security; - Once inserted the code compiles fine with no errors, however shows no content.

My code is as follows. Is it possible I have configured my web.config file wrong, particularly the authentication/forms elements?

Thank you for taking the time to read this if you have gotten this far!

Code:
<form id="form1" runat="server">
    <div>
    
        <asp:DetailsView ID="UserProfile" runat="server" AutoGenerateRows="False"
            DataKeyNames="UserId" DataSourceID="EmployeeProfileDataSource">
            <Fields>
                <asp:BoundField DataField="UserId" HeaderText="UserId" ReadOnly="True"
                    SortExpression="UserId" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName"
                    SortExpression="FirstName" />
                <asp:BoundField DataField="Surname" HeaderText="Surname"
                    SortExpression="Surname" />
                <asp:BoundField DataField="ContactNumber" HeaderText="ContactNumber"
                    SortExpression="ContactNumber" />
                <asp:BoundField DataField="Office" HeaderText="Office"
                    SortExpression="Office" />
            </Fields>
        </asp:DetailsView>
    
        <asp:SqlDataSource ID="EmployeeProfileDataSource" runat="server"
            ConnectionString="<%$ ConnectionStrings:EmployeeConnectionString %>"
            SelectCommand="SELECT [UserId], [FirstName], [Surname], [ContactNumber], [Office] FROM [employee] WHERE ([UserId] = @UserId)">
            <SelectParameters>
                <asp:Parameter Name="UserId" Type="Object" />
            </SelectParameters>
        </asp:SqlDataSource>
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class LoggedProfile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void EmployeeProfileDataSource_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;
    }
}
Code:
<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <connectionStrings>
    <add name="EmployeeConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Philip\Desktop\WebSite5\App_Data\employeedata.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.web>

    <membership defaultProvider="EmployeeSqlMembershipProvider">
      <providers>
        <!--Add a customized SqlMembershipProvider -->
        <add name="EmployeeSqlMembershipProvider"
        type="System.Web.Security.SqlMembershipProvider"
        connectionStringName="EmployeeConnectionString"
        enablePasswordRetrieval="false"
        enablePasswordReset="true"
        requiresQuestionAndAnswer="true"
        applicationName="SecurityTutorials"
        requiresUniqueEmail="true"
        passwordFormat="Hashed"
        maxInvalidPasswordAttempts="5"
        minRequiredPasswordLength="7"
        minRequiredNonalphanumericCharacters="1"
        passwordAttemptWindow="10"
        passwordStrengthRegularExpression=""/>
      </providers>
    </membership>
    <authentication mode="Forms"/>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
</configuration>