208

I have an ASP.NET application which runs fine on my local development machine.

When I run this application online, it shows the following error:

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

Why is this appearing, and how can I fix it?

TylerH
  • 20,799
  • 66
  • 75
  • 101
G.S Bhangal
  • 3,060
  • 4
  • 23
  • 48
  • 2
    You probably make a bad sql call/connection. If you google it http://blogs.msdn.com/b/jongallant/archive/2009/05/02/solution-to-the-format-of-the-initialization-string-does-not-conform-to-specification-starting-at-index-0-exception.aspx – Aristos Nov 23 '11 at 13:41
  • 1
    The most voted answer at http://stackoverflow.com/questions/9040266/how-to-fix-error-format-of-the-initialization-string-does-not-conform-to-speci seems to be more accurate than the most voted one here: although the connection string you use in development may work, publishing needs to provide a different connection string appropriate for production, and this can fail. E.g. the person that asked that question found the connection to be "$(ReplacableToken_mcn-Web.config Connection String_0)," which indicates the replacement that should have happened as part of publish didn't happen. – divega Apr 05 '17 at 17:49

31 Answers31

249

Check your connection string. If you need help with it check Connection Strings, which has a list of commonly used ones.

Commonly used Connection Strings:

SQL Server 2012

Standard Security

Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;

Trusted Connection

Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

Connection to a SQL Server instance

The server/instance name syntax used in the server option is the same for all SQL Server connection strings.

Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;
Password=myPassword;

SQL Server 2005

Standard Security

Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;

Trusted Connection

Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

Connection to a SQL Server instance

The server/instance name syntax used in the server option is the same for all SQL Server connection strings.

Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;Password=myPassword;

MySQL

Standard

Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

Specifying TCP port

Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

Oracle

Using TNS

Data Source=TORCL;User Id=myUsername;Password=myPassword;

Using integrated security

Data Source=TORCL;Integrated Security=SSPI;

Using ODP.NET without tnsnames.ora

Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)));User Id=myUsername;Password=myPassword;
Hanlet Escaño
  • 17,114
  • 8
  • 52
  • 75
  • 11
    My problem is not related for wrong ConnectionString. I am able to connect from my developer machine to production db and use it. When I deploy same settings I am getting this error. Do you have idea what could go wrong? Thanks – Denis Besic Jun 24 '13 at 20:22
  • 3
    There could be many things going on Denis. First, is your server and your dev machine in the same network? What kind of login are you using? Are you using any kind of proxy, are you behind a firewall in the prod server? Are getting into any VPN? – Hanlet Escaño Jun 24 '13 at 20:40
  • 3
    I am not sure who is responsible, is it Visual Studio, Tool for Web Deployment or hosting smarterasp.net. When application was deployed something changed my connectionstring. I accessed directly to web.config and manually update it and it works. Thanks for your time with +1 ;) – Denis Besic Jun 24 '13 at 21:08
  • It's not working, I have tried to put the correct connection string. –  Sep 12 '17 at 09:38
  • Can any one see this question? https://stackoverflow.com/questions/46167682/rest-api-not-working-with-mysql –  Sep 12 '17 at 09:39
46

This might help someone.. My password contained a semicolon so was facing this issue.So added the password in quotes. It was really a silly mistake.

I changed the following :

<add name="db" connectionString="server=local;database=dbanme;user id=dbuser;password=pass;word" providerName="System.Data.SqlClient" />

to

<add name="db" connectionString="server=local;database=dbanme;user id=dbuser;password='pass;word'" providerName="System.Data.SqlClient" />
Abdul Rehman Sayed
  • 6,532
  • 7
  • 45
  • 74
  • Yes this was my issue to my password had a ; so adding the '' solved it –  May 20 '20 at 16:34
  • Lifesaver! I was going nuts trying to figure out what in the world was wrong with the new Azure solution I was trying to get up and running and never even noticed the password I had autogenerated using a password manager picked a semicolon as one of the characters. I'd upvote this 10x if I could. Thanks! :) – dhughes May 25 '21 at 22:16
