1

As a follow on from this question I'm building a custom server control to be placed on a Sharepoint 2010 master page.

The idea behind this is that it will display a menu that is dynamically populated from the database.

As this is a server control, I'm building it in a dll but I've run into a small snag. As it has to connect to the database, I need to store the connection string somewhere (and have it able to be configured based on target configuration (dev/test/prod).

I was intending to the user the Entity Framework as my ORM but i'm confused as to where the connection string is to be stored. In a normal ASP.NET web app it goes in the web.config, but this is a server control in a external DLL.

Where do I store the connection string so that I can just build and deploy the assembly containing the server control.

EDIT:
Before I post a bounty, I have tried using an embedded resource XML file to hold the connection string and while this works, I'm curious as to if this is best practice/a better way?

2 Answers 2

2

I would recommend against having the control connect to the database. Instead, the user of the control should connect to the database, and then databind to the control.

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

10 Comments

I'm not sure I understand. How do you mean the user of the control should connect?
Yes. Separation of concerns. A control should be concerned about rendering HTML. Something else should be concerned about passing the data to the control that the control needs to render the HTML.
OK, that's fine, but I can't think of a way that the control would be able to get this information. What would the control call to populate itself (considering I'm just trying to build a control that can be plugged into a Sharepoint page)
It would databind just like a DataGrid does.
Hookay, now I'm pretty confused. What exactly are we databinding to the control? The connection string? or the required data? If it's not clear, I'm using the results of the connection to create some HTML, not to display it in a table or somesuch.
|
0

The connection string should be placed in whatever the application the .dll is in uses for its configuration file. Since this is ASP.NET, the web.config file will do nicely. When you invoke any methods from the DLL, you should pass the connection string to it as an object:

MyConnectionString
{
    get
    {
        return ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString
    }
}

usage:

using(SqlConnection conn = new SqlConnection(MyConnectionString))
{
    //other stuff here. Like making your command object, or passing it.. etc.)
}

2 Comments

So if I add the connection string to the Sharepoint web.config, it will pick this up automatically?
@Alastair Pitts It may or may not. I wouldn't bet on it. You'll have to write a line of code that returns the connection string and pass it to the DLL. I've updated my answer to include a possible way of doing it.

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.