Click here to Skip to main content
15,118,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:             {

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>();
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                        ClsAppointmentServices appointmentTYpes = new ClsAppointmentServices(Convert.ToInt32(reader["TypeID"]), Convert.ToString(reader["AppointmentType"]));
                    }//End while
                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)
    public void LoadDDLServiceTypes()
        appointmentTypeList = new List<ClsAppointmentServices>();
        systemBusinessLayer = new BusinessLayer();
        appointmentTypeList = systemBusinessLayer.GetAppointmentTypes();
        ddlServiceTypes.DataMember = "AppointmentType";
        ddlServiceTypes.DataSource = appointmentTypeList;

Lastly folows my web.config file:
<?xml version="1.0"?>
  For more information on how to configure your ASP.NET application, please visit

        <compilation debug="true" targetFramework="4.0">
                <add assembly="System.Design, Version=, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
                <add assembly="System.Windows.Forms, Version=, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Speech, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/></assemblies></compilation>
        <add name="ConnString" connectionString="Data Source=M104-06;Initial Catalog=AMS-Database;Integrated Security=True;"  providerName="System.Data.SqlClient" />
Updated 8-Jun-11 0:32am

System.NullReferenceException: Object reference not set to an instance of an object.
This simply means that you are trying to access a property of an object that is null.

Using Visual Studio DEBUGGER will tell you exact line and the object that is null and thus throwing an error.
For now, it looks like there is an issue here:
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.
Anele Ngqandu 8-Jun-11 5:42am
Ok but intrestingly when am using ArrayList it works.
Anele Ngqandu 8-Jun-11 5:45am
Ow ow my bad...didnt know the code below have effect

appointmentTypeList = new List<clsappointmentservices>();
systemBusinessLayer = new BusinessLayer();

appointmentTypeList = systemBusinessLayer.GetAppointmentTypes();

ddlServiceTypes.DataSource = appointmentTypeList;
ddlServiceTypes.DataTextField = "AppointmentType1";
ddlServiceTypes.DataValueField = "AppointmentType1";

Problem solved.
Sandeep Mewara 8-Jun-11 6:15am
Good to know.

Make your connection string static

static string Connectionstring and check
Anele Ngqandu 8-Jun-11 5:49am
thanx my stupidity...problem solved

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900