Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 416
  • Last Modified:

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))
            {
                connection.Open();

                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;

                    command.Parameters.Add(sqlParamUserId);

                    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

0
VBBRett
Asked:
VBBRett
1 Solution
 
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'?>
  <configuration>
    <connectionStrings>
      <clear />
      <add name="MapsLocation" 
       providerName="System.Data.ProviderName" 
       connectionString="Your Valid Connection String;" />
    </connectionStrings>
  </configuration>

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now