Last post Jan 10, 2020 02:08 PM by JonJacobs
Jan 09, 2020 08:13 PM|JonJacobs|LINK
Please help me retrieve this connection string from the Web.Config
Nothing I try works. I always wind up with some default SQL Server Express connection string.
<add name="MyConString" connectionString="<AMySQLConnectionString>"/>
private void Connect()
string constring = <get connection string>;
con = new MySqlConnection(constring);
Jan 09, 2020 08:17 PM|bbcompent1|LINK
Hello there. Make sure your code and web.config is like below:
<add name="ApplicationServices" connectionstring="Data Source=00.0.0.0;Initial Catalog=dbname;User Id=user;Password=pass" providername="MySql.Data.MySqlClient" />
string strConnString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
Jan 09, 2020 09:24 PM|JonJacobs|LINK
Thank you so much for your reply. That is how I originally coded it. As before, it throws a null reference exception.
ConfigurationManager.ConnectionStrings simply does not see the named connection string. In this case ["ApplicationServices"].
That is why I had to reach out for help.
Jan 09, 2020 09:40 PM|bbcompent1|LINK
Hm, and I trust you did reference the namespace for ConfigurationManager, yes? I'd say share your code (strip the sensitive bits) so I can try replicating your issue here.
Jan 09, 2020 09:54 PM|bbcompent1|LINK
I did some digging around and I think I know where the SQL Express connection is coming from, machine.config. According to a StackOverflow discussion:
"It's in there by default, pulled in from your machine config file. To get rid of it just put
Above your first connection string to remove it."
I'm thinking perhaps this may be overriding your MySQL Connection.
Jan 09, 2020 10:59 PM|JonJacobs|LINK
Thank you again. For my desktop applications, the machine.config entry just added to the list without getting in the way. I did what you suggested but it made no difference.
<add name="ApplicationServices" connectionString=...
Since the machine.config entry (which I did already know about) was still present, but my Web.Config entry was not, it has become obvious that my entire <connnectionStrings> piece is totally ignored.
Jan 10, 2020 05:33 AM|Yongqing Yu|LINK
Can you tell us what your project is? Webform? Mvc? .Net core?
When you try multiple solutions and still get the null value, we suggest you check if your connectionStrings are placed in the web.config file under the root directory?
Because some projects contain multiple web.config files, you need to make sure that the web.config file you are currently using is a file in the root directory of the project.
Jan 10, 2020 02:08 PM|JonJacobs|LINK
>Can you tell us what your project is? Webform? Mvc? .Net core?
>>MVC5, VS 2017 Pro
>make sure that the web.config file you are currently using is a file in the root directory of the project.
Amazing. A second Web.Config was created under the Views folder, and I didn't even know it. I deleted that one and edited the main one.
Now it works! Thank you!