20

I uploaded my site on godaddy shared host. I can access this database from my management studio. I can't access this database from my site. I get following error:

Format of the initialization string does not conform to specification starting at index 0.

My connection string is in web.config and it looks like this:

<connectionStrings>
    <add name="mcn" connectionString="Data Source=mydatabase.db.8706856.hostedresource.com; Initial Catalog=mydatabase; User ID=username; Password=xyz;" providerName="System.Data.SqlClient" />    
</connectionStrings>

What cause this error? I have also tried to write ...Data Source=(local);...

gunr2171
  • 16,104
  • 25
  • 61
  • 88
1110
  • 7,829
  • 55
  • 176
  • 334
  • Do you have the stack trace for the exception? – Michael Liu Jan 27 '12 at 22:10
  • Yes I have but there is nothing important. This must be something with connection string. I tried to rename it to 'LocalSqlServer' but nothing. – 1110 Jan 27 '12 at 22:18
  • How is your code referencing the connection string? – Michael Liu Jan 27 '12 at 22:43
  • I am using 'ninject IOC'. In global.asax I have 'Bind().To().WithConstructorArgument( "connectionString", ConfigurationManager.ConnectionStrings["mcn"].ConnectionString);' Code is fine. As I said it works on my PC. It doesn't work on live server. – 1110 Jan 27 '12 at 22:46
  • Do you get the same error if you copy the entire Web.config file—exactly as is—back from GoDaddy to your own PC? – Michael Liu Jan 27 '12 at 22:50
  • When I do that I get the same error. Here how I build application. I build it normally and right click on project and select Build Deployment Package. Then I copy all from .../obj/debug/package/packageTmp to server. In web config instead whole connection string I have: – 1110 Jan 27 '12 at 22:57

20 Answers20

19

It might help to see what the actual connection string is. Add to Global.asax:

throw new Exception(ConfigurationManager.ConnectionStrings["mcn"].ConnectionString);

If the actual connection string is $(ReplacableToken_mcn-Web.config Connection String_0), that would explain the problem.

Michael Liu
  • 52,147
  • 13
  • 117
  • 150
  • Now I have checked and yes my connection string is '$(ReplacableToken_mcn-Web.config Connection String_0)' So I have copied my normal web/config to host server and it works. But is it ok? Have I make some security issue now? – 1110 Jan 28 '12 at 12:17
  • 6
    I wouldn't worry that there is a security issue; Web.config is protected from browsing by Internet users. Just make sure your database password is unique to GoDaddy, because GoDaddy administrators *can* see your password. – Michael Liu Jan 28 '12 at 13:55
  • @MichaelLiu +1 just been had by this. – Paul Aldred-Bann Jan 31 '13 at 10:13
5

I was facing the same problem and found out that my connection string had an extra double-quote character in the middle of the connection string.

dotNET
  • 33,414
  • 24
  • 162
  • 251
  • IMHO, this answers a more general version of the question in a way that may help more users arriving here wondering WTF "Format of the initialization string does not conform to specification starting at index 0." means. – Mike Devenney Oct 03 '17 at 12:30
4

For my case, the culprit was the semicolon and double quotes in the password for prod DB. Our IT team use some tool to generate passwords, so it generated one with the semicolon and double quotes Connectionstring looks like

<add key="BusDatabaseConnectionString" value="Data Source=myserver;Initial Catalog=testdb;User Id=Listener;Password=BlaBla"';[]qrk/>

Got the password changed and it worked.

Manish
  • 59
  • 2
  • 1
    This was actually a problem for me too when using the EF Core CLI tools. The connection string worked everywhere else except when using those tools. Removing the quotes solved the issue immediately. – JSextonn May 20 '18 at 06:12
3

I originally was calling the connection string value as shown below (VB.NET) and got the error being mentioned

 Using connection As New SqlConnection("connectionStringName") 
  '// more code would go here...
 End Using

adding a reference to System.Configuration and updating my code as shown below was my solution. The connection string was not the problem since others controls used it without any issues (SqlDataSource)

Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("connectionStringName").ConnectionString)
    '// more code would go here...
End Using
Geovani Martinez
  • 2,053
  • 2
  • 27
  • 32
2

I had this in VS2015 and my fix was to change the first line in my WebConfig from

<?xml version="1.0" encoding="utf-8"?>

to

<?xml version="1.0"?>

Curious.

sobelito
  • 1,525
  • 17
  • 13
2

I was having the same issue when accessing a published ASP.NET Web Api. In my case, I realized that when I was about to publish the Web Api, I had not indicated a connection string inside the Databases section:

After using the three dot button, the connection string will be displayed on the text field to the left

So I generated it using the three dot button, and after publishing, it worked.

