1

1 Solution.

2 Projects (1.WEB - 2. Three Layered Structure)

Calling a stored procedure: Web -> BOL -> BLL -> DAL.

DAL's Method: gets the connection string and the executes.

 public DataSet ExecuteSQL(string sp, Persona user)
    {
        EH eh = new EH(); // Error_Handling.cs object
        try
        {
            DataSet ds = new DataSet();
            string connectionString = null;
            connectionString = GetNewConnection("BO"); //HERE COMES THE ERROR
            SqlConnection conn = new SqlConnection(connectionString);
            using (conn)
            { ....

GetNewConnection().

 public string GetNewConnection(string server)
    {
        return ConfigurationManager.ConnectionStrings["BO"].ConnectionString;
    }

App.Config fragment naming the desired connection

<connectionStrings>
<add name="BO"
    connectionString="Data Source=Server;Initial Catalog=BO;User ID=WebUser;Password=333;Integrated Security=False"
    providerName="System.Data.SqlClient" />

I get the following Exception when returning from GetNewConnection()

  • $exception {"Object reference not set to an instance of an object."} System.Exception {System.NullReferenceException}

Why is it getting a null reference?

4
  • 1
    App.config for a web project? Shouldn't that be web.config? Commented Aug 6, 2012 at 14:31
  • BTW: at GetNewConnection, shouldn't you use the server argument instead of hard coding "BO"? Commented Aug 6, 2012 at 14:32
  • @AndreCalil Yes, this is for testing purposes. Commented Aug 6, 2012 at 14:37
  • 1
    DLLs do not usually have their own configuration files (the configuration system certainly doesn't have defaults that make that kind of scenario easy). This is because DLLs are loaded into an executable process - the configuration system assumes all configuration will be part of that executable). Commented Aug 6, 2012 at 14:44

2 Answers 2

4

You should have the ConnectionString in the Web.Config of the UI project, Your WEB Project

Copy that ConnectionString section and Paste in your Web.Config and it should work fine

Sign up to request clarification or add additional context in comments.

4 Comments

But I want it to be in DLL. Because once the system is running. WebPage will probably be on another location. So, if Web requests go to one server for C#, and then before executing SQL it has to go to another server to get the connectionstring, and then go back again to the other server to excute SQL... It sounds like a waste of time. Am I missing something?
Yes - the DLL will be on the web server as part of the web project
You will be deploying your web files & the DLL's(data access layer project DLL ) together in the same server, rite ? same bin folder of the web app ?
@Shyju oh, I got confussed, yes, they'll be together in the same server.
3

The connectionstring should be in the web.config, not the app.config

1 Comment

Check comment on previuos answer please.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.