0

In my application I implement a dynamic connection string that get values directly from the app.config file. But before get values I assigned values to label text and the call it from connection string. I want to get that value directly from string without using label

This is my code

dbserverip.Text = ConfigurationManager.AppSettings["serverip"].ToLower().Trim();
curport.Text = ConfigurationManager.AppSettings["dbport"].ToLower().Trim();
currentdb.Text = ConfigurationManager.AppSettings["defdatabase"].ToLower().Trim();

after I assigned values to labels, I called it from connection string like this:

string constring = string.Format(
    "datasource='{0}';port='{1}';database='{2}';username=uwloanmanmain;password=XXXXXXXX;Connect Timeout=180;Command Timeout=180",
    dbserverip.Text, curport.Text, currentdb.Text); 

but I want to do this without using any labels. Just only using string values. How can I do this?

2
  • are you familiar with Session objects or you could store these in a public static string dbserverip { get; set; }; for example.. look up how to declare and use string variables as well MSDN C# Strings Commented Nov 13, 2014 at 15:58
  • In that case, you need to define those values as string fields in your class, assign the values from the config file to those fields, and you're done! Commented Nov 13, 2014 at 15:59

3 Answers 3

1

Replace dbserverip.Text with ConfigurationManager.AppSettings["serverip"].ToLower().Trim(), curport.Text with ConfigurationManager.AppSettings["dbport"].ToLower().Trim(), and currentdb.Text with ConfigurationManager.AppSettings["defdatabase"].ToLower().Trim()

string constring = string.Format("datasource='{0}';port='{1}';database='{2}';username=uwloanmanmain;password=xxxxxxxxxxxx;Connect Timeout=180;Command Timeout=180", 
                       ConfigurationManager.AppSettings["serverip"].ToLower().Trim(), 
                       ConfigurationManager.AppSettings["dbport"].ToLower().Trim(), 
                       ConfigurationManager.AppSettings["defdatabase"].ToLower().Trim());
Sign up to request clarification or add additional context in comments.

Comments

1

I imagine in the place where you construct the connection string you don't have an access to the ConfigurationManager. The way I see to resolve this is to create a separate type/class, that once filled with data from config in the place where you now populate labels, is available among types of your program.

And also for this kind of information may be useful to use SecureString class.

1 Comment

Yes. as you said i cant access configurationmanager where i construct connection string. thanks for your reply sir. i'll look in to securestring class
0

You can use string variables like this.

string sdbserverip = ConfigurationManager.AppSettings["serverip"].ToLower().Trim();
string scurport = ConfigurationManager.AppSettings["dbport"].ToLower().Trim();
string scurrentdb = ConfigurationManager.AppSettings["defdatabase"].ToLower().Trim();

string constring = string.Format("datasource='{0}';port='{1}';database='{2}';username=uwloanmanmain;password=xxxxxxxxxxxxxxx;Connect Timeout=180;Command Timeout=180", sdbserverip, scurport, scurrentdb);

Comments

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.