12

Set the project containing your DbContext class as the startup project.

I was getting this error while calling enable-migrations. Even if in the Package Manager Console I selected the right Default project, it was still looking at the web.config file of that startup project, where the connection string wasn't present.

Mart
  • 5,608
  • 3
  • 32
  • 45
  • 4
    +1. I think the starting project should be the project that contains the connection string. This was my problem. Thanks for your suggestion of Startup project. – Tchaps Jul 24 '16 at 09:32
10

Check your connection string like I forget to add services.AddDbContext<dbsContext>(options => options.UseSqlServer("Default"));

It causes the error and here when I add Configuration.GetConnectionString, then it solves the issue

like now the connection is:

services.AddDbContext<dbsContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Default")));

works fine (This problem is solved for .net core)

pushkin
  • 9,575
  • 15
  • 51
  • 95
user8099291
  • 279
  • 1
  • 3
  • 12
4

Make sure that your connection string is in this format:

server=FOOSERVER;database=BLAH_DB;pooling=false;Connect Timeout=60;Integrated Security=SSPI;

If your string is missing the server tag then the method would return back with this error.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
DBNoob
  • 41
  • 1
4

I had the same problem. Locally the site ran fine, but on azure it would fail with the message above.

turns out the problem was setting the connectionstring in the ctor, like this:

    public DatabaseContext() 
    {
        Database.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
    }

Does NOT work, this will:

    public DatabaseContext() : base("db")
    {
    }

Beats me..

Flores
  • 8,226
  • 5
  • 49
  • 81
  • This is such a strange fix but just saved my life too. For the record, "db" is the name of the key of your connection string in the config file (mine was not "db") – mike Dec 06 '19 at 00:16
  • Fixed it for me as well. I had a valid connectionstring but setting it manually threw the "Format of the initialization string does not conform to specification starting at index 0". – Sardaukar Mar 30 '20 at 07:11
3

Referencing the full sp path resolved this issue for me:

var command = new SqlCommand("DatabaseName.dbo.StoredProcedureName", conn)
anon
  • 31
  • 1
3

I removed &quot at the end of the connection string and it worked

Instead of

App=EntityFramework&quot;

Used

App=EntityFramework;

Set DefaultConnection as below

<add name="DefaultConnection" connectionString="data source=(local);initial catalog=NamSdb;persist security info=True;user id=sa;password=sa;MultipleActiveResultSets=True;App=EntityFramework;" providerName="System.Data.SqlClient" />

