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:
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:
C#
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:
C#
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;
    }
}

Lastly folows my web.config file:
XML
<?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>
Posted
Updated 8-Jun-11 0:32am
v2

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.
   
Comments
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";
ddlServiceTypes.DataBind();

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

Make your connection string static

static string Connectionstring and check
   
Comments
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