Adding List Methods to a C# Project

I have a List Method that I can't seem to get to work.  I get object instance not set to an instance of an object on the connection string for some reason.  Please tell me what I am doing wrong with my method and how to fix it so that I can add a list method.  Please see below for the method:

public List<OfficeMaps> GetOfficeMapsByMapId(int mapid)
            List<OfficeMaps> OfficeMapList = new List<OfficeMaps>();

            string connectionString =  System.Configuration.ConfigurationManager.ConnectionStrings["MapsLocation"].ToString();

            using (SqlConnection connection = new SqlConnection(connectionString))

                using (SqlCommand command = new SqlCommand("SP_GetMapByID", connection))

                    command.Connection = connection;

                    command.CommandType = System.Data.CommandType.StoredProcedure;


                    SqlParameter sqlParamUserId = new SqlParameter("@MapId", SqlDbType.Int);

                    sqlParamUserId.Direction = ParameterDirection.Input;

                    sqlParamUserId.Value = mapid;


                    SqlDataReader reader = command.ExecuteReader();

                    while (reader.Read())
                        System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;

                        OfficeMapList.Add(new OfficeMaps
                            MapId = Convert.ToInt32(reader["mapId"].ToString()),
                            LocationTitle = reader["LocationTitle"].ToString(),
                            CompanyName = reader["CompanyName"].ToString(),
                            SubCompanyName = reader["SubCompanyName"].ToString(),
                            LocationName = reader["LocationName"].ToString(),
                            StreetAddress = reader["StreetAddress"].ToString(),
                            StreetAddress2 = reader["StreetAddress2"].ToString(),
                            StreetAddress3 = reader["StreetAddress3"].ToString(),
                            City = reader["City"].ToString(),
                            MapState = reader["MapState"].ToString(),
                            ZipCode = reader["ZipCode"].ToString(),
                            Region = reader["Region"].ToString(),
                            Country = reader["Country"].ToString(),
                            Latitude = reader["Lattitude"].ToString(),
                            Longitude = reader["Longtitude"].ToString(),
                            PhoneNumber = reader["PhonenNumber"].ToString(),
                            FaxNumber = reader["FaxNumber"].ToString(),
                            SpecialLocationComments = reader["SpecialLocationComments"].ToString(),


                return OfficeMapList;

Open in new window

Who is Participating?
Vel EousResearch & Development ManagerCommented:
The error message you are receiving is suggestive that there is a problem with your connection string assignment and that string connectionString =  System.Configuration.ConfigurationManager.ConnectionStrings["MapsLocation"].ToString(); is not returning what you are expecting it to return.

Put a breakpoint at that line and step over your code to see what, if anything, is being assigned to yourconnectionString variable.  Also make sure that you have correctly added your connection string key to your app.config file:
<?xml version='1.0' encoding='utf-8'?>
      <clear />
      <add name="MapsLocation" 
       connectionString="Your Valid Connection String;" />

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.