Note : In connectionString Do not include :
|x| Metadata info : "metadata=res://*/"
|x| Encoded Quotes : """

Sujay U N
  • 4,974
  • 11
  • 52
  • 88
3

Got this problem with SQLite in aspnetcore. I wrote

 "DefaultSQLiteConnection": "MyBlog.db"

instead of

"DefaultSQLiteConnection": "Data Source = MyBlog.db"
DevCisse
  • 130
  • 2
  • 7
2

I solved this by changing the connection string on the publish settings of my ASP.NET Web Api.

Check my answer on this post: How to fix error ::Format of the initialization string does not conform to specification starting at index 0::

Community
  • 1
  • 1
ndarriulat
  • 749
  • 1
  • 9
  • 11
2

I had the same error. In my case, this was because I was missing an closing quote for the password in the connection string.

Changed from this

<add name="db" connectionString="server=local;database=dbanme;user id=dbuser;password='password" providerName="System.Data.SqlClient" />

To

<add name="db" connectionString="server=local;database=dbanme;user id=dbuser;password='password'" providerName="System.Data.SqlClient" />
Amer Bashoeb
  • 301
  • 2
  • 4
2

In my case the problem was that on the server, a different appsettings.json file was used by the application.

AGuyCalledGerald
  • 7,882
  • 17
  • 73
  • 120
2

In my case, I got a similar error:

An unhandled exception was thrown by the application. System.ArgumentException: Format of the initialization string does not conform to specification starting at index 91.

I change my connection string from:

Server=.;Database=dbname;User Id=myuserid;Password=mypassword"

to:

Server=.;Database=dbname;User Id=myuserid;Password='mypassword'"

and it works, I added single quotes to the password.

YuKeung
  • 33
  • 3
2

In my case the problem was in the encoding of the connection string.

In local it worked without problems, but when installing it in a production environment it showed this error.

My application allows to set the connection string using a form and when the connection string was copied from local to production, invisible characters were introduced and although the connection string was visually identical at the byte level it was not.

You can check if this is your problem by following these steps:

  1. Copy your connection string to Notepad++.
  2. Change the codification to ANSI. In Menu Encoding>Encode to ANSI.
  3. Check if additional characters are included.

If these characters have been included, delete them and paste the connection string again.

2

My generated password contained few characters that were valid in AWS RDS, but for some reason my .NET app could not handle it. The solutions that worked for me, was to generate new password without characters like ` (backtick).

rePhat
  • 314
  • 3
  • 9
2

In my case, it was...

index: 58

but what ended up happening was the password generated had a single quote in it. SQL Server accepted that, but my connection string had trouble parsing it. Hope this saves someone at least a few minutes, because it cost me an hour to two before I relized what was going on. cheers

jgritten
  • 915
  • 1
  • 11
  • 20
2

Hope this helps someone in the future. My connection string had blank values wrapped in double quotes, e.g. User ID="". So to resolve it I had to escape the double quotes.

Dim ConString = "Data Source=MYSERVER;User ID="";Initial Catalog=Northwinds..."

ConString = ConString.Replace("""", """""")
J. Minjire
  • 1,003
  • 11
  • 22
1

This also happens when you copy a web page from one solution to another, then you run your solution and find out that it has a different connection string name in the webconfig. Then you carelessly change the name of the connection string in the properties panel in the design view of the page.

Better to just change it in the code portion instead of the design.

Gellie Ann
  • 439
  • 1
  • 6
  • 10
1

I had the same problem and finally I managed to resolve it in the following way:

The problem was in the connection string definition in my web.config.

<add name="DefaultConnection" connectionString="DefaultConnection_ConnectionString" providerName="System.Data.SqlClient"/>

The above worked perfect locally because I used a local Database when I managed users and roles. When I transfered my application to IIS the local DB was not longer accessible, in addition I would like to use my DB in SQL Server. So I change the above connection string the following SQL Server DB equivalent:

<add name="DefaultConnection" connectionString="data source=MY_SQL_SERVER; Initial Catalog=MY_DATABASE_NAME; Persist Security Info=true; User Id=sa;Password=Mybl00dyPa$$" providerName="System.Data.SqlClient"/>

NOTE: The above, also, suppose that you are going to use the same SQL Server from your local box (in case that you incorporate it into your local web.config - that is what exactly I did in my case).

ahsteele
  • 26,243
  • 28
  • 134
  • 248
Andreas Venieris
  • 452
  • 3
  • 15
1

My problem was I added database logging code to my constructor for a DB object, and this seemed to cause havoc on my azure deployment profile.

FYI - I simplified this example, in the real code this was turned off in production (but still in the code)

public class MyDB : DbContext
{
    public MyDB()
    {
         this.Database.Log = x => { Debug.WriteLine(x); };
    }
}
Aaron Sherman
  • 3,789
  • 1
  • 30
  • 33
1

I had typo in my connection strings "Database==PESitecore1_master"

<add name="master" connectionString="user id=sa;password=xxxxx;Data Source=APR9038KBD\SQL2014;Database==PESitecore1_master"/>
Azadeh Khojandi
  • 3,806
  • 1
  • 30
  • 32
1

I had the same issue, came to find out that the deployment to IIS did not set the connection strings correctly. they were '$(ReplacableToken_devConnection-Web.config Connection String_0)' when viewing the connection strings of the site in IIS, instead of the actual connection string. I updated them there, and all worked as expected

jason
  • 91
  • 1
  • 4
1

I copied and pasted my connection string configuration into my test project and started running into this error. The connection string worked fine in my WebAPI project. Here is my fix.

var connection = ConfigurationManager.ConnectionStrings["MyConnectionString"];
var unitOfWork = new UnitOfWork(new SqlConnection(connection.ConnectionString));
LordWilmore
  • 2,829
  • 2
  • 25
  • 30
Kris Kilton
  • 938
  • 9
  • 11
1

My problem wasn't that the connection string I was providing was wrong, or that the connection string in the app.config I thought I was using was wrong, but that I was using the wrong app.config.

Jeff Dege
  • 11,190
  • 22
  • 96
  • 165
1

For the one other unfortunate soul that is managing a legacy webforms application that uses an inline sqldatasource, along with connection strings stored in web.config, then you may get this error if you access your connection string like <%APSDataConnectionString%> instead of <%$ ConnectionStrings:MyConnectionString %>. This happened to us when upgrading .NET from 3.5 to 4.x.

<asp:DropDownList ID="ddl" runat="server" DataSourceID="SqlDataSource1"
  DataTextField="value" DataValueField="id"></asp:DropDownList>                
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
  SelectCommand="select id, value from a_table">
</asp:SqlDataSource>
Michael
  • 2,825
  • 3
  • 24
  • 30
1

I had this error too and was able to solve it as follows: I previously wrote the connectionstring to the appsettings.json into a section that I created (ConnectionsStrings (notice the extra "s") and tried to connect to my database which caused the error. It was an ASP.NET CORE application and so I wanted to connect to it with the .GetConnectionString Method (details on that here). It seems that this method implicitly searches for a connectionstring in the "ConnectionStrings"-section, which didn't exist. When I changed/corrected it to "ConnectionStrings" it worked as expected.

clueless
  • 11
  • 2
1

I had the same problem spent an entire day. The error indicates Connection string issue for sure as index 0 is connection string. My issue was in the Startup class. I used:

var connection = Configuration["ConnectionStrings:DefaultConnection"];
services.AddControllersWithViews();
services.AddApplicationInsightsTelemetry();
services.AddDbContextPool<Models.PicTickContext>(options => options.UseSqlServer("connection"));`

which is wrong instead use this:

services.AddControllersWithViews();
services.AddApplicationInsightsTelemetry();
services.AddDbContextPool<Models.PicTickContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

and my problem was solved.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • 1
    That is most likely because you passed the string "connection" instead of your variable connection. – rePhat Sep 01 '21 at 12:06
1

I had this error and it turned out that the cause was that I had surrounded a (new) tableadapter with a Using/End Using. Just changing the tableadapter to stay live for longer (duration of class in my instance) fixed this for me. Maybe this will help someone.

Tim F.
  • 268
  • 2
  • 8
0

Sometimes the Sql Server service has not been started. This may generate the error. Go to Services and start Sql Server. This should make it work. enter image description here

  • If Sql Server Express is not started you get Server Instance Exception not the one which OP described in his question. – Rohan Rao May 08 '20 at 07:58
0

As I Know, whenever you have more than 1 connection string in your solution(your current project, startup project,...), you may confront with this error

this link may help you click

0

You have to use the exact same name of the class that you derived from DbContext.

example: Note that the class name is: "CreativeFormDbContext"

  public class CreativeFormDbContext : DbContext
    {
        public DbSet<Person> Persons { get; set; }

      
    }

app.config file: And the name property is "CreativeFormDbContext"

<connectionStrings> 
    <add name="CreativeFormDbContext" providerName="MySql.Data.MySqlClient"
        connectionString="Server=localhost;Database=creativeform;Uid=username;Pwd=pass"/>
  </connectionStrings>
Kadir
  • 1