How to retrieve Connection Strings from a Web App config file

12 05 2010

A friend of mine who’s just starting out with a bit of C# and asp.NET asked me about this yesterday. He had spent the evening before doing lots of searching and hadn’t found anything that actually helped him get his connection string from his web.config file into the C#. So I thought I’d write a little post about it.

The common practice with connection strings in asp.NET is to put them in your web.config file. By doing this, when you need to change your connection string, for example when you put it on your web server, you can change it in one place in the config file and your web application doesn’t need to be changed or rebuilt in any way. As mentioned above, the problem my friend had was getting the connection string from the config file into the C# so here’s some code example to show how it’s done.

1. Put your connection string in your config file (www.connectionstrings.com is a great resource to help with formatting of connection strings and different types of connection strings for different servers). Please excuse the formatting of the XML below, it wouldn’t allow me to use the <> tag’s in the code box so this was a better way!

<connectionStrings>

<add name=”UsefulAppConnString” connectionString=”Data Source=myServerAddress;Initial Catalog=myDataBase;IntegratedSecurity=SSPI;”

</connectionStrings>

2. Add a reference to System.Configuration (right click ‘References > Add Reference’, select the .NET tab and find System.Configuration. Double click it or highlight it and click ‘OK’).

addref

3. Add a using statement to your C# class where you will be pulling in the connection string.

using System.Configuration;

4. Now you have everything you need to get the connection string! In this example I’m just going to put it in a string variable. You could assign it to a string variable when your web app loads that could then be accessed throughout your web application or you could just retrieve it from the config file every time you wanted to use it. That’s up to you.

string connString = ConfigurationManager.ConnectionStrings["UsefulAppConnString"].ToString();

You can also store the connection string in the appSettings part of a config file. If you do, you will need to use ConfigurationManager.AppSettings instead of ConfigurationManager.ConnectionStrings.

So the ConfigurationManager is effectively your config file. Using ConfigurationManager.ConnectionStrings is looking at the data within the ConnectionStrings tags in your config file. The name in the square brackets as you’ve probably noticed is the name attribute I assigned to my connection string in step 1.

So there it is. How to retrieve your connection string from your web.config file.





Date Manipulation in C#

30 03 2010

DateTime to String

When you have a DateTime object that you want to store or present as a string, I find the easiest way to format the date is as follows:

DateTime today = DateTime.Now;
string stDate = today.ToString("dd MMMM yyyy");

This code will return "30 March 2010". You can format it as you like using the ToString method, using the key string combinations for different ways of displaying days, months, year, minutes and seconds. I have linked to a useful site in a previous post that will give you the string combinations you can use, here’s the link again http://blog.stevex.net/string-formatting-in-csharp/ . The ones you will probably use most are under Custom Date Formatting on that site.

If you wanted to add the “th” after the date you could do the following:

DateTime today = DateTime.Now;
string stDate = String.Format("{0}th {1}", today.ToString("dd"), today.ToString("MMMM yyyy"));

This code will return “30th March 2010”.

Adding & Subtracting Days / Months / Years / Hours / Minutes / Seconds

There are methods available to add days, months and years to a DateTime object. AddDays(), AddMonths() etc. So if I wanted to show 6 months time, I would use the following code:

DateTime sixMonthsTime = DateTime.Now.AddMonths(6);
string stDate = sixMonthsTime.ToString("MMMM yyyy");

This code will return “September 2010”.

To subtract days, months, years etc, we use the same method. If I wanted to show 6 months ago, I would use the following code:

DateTime sixMonthsTime = DateTime.Now.AddMonths(-6);
string stDate = sixMonthsTime.ToString("MMMM yyyy");

This code will return “September 2009”.

 

This is all I have chance to write on this subject at the moment but I will add to this post whenever I get chance or come across something new.





String Formatting in C#

5 02 2010

I don’t have much to say in this post really. It’s all in the title and I’m going to point you to this brilliant post on SteveX Compiled. Whenever I need to check up on a string format, this is the place I go! 🙂

http://blog.stevex.net/string-formatting-in-csharp/