2

I manually change my configSource to point to a local SQL Server or an SQL Azure server, as needed, during development. I would like for my code to find out the value of the configSource so that my code knows what database server is being used.

Using C#, how do I get the current configSource value?

Note that this is not the same as getting the configSource for a Windows application! That way will not work in ASP.NET!

Reid
  • 3,170
  • 2
  • 23
  • 37
  • 2
    While self-answering is great, I would like to point out that I agree with the downvotes. Judging your question in isolation, there is not a whole lot of research effort shown, nor are there any attempts posted. If you included that, it would be much better. Remember that you still have to ask the question in a way that _other people_ could answer it. – gunr2171 Sep 24 '14 at 16:25
  • 1
    @gunr2171 I feel the purpose of asking a question is that it be searchable in this case. The research I have done is irrelevant to the person who is looking for a solution, which in this case is very easy to implement. So what would be the point to show the research? I work very hard to find information (perhaps too hard) so that I don't post a question when the answer is out there. But I understand what you are saying. So we can agree to disagree on this. Thank you for your support in general. – Reid Sep 24 '14 at 16:32
  • 4
    @gunr2171 I don't know much about this topic, but "How do I find out what the current configSource is using C#?" seems like it would be answerable by other people to me. Generally when people post self-answered questions they don't include where they are "stuck" which is generally what happens when you show research effort; they're not stuck. Why pretend? – eddie_cat Sep 24 '14 at 16:32

1 Answers1

1

The following code will read the configuration section for connectionStrings out of web.config, and grab the ConfigSource attribute:

ConnectionStringsSection connectionStringsSection = 
      System.Web.Configuration.WebConfigurationManager
     .GetSection("connectionStrings", "/Web.config") as ConnectionStringsSection;
string configSource = connectionStringsSection.SectionInformation.ConfigSource;
Lynn Crumbling
  • 12,985
  • 8
  • 57
  • 95
Reid
  • 3,170
  • 2
  • 23
  • 37
  • why would you ask a question an then post that code in the answer.. what is that ..is this working code or non working code..? put it as part of your initial question if this is the code that is not working – MethodMan Sep 22 '14 at 21:25
  • 3
    It is working code. Asking and answering your own question is actually encouraged by SO. I searched and tried many things to get this answer and so I am sharing it with the community so that others don't have the same hassle if they have the same problem. Please see: http://stackoverflow.com/help/self-answer – Reid Sep 22 '14 at 21:54
  • 11
    @DJKRAZE - self answering is something we welcome. If you've spent a couple of days working on a tricky problem where's better to show off your awesome solution. – ChrisF Sep 24 '14 at 16:22
  • @DJKRAZE I have asked SO to identify solution sharing via simultaneous posting of question and answer: http://meta.stackoverflow.com/questions/272082/can-instant-self-answer-be-automatically-identified-as-such – Reid Sep 24 '14 at 16:22
  • 5
    @Ting L: That edit adds nothing to the answer. At all. It's just text for the sake of text. Now I have to parse the text, realize that it adds nothing to the code, realize how much time I've wasted reading that useless fluff *and* realizing that it's useless, and then continue reading to the code knowing the text does nothing to explain the code. And then realize that text was taken from the OP's response to a comment that doesn't even relate to the answer at hand. – BoltClock Sep 24 '14 at 17:11
  • 2
    @BoltClock I was only helping the OP so that his self-answer won't be considered posting bare code (as criticized by DJ) . I have no interest here rep-wise. Not so sure that this is waste of time, and your complaint isn't. – thor Sep 24 '14 at 18:10