Adding List Methods to a C# Project

Posted on 2012-09-05
Last Modified: 2012-09-06
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

Question by:VBBRett
    1 Comment
    LVL 14

    Accepted Solution

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    This tutorial will show you how to add an attribute to an XML ( stream returned from a Windows Communication Foundation ( (WCF) Web Service.  Some knowled…
    Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something ge…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now