I have a login control with additional steps (First Name and Last Name). The code is this:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>CreateUserWizard Extra</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CreateUserWizard
id="CreateUserWizard1"
OnCreatedUser="CreateUserWizard1_CreatedUser"
Runat="server" >
<WizardSteps>
<asp:WizardStep>
<asp:Label
id="lblFirstName"
Text="First Name:"
AssociatedControlID="txtFirstName"
Runat="server" />
<br />
<asp:TextBox
id="txtFirstName"
Runat="server" />
<br /><br />
<asp:Label
id="lblLastName"
Text="Last Name:"
AssociatedControlID="txtLastName"
Runat="server" />
<br />
<asp:TextBox
id="txtLastName"
Runat="server" />
</asp:WizardStep>
<asp:CreateUserWizardStep />
</WizardSteps>
</asp:CreateUserWizard>
</div>
</form>
</body>
</html>
And in the code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
CreateUserProfile(CreateUserWizard1.UserName, txtFirstName.Text, txtLastName.Text);
}
private void CreateUserProfile(string userName, string firstName, string lastName)
{
string conString = WebConfigurationManager.ConnectionStrings["UserProfiles"].ConnectionString;
SqlConnection con = new SqlConnection(conString);
SqlCommand cmd = new SqlCommand("INSERT UserProfiles (UserName,FirstName,LastName) VALUES (@UserName,@FirstName,@LastName)", con);
cmd.Parameters.AddWithValue("@UserName", userName);
cmd.Parameters.AddWithValue("@FirstName", firstName);
cmd.Parameters.AddWithValue("@LastName", lastName);
using (con)
{
con.Open();
cmd.ExecuteNonQuery();
}
}
}
But when I run the website I get the next error:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 13: private void CreateUserProfile(string userName, string firstName, string lastName)
Line 14: {
Line 15: string conString = WebConfigurationManager.ConnectionStrings["UserProfiles"].ConnectionString;
Line 16: SqlConnection con = new SqlConnection(conString);
Line 17: SqlCommand cmd = new SqlCommand("INSERT UserProfiles (UserName,FirstName,LastName) VALUES (@UserName,@FirstName,@LastName)", con);
Source File: c:\Users\User\Desktop\Vivilove\CreateUserWizardExtra.aspx Line: 15
I don´t know what´s happening. Help please...