Thursday, March 29, 2012

Login control with default role, and additional fields?

Please advise before I lose my mind.A full example in VB would keep my sanity (maybe its to late...brrrr-looo-hiiiii-haaaa-hoo-brrrrr)

I am a Newbe never programmed before. Its several hints and examples out there but I still don't get it.

My questing is:

  1. How to add a default role, "User_basic", for the users creating a new user account (I don't want the user to be able to select a role with a listbox)
  2. How to save the "First name" (additional field in the control) to my table.

The first thing I do is to create a "Create user step" for my site.

Using the CreateUSerWizard, in the default first step containing the "User name", "Password" etc I have added a new field "First name:" (I am trying to get all my fields in the same step) I also have made a new table (user_info) in my db. This table has 2 columns, UserId and FirsName.

My code so far:

***VB***

PartialClass CreateNewLogUser

Inherits System.Web.UI.Page

ProtectedSub DeactiveStep(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles CreateUserWizard1.CreatedUser

'HOW DO I SAVE THE DEFAULT ROLE (user_basic) FOR THE USER BEFORE THIS STEP (this step is run after the user is created. Or what…???)

'....Role.UserId = "User_basic"....

Dim FirstNameAs TextBox

FirstName = CreateUserWizard1.FindControl("FirstName")

' Todo: Store the FirstNAme to the DB

EndSub

EndClass

***GUI***

<asp:CreateUserWizardID="CreateUserWizard1"runat="server"BackColor="#F7F6F3"BorderColor="#E6E2D8"

BorderStyle="Solid"BorderWidth="1px"Font-Names="Verdana"Font-Size="0.8em">

<WizardSteps>

<asp:CreateUserWizardSteprunat="server">

<ContentTemplate>

<tableborder="0"style="font-size: 100%; font-family: Verdana">

<tr>

<tdalign="center"colspan="2"style="font-weight: bold; color: white; background-color: #5d7b9d">

Sign Up for Your New Account</td>

</tr>

<tr>

<tdalign="right">

<asp:LabelID="UserNameLabel"runat="server"AssociatedControlID="UserName">User Name:</asp:Label></td>

<td>

<asp:TextBoxID="UserName"runat="server"></asp:TextBox>

<asp:RequiredFieldValidatorID="UserNameRequired"runat="server"ControlToValidate="UserName"

ErrorMessage="User Name is required."ToolTip="User Name is required."ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>

</td>

</tr>

<tr>

<tdalign="right">

<asp:LabelID="PasswordLabel"runat="server"AssociatedControlID="Password">Password:</asp:Label></td>

<td>

<asp:TextBoxID="Password"runat="server"TextMode="Password"></asp:TextBox>

<asp:RequiredFieldValidatorID="PasswordRequired"runat="server"ControlToValidate="Password"

ErrorMessage="Password is required."ToolTip="Password is required."ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>

</td>

</tr>

<tr>

<tdalign="right">

<asp:LabelID="ConfirmPasswordLabel"runat="server"AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label></td>

<td>

<asp:TextBoxID="ConfirmPassword"runat="server"TextMode="Password"></asp:TextBox>

<asp:RequiredFieldValidatorID="ConfirmPasswordRequired"runat="server"ControlToValidate="ConfirmPassword"

ErrorMessage="Confirm Password is required."ToolTip="Confirm Password is required."

ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>

</td>

</tr>

<tr>

<tdalign="right">

<asp:LabelID="EmailLabel"runat="server"AssociatedControlID="Email">E-mail:</asp:Label></td>

<td>

<asp:TextBoxID="Email"runat="server"></asp:TextBox>

<asp:RequiredFieldValidatorID="EmailRequired"runat="server"ControlToValidate="Email"

ErrorMessage="E-mail is required."ToolTip="E-mail is required."ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>

</td>

</tr>

<tr>

<tdalign="right">

<asp:LabelID="QuestionLabel"runat="server"AssociatedControlID="Question">Security Question:</asp:Label></td>

<td>

<asp:TextBoxID="Question"runat="server"></asp:TextBox>

<asp:RequiredFieldValidatorID="QuestionRequired"runat="server"ControlToValidate="Question"

ErrorMessage="Security question is required."ToolTip="Security question is required."

ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>

</td>

</tr>

<tr>

<tdalign="right">

<asp:LabelID="AnswerLabel"runat="server"AssociatedControlID="Answer">Security Answer:</asp:Label></td>

<td>

<asp:TextBoxID="Answer"runat="server"></asp:TextBox>

<asp:RequiredFieldValidatorID="AnswerRequired"runat="server"ControlToValidate="Answer"

ErrorMessage="Security answer is required."ToolTip="Security answer is required."

ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>

</td>

</tr>

<tr>

<tdalign="right">

First name:</td>

<td>

<asp:TextBoxID="FirstNameTextBox"runat="server"></asp:TextBox></td>

</tr>

<tr>

<tdalign="center"colspan="2">

<asp:CompareValidatorID="PasswordCompare"runat="server"ControlToCompare="Password"

ControlToValidate="ConfirmPassword"Display="Dynamic"ErrorMessage="The Password and Confirmation Password must match."

ValidationGroup="CreateUserWizard1"></asp:CompareValidator>

</td>

</tr>

<tr>

<tdalign="center"colspan="2"style="color: red">

<asp:LiteralID="ErrorMessage"runat="server"EnableViewState="False"></asp:Literal>

</td>

</tr>

</table>

</ContentTemplate>

</asp:CreateUserWizardStep>

<asp:CompleteWizardSteprunat="server">

</asp:CompleteWizardStep>

</WizardSteps>

<SideBarStyleBackColor="#5D7B9D"BorderWidth="0px"Font-Size="0.9em"VerticalAlign="Top"/>

<TitleTextStyleBackColor="#5D7B9D"Font-Bold="True"ForeColor="White"/>

<SideBarButtonStyleBorderWidth="0px"Font-Names="Verdana"ForeColor="White"/>

<NavigationButtonStyleBackColor="#FFFBFF"BorderColor="#CCCCCC"BorderStyle="Solid"

BorderWidth="1px"Font-Names="Verdana"ForeColor="#284775"/>

<HeaderStyleBackColor="#5D7B9D"BorderStyle="Solid"Font-Bold="True"Font-Size="0.9em"

ForeColor="White"HorizontalAlign="Center"/>

<CreateUserButtonStyleBackColor="#FFFBFF"BorderColor="#CCCCCC"BorderStyle="Solid"

BorderWidth="1px"Font-Names="Verdana"ForeColor="#284775"/>

<ContinueButtonStyleBackColor="#FFFBFF"BorderColor="#CCCCCC"BorderStyle="Solid"

BorderWidth="1px"Font-Names="Verdana"ForeColor="#284775"/>

<StepStyleBorderWidth="0px"/>

</asp:CreateUserWizard>

Firstly to add a default role to the new account, add some code along the lines of

Protected Sub CreateUserWizard1_CreatedUser(ByVal senderAs Object,ByVal eAs System.EventArgs) Roles.AddUserToRole(CreateUserWizard1.UserName,"User_basic")End Sub

Then to save the other fields you need to look at profiles. This site might help you get started.

http://aspnet.4guysfromrolla.com/articles/120705-1.aspx

Hope this gets you started

Jeremy


Hi, I am nearly there. But I am getting this error:

No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type.

Exception Details:System.ArgumentException: No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type

Line 26: Using conLine 27: con.Open()Line 28: cmd.ExecuteNonQuery()Line 29: End UsingLine 30: End Sub

Here is my code in the .aspx.vb-file:

*************************************************'
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports System.Collections.Generic

PartialClass CreateNewLogUser
Inherits System.Web.UI.Page

ProtectedSub CreateUserWizard1_CreatedUser(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles CreateUserWizard1.CreatedUser
Dim FirstNameAs TextBox =CType(CreateUserWizardStep0.ContentTemplateContainer.FindControl("FirstNameTextBox"), TextBox)
Dim LastNameAs TextBox =CType(CreateUserWizardStep0.ContentTemplateContainer.FindControl("FirstNameTextBox"), TextBox)
Dim UserNameTextBoxAs TextBox =CType(CreateUserWizardStep0.ContentTemplateContainer.FindControl("UserName"), TextBox)
Dim UserAs MembershipUser = Membership.GetUser(UserNameTextBox.Text)
Dim UserIDAsObject = User.ProviderUserKey
Dim conStringAsString = WebConfigurationManager.ConnectionStrings("MyConnectionString1").ConnectionString
Dim conAsNew SqlConnection(conString)
Dim cmdAsNew SqlCommand("INSERT LogUser_Personal_info ( UserId, FirstName, LastName ) VALUES ( @.UserId, @.FirstName,@.LastName )", con)
cmd.Parameters.AddWithValue("@.UserID", UserID)
cmd.Parameters.AddWithValue("@.FirstName", FirstName)
cmd.Parameters.AddWithValue("@.LastName", LastName)
Using con
con.Open()
cmd.ExecuteNonQuery()
EndUsingEndSub

End

Class
*******************************************************************

Here from my web.config- file:

<

connectionStrings>
<addname="MyConnectionString1"connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
*****************************************************************

Please advise.

0 comments:

Post a Comment