本文介绍了使用列表填充下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试使用List<>填充下拉列表,现在抛出此错误:

异常详细信息: System.NullReferenceException:对象引用未设置为对象的实例.
源错误:

Hi
I am trying to populate dropdown using List<>, Now its throwing this error:

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

Line 27:         #region GetAppointmentTypes()
Line 28:         public List<ClsAppointmentServices> GetAppointmentTypes()
Line 29:         {
Line 30:             using (SqlConnection con = new SqlConnection(ConnString))
Line 31:             {


以下是我的用于连接和填充组合框的BusinessLayer代码:


Below is my BusinessLayer Code for connection and populating combobox:

namespace DataSource
{
   public class BusinessLayer
    {
        String ConnString;
        public String ConnString1
        {
            get { return ConnString; }
            set { ConnString = value; }
        }

        public BusinessLayer()
        {
            ConnString = ConfigurationManager.ConnectionStrings["AMS - Database"].ConnectionString;
        }
        #region GetAppointmentTypes()
        public List<ClsAppointmentServices> GetAppointmentTypes()
        {
            using (SqlConnection con = new SqlConnection(ConnString1))
            {
                SqlCommand cmd = new SqlCommand("procGetAppointmentTypes", con);
                cmd.CommandType = CommandType.StoredProcedure;
                List<ClsAppointmentServices> serviceList = new List<ClsAppointmentServices>();
                try
                {
                    con.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        ClsAppointmentServices appointmentTYpes = new ClsAppointmentServices(Convert.ToInt32(reader["TypeID"]), Convert.ToString(reader["AppointmentType"]));
                        serviceList.Add(appointmentTYpes);
                    }//End while
                    reader.Close();
                }
                catch (SqlException)
                {
                    throw new ApplicationException("Error connection to database");
                }
                return serviceList;
            }//End Using
        }
        #endregion GetAppointmentTypes()
    }
}


//Below is my client Layer:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataSource;
public partial class Default3 : System.Web.UI.Page
{
    BusinessLayer systemBusinessLayer;
    List<ClsAppointmentServices> appointmentTypeList;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
            LoadDDLServiceTypes();
    }
    public void LoadDDLServiceTypes()
    {
        appointmentTypeList = new List<ClsAppointmentServices>();
        systemBusinessLayer = new BusinessLayer();
        appointmentTypeList = systemBusinessLayer.GetAppointmentTypes();
        ddlServiceTypes.DataMember = "AppointmentType";
        ddlServiceTypes.DataSource = appointmentTypeList;
    }
}


最后关注我的web.config文件:


Lastly folows my web.config file:

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>

    <system.web>
        <compilation debug="true" targetFramework="4.0">
            <assemblies>
                <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
                <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Speech, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/></assemblies></compilation>
    </system.web>
    <connectionStrings>
        <add name="ConnString" connectionString="Data Source=M104-06;Initial Catalog=AMS-Database;Integrated Security=True;"  providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

推荐答案

ConnString = ConfigurationManager.ConnectionStrings["AMS - Database"].ConnectionString;



因此,当您尝试使用它并在此行中打开连接时,它将引发错误.



And thus, when you try to use it and open a connection in this line, it throws an error.

using (SqlConnection con = new SqlConnection(ConnString1))


很有可能您的连接字符串未正确初始化.


Most probably your connection string is not initialized properly.



这篇关于使用列表填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:56