为什么我收到对象引用未设置错误

时间:2014-04-02 21:27:20

标签: c# asp.net sql

我的C#代码:

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.Xml.Linq;
using System.Configuration;

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

        PopulatePhysician();

    }

    public void PopulatePhysician()
    {
        string myQuery = "SELECT * FROM tblPhysician";
        string conn = ConfigurationManager.ConnectionStrings["ConnStringTEST"].ConnectionString;
        SqlConnection cn = new SqlConnection(conn);
        SqlCommand cmd = new SqlCommand(myQuery, cn);

        SqlDataReader ddlValues = default(SqlDataReader);
        ddlValues = cmd.ExecuteReader();

        if (!IsPostBack)
        {
            name.DataSource = ddlValues;
            name.DataValueField = "content_id";
            name.DataTextField = "content_title";
            name.DataBind();
            //set the default value for the drop down
            ListItem Item = new ListItem();
            Item.Text = "Select a Last Name...";
            Item.Value = "0";
            //Item.Selected = True
            name.Items.Insert(0, Item);
        }
        cmd.Connection.Close();
        cmd.Connection.Dispose();
    }
}

我的部分web.config文件:

  <appSettings file="Application.config">
    <add key="ConnStringTEST" value="Data Source=ep-db;Initial Catalog=DSCONTENT;Integrated Security=FALSE;user=zytuid;pwd=testingitout;" />
  </appSettings>

ASP代码:

    <asp:DropDownList ID="name" runat="server" ClientIDMode="Static">
    </asp:DropDownList>

为什么会收到以下错误:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 21:     {
Line 22:         string myQuery = "SELECT * FROM tblPhysician";
Line 23:         string conn = ConfigurationManager.ConnectionStrings["ConnStringTEST"].ConnectionString;
Line 24:         SqlConnection cn = new SqlConnection(conn);
Line 25:         SqlCommand cmd = new SqlCommand(myQuery, cn);


Source File: c:\Webserver\WWESTEXEC\zzsearch.aspx.cs    Line: 23 

4 个答案:

答案 0 :(得分:4)

因为ConfigurationManager.ConnectionStrings["ConnStringTEST"]返回null。

ConnectionStrings不引用appSettings部分。它引用了connectionStrings section

答案 1 :(得分:3)

问题是连接字符串是作为AppSettings条目添加的,而不是配置文件中的连接字符串。这就是ConfigurationManager.ConnectionStrings["ConnStringTEST"]返回null并调用ConnectionString属性导致NullReferenceException的原因。

更改此

<appSettings file="Application.config">
    <add key="ConnStringTEST" value="Data Source=ep-db;Initial Catalog=DSCONTENT;Integrated Security=FALSE;user=zytuid;pwd=testingitout;" />
</appSettings>

对此;

<connectionStrings>
    <add name="ConnStringTEST" connectionString="Data Source=ep-db;Initial Catalog=DSCONTENT;Integrated Security=FALSE;user=zytuid;pwd=testingitout;" />
</connectionStrings>

有关如何在ADO.NET中配置和检索连接字符串的详细信息,请参阅此link

答案 2 :(得分:1)

您应该先检查是否已设置该值,然后才能调用ConnectionString

使用以下内容替换您的代码:

 string conn = ConfigurationManager.ConnectionStrings["ConnStringTEST"].ConnectionString 

if(!string.IsNullOrEmpty(ConfigurationManager.ConnectionStrings["ConnStringTEST"])){
    string conn = ConfigurationManager.ConnectionStrings["ConnStringTEST"].ConnectionString ;

    }

答案 3 :(得分:0)

我认为您的问题是您正在查看ConfigurationManager.ConnectionStrings而不是ConfigurationManager.AppSettings。我的意思是您的Web配置中的连接字符串位于AppSettings部分,而不是在connectionStrings部分。

试试这个:

string conn = ConfigurationManager.AppSettings["ConnStringTEST"];