0

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...

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1925034
  • 39
  • 1
  • 1
  • 3
  • Does UserProfiles exist as a connection string name in your web.config? – ScottE Dec 23 '12 at 14:22
  • if you haven't noticed, your insert statement is missing "into" keyword. It however doesn't relate to this exception rather it would be an sqlexception.. – Sunny Dec 23 '12 at 14:52
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 20 '14 at 05:18

1 Answers1

3

Given the error message, I suspect this is the line that's throwing the exception:

string conString = WebConfigurationManager.ConnectionStrings["UserProfiles"].ConnectionString;

... which would certainly give a NullReferenceException if you didn't have a connection string for UserProfiles configured. The ConnectionStrings["UserProfiles"] part will return null, and then when you dereference that for the ConnectionString property, that will throw the exception.

You haven't shown your configuration file, but that's the first thing to check - look in your ConnectionStrings section, and check for a value for the exact name UserProfiles (there could be a subtle typo, such as UserProfile).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • You´re right. I forgot the ConnectionString in the web.config file: Sorry if my question is very simple, I´m a novice. Your answer really helped me. Thank you :D – user1925034 Dec 23 '12 at 14:37