how can make a connection string in a class file

Posted by Umeshdwivedi under Regular Expressions on 7/31/2011 | Points: 10 | Views : 4429 | Status : [Member] | Replies : 1
dear to all
i want to mak a connection string in class file and call the function in .cs code
how can i do .give me an example

Latest Technology Trainer
And Part time software consultant


Posted by: SheoNarayan on: 8/1/2011 [Administrator] HonoraryPlatinum | Points: 25

What is the problem you are facing when you are trying to do so? What approach you have used that is causing problem in doing this?

Ideally you store the database connectionstring into the web.config file and access it in the class file using ConfigurationSettings that exists in the System.Configuration namespace. Something like below

protected static string ConnStr = ConfigurationSettings.AppSettings["ConnStr"].ToString();

Above code only works if your database connection string exists into your web.config file like this

<add key="ConnStr" value="Data Source=MY-PC\SQLEXPRESS;Initial Catalog=xxx;Persist Security Info=True;User ID=xxx;Password=xxx;" />


Sheo Narayan

Umeshdwivedi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response