登录控制时的对象引用错误

时间:2012-12-23 14:16:25

标签: c# asp.net nullreferenceexception

我有一个带有附加步骤(名字和姓氏)的登录控件。代码是这样的:

<%@ 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>

在后面的代码中:

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();
    }
}
}

但是当我运行网站时,我得到了下一个错误:

对象引用未设置为对象的实例。

描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

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 

我不知道发生了什么。请帮忙......

1 个答案:

答案 0 :(得分:3)

鉴于错误消息,我怀疑这是抛出异常的行:

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

...如果您没有配置NullReferenceException的连接字符串,肯定会给UserProfilesConnectionStrings["UserProfiles"]部分将返回null,然后当您取消引用ConnectionString属性时,将抛出异常。

您尚未显示配置文件,但这是首先要检查的内容 - 查看ConnectionStrings部分,并检查完全名称UserProfiles的值(可能是一个微妙的错字,例如UserProfile)。

相关问题