Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6660495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:09:44+00:00 2026-05-26T02:09:44+00:00

We have a certain database that has some employee information, and I just wrote

  • 0

We have a certain database that has some employee information, and I just wrote a small class to access some of this information. This class contains with it an application configuration file that has a connection string to the database. There is only 3 files in this project.
Two .cs files and one App.config file.

The app.config file has this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="connString" value="server=myserver;uid=mysa;pwd=mypwd;database=mydb"/>
  </appSettings>
</configuration>

One of my CS files I have a class like so:

namespace Employees.DAL
{

    public static class DAL
    {
        private static readonly string connString = System.Configuration.ConfigurationManager.AppSettings["connString"].ToString();


        public static Employee GetEmployeeByID(long EmpID)
        {
            Employee e = null;

            using (SqlConnection con = new SqlConnection(connString))
            {
                using (SqlCommand cmd = new SqlCommand("EMPDLL_selEmployeeByID", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@LoginID", SqlDbType.BigInt).Value = EmpID;
                    con.Open();

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            if (reader.Read())
                            {   //todo: implement Employee class
                                e = new Employee((long)reader["LoginID"]
                                                );
                            }

                        }
                    }
                }
            }

            return e;
        }
    }
}

Fairly simple just grabs a record from the db and makes an employee out of it which is in the Employee.cs class:

namespace Employees.Objects
{
    public class Employee
    {
        /// <summary>
        /// ID of employee.
        /// </summary>
        public long LoginID { get; private set; }

        public Employee( long LoginID)
           {
            this.LoginID = LoginID;
           }
     }
}

Now what I did was create a new console project in vs2010 and I add a reference to this .dll file that I built, namely Employees. I need to do this because I have to write this as a reusable class so that others can include it.

So then I just did some simple console app like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Employees.DAL;
using Employees.Objects;

    namespace ConsoleEmployeesTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    Employee e = DAL.GetEmployeeByID(68); //fails right here...
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
                Console.ReadLine();
            }
        }
    }

I get an exception

System.TypeInitilizationException: the type initializer for Employees.DAL.DAL threw an

exception –> System.NullReferenceException: Object reference not set to an instance of an

object.

Is this because the new app does not see the app.config file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:09:45+00:00Added an answer on May 26, 2026 at 2:09 am

    Consider adding the configuration file as a resource to your DLL and then at run time read the value out of the resource instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have certain PHP class methods that access external variables. These variables are not
I have a DBAdmin class that connects to the database, and then some other
I'm writing some code that has to cascade delete records in a certain database,
We have a web application that gets its data from a certain database. The
I have a working query that will return some results(records) from my database, like:
We have 18 databases that should have identical schemas, but don't. In certain scenarios,
Say I have a database containing Books and Users and these users have certain
I have a certain POJO which needs to be persisted on a database, current
I have a database which holds the residents of each house in a certain
I have a system set up to lock certain content in a database table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.