Help with a working example connectionstring file

newcoder

New member
Joined
Feb 27, 2018
Messages
4
Programming Experience
Beginner
Hello,
I am very new to C# and I really need help with a project I am working on. I am looking to load a web.config from say c:\temp\web.config and populating some Text box's from sections of the web.config. I would also need to write in these Text Box's and have it write back to the same sections in the web.config. Example <add name="DefaultConnection" connectionString="Data Source=MSSQLSERVER;Initial Catalog=MYDATABASE;Integrated Security=True"
I would like to load the MSSQLSERVER from data source= into TextBox1 and MYDATABASE from Inital Catalog = into TextBox2 and be able to save these back to the file. If some one could help me with a working example I would be very grateful. I have spent many days trying examples but none ever seem to do just what I need. If I have a working example I would be able to take that and add all the fields I need to my program. I need to do this with a Windows form and not ASP.net

I thank you for your time and look forward to hearing from everyone.
 
Last edited:
It is possible you can do it like this:
//add usings to top of file
//using System.Linq;
//using System.Xml.Linq;

var filename = @"C:\temp\web.config";
var doc = XDocument.Load(filename);
var attr = doc.Descendants("connectionStrings").First().Element("add").Attribute("connectionString");

var builder = new System.Data.SqlClient.SqlConnectionStringBuilder(attr.Value);
var value = builder.DataSource; //same with .InitialCatalog
builder.DataSource = "newvalue";


attr.Value = builder.ConnectionString;
doc.Save(filename);

The example is using Basic Queries (LINQ to XML) (C#) | Microsoft Docs
 
This does not seem to do what I need. I need to get the value from DATA SOURCE= which is MSSSQLSERVER and put it in textbox1 then the value from Initalcatalog and put in textbox2. I am also confused by the sqlclient reference as I just need to pull these type of values into textbox's and then change them and write them back to the web.config file.
 
I need to get the value from DATA SOURCE= which is MSSSQLSERVER
That is the value in "value" variable when I run it.
 
That is the value in "value" variable when I run it.


Of course it was my fault lol. Somewhere along the lines of trying different things the MSSQLSERVER got updated to = newvalue


Thank you very much and sorry for the confusion....
 
This example is working for pulling the connectionstring but how do I get information from the appSettings sections? Sorry for all the questions.
 
Look at the "var attr =" example in post 2, it is using Linq to Xml to query elements and attributes from the xml file. There are more examples if you need them in the Microsoft Docs link I also posted.
 
There is functionality built into the Framework to read config files so you don't have to resort to reading them as raw XML. It may be worth your while to check out this example of mine, which reads and writes both the connectionStrings and appSettings elements.

Protected Configuration (Encrypting Config Files)

It's a VB project but the principles are exactly the same for C#. The encryption part was the point of that thread but you can ignore that.
 
Back
Top Bottom