What is weird, is that for a long time I am pretty sure that there was no connection string in that configuration but it still worked.

ndarriulat
  • 749
  • 1
  • 9
  • 11
1

Another pitfall is that connectionString sometimes refers to the name of the connection string in the app/web-config and sometimes to the actual connection string itself and vice versa.

Very easy to fix but sometimes hard to spot.

Jonas Stensved
  • 14,378
  • 5
  • 51
  • 80
0

My issue was that my web.config file kept a reference to a deleted entity model connection so check there are not any outdated connection strings.

kravits88
  • 12,431
  • 1
  • 51
  • 53
0

I got below error when trying to launch application :

Format of the initialization string does not conform to specification starting at index 57.

And during my research I found this stack and I was able to fix this error by looking at the web config file and found there was a extra string in the passowrd. Once I removed the string I was able to access the website without any error.

0

I was getting this exception, fixed it by adding throwIfV1Schema: false to my DbContext constructor:

public class AppDb : IdentityDbContext<User>
{
    public AppDb()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }
}
0

For anyone who may stumble across this thread while trying to fix this same error that results by running Enable-Migrations, chances are none of the solutions above will help you (I tried them all).

I encountered this same issue in Web API 2 after running this in PM console:

Enable-Migrations -EnableAutomaticMigrations -ConnectionString IdentityConnection -ConnectionProviderName System.Data.SqlClient -Force

I fixed it by changing it to actually use the ApplicationDbContext created in IdentityModels.

Enable-Migrations -ContextTypeName ApplicationDbContext -EnableAutomaticMigrations -Force

The interesting thing is not only does this reference the same exact connection string, but the constructor includes code that 4castle said was a potential fix (i.e., the throwIfV1Schema: false suggestion.

Note that the -Force parameter is only being used because the Configuration.cs file already exists.

0

I have wasted 1.5 working days on this error and in the end a coworker solved the issue by replacing User Id by Uid and password by Pwd. The updated connection string for .Net was the error for me

Maham Khan
  • 31
  • 1
0

None of the listed solutions in this thread worked for me. I started getting this error after I made some changes to the connection strings section of the web.config file. (My app connects to multiple databases.) I carefully examined what changes I had made are realized I had removed the tag at the top of my list. I restored the tag at the top of my list of connection strings and the problem went away immediately. This site that was getting the error is a application that resides below the main site (https://www.domain.org/MySite). This might not fix the problem for everyone, but it did resolve the problem for me.

0

An unwanted single quote was my problem. Checking the connection string from the location of the index mentioned in the error string helped me spot the issue.

Kirsten
  • 15,730
  • 41
  • 179
  • 318
0

If you're using EF and Publish Profiles, you could have a blank connection string entry in your publish profile. Annoying but entirely possible.

Mike Devenney
  • 1,758
  • 1
  • 23
  • 42
0

I had the same error message in my localhost development with my Visual Studio. Note that everything was working when released with Microsoft Release Management (MRM). I fixed my situation by changing the initial value that was a MRM reference.

My App.config initial value was the following:

  <connectionStrings>
    <add name="BDORA" connectionString="__token3__" />
  </connectionStrings>

Locally, the application execution could not interpret the token3 reference to its real value in Tokens.xml. To make it work on my localhost, I had to force the value from token3 in Tokens.xml into the App.config file.

Michael Fayad
  • 1,216
  • 1
  • 17
  • 38
0

My fix was suprisingly simple and another one of those obvious when you realise what you've done. I was manually building the configuration using .NET Core/Standard in the following fashion:

var configurationBuilder = new ConfigurationBuilder();
var root = configurationBuilder.Build(); 

and had forgotten to include the appsettings.json file that had my configuration settings in it

configurationBuilder.AddJsonFile("appsettings.json", false);

Once added, all started working once more.

The Senator
  • 5,181
  • 2
  • 34
  • 49
0

This helped me in the config. Although I did not change my framework Visual Studio 2022 seemed to insist upon it and once all those other changes.

in the startup section

supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"

I did not check other versions as I am under a deadline crunch on other stuff

dodrg
  • 1,142
  • 2
  • 18
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34085754) – adt Mar 27 '23 at 16:19
0

solved above error by add Configuration instead of just string path change

services.AddDbContext<ProductDbContext>(opts => {
                opts.UseSqlServer("ConnectionStrings:AppDataConnection");
            });

to

services.AddDbContext<ProductDbContext>(opts => {
                opts.UseSqlServer(Configuration["ConnectionStrings:AppDataConnection"]);
            });
Assay Khan
  • 56
  • 9
-1

In my Case, it is because of having more than one connection string in My Solution(with many project), the one for current project and the other one for a startup project. for more information please refer to Telerik report not working

here we have the same situation.