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

人盡茶涼 提交于 2019-11-27 02:08:15
Guru

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

Web.config :

<connectionStrings>
    <add name="TestDataConnectionString" connectionString="Data Source=.\SQLExpress;Initial Catalog=TestData;User ID=satest;Password=satest"
     />
  </connectionStrings>

In aspx.cs Page the code must be written in the below format :

SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["TestDataConnectionString"].ToString());

Web Deployment tool created wrong line in config when I checked Enable CodeFirst Migrations check-box.

In my case I accidentally wrote "password:" instead of "password=" in my conn string

H. West

Check to make sure the credentials are correct for the connection string in Web.config. Mine was missing the password for the account with permissions to the database.

Shriroop

I encountered the same error. In my case it was the config transform not working properly. There is an issue with config transforms when it comes to connection strings. Some reference:

Also one can write the code in the aspx.cs page as

using (IDbConnection dbConnection =
       new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString))
       {
           // TODO: Write SQL Stored Procedures or SQL Statements using Dapper
       }

For those who would like to find out more about Dapper.

Hope this helps.

If you have been using the Visual Studio Publish Wizard for deployment and checked the Execute Code First Migrations check box in Settings, a new ConnectionString is automatically added to the Server Web.config file, similar t to the 2nd line below:

<add name="LCWeb3Context" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=LCWeb3;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\LCWeb3.mdf" providerName="System.Data.SqlClient" />
<add name="LCWeb3Context_DatabasePublish" connectionString="LCWeb3Context_DatabasePublish.ConnetionString" providerName="System.Data.SqlClient" />

First, notice the added connection string contains "ConnetionString": I think it should be "ConnectionString"! But that's not the solution.

To avoid the "Format of the initialization string does not conform to specification starting at index 0" error, do the following in the Publish Wizard:

  1. In the Settings, select Configuration: Release
  2. In the Settings,don't forget to paste your Connection String in the "Remote Connection String" field
  3. In the Settings, check Execute Code First Migrations

When doing the above, the connection string added to the Server Web.config reads:

<add name="LCWeb3Context_DatabasePublish" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\LCWeb3.mdf;Initial Catalog=LCWeb3;Integrated Security=True" providerName="System.Data.SqlClient" />

and the "Format of the initialization string does not conform to specification starting at index 0" error no longer occurs.

I has the same issue, when I use command: "Update-Database" in Package Manager Console.

To Fix, make sure set startup project to the project which you want to do update.

E.g. I got Db project and Web project, make sure set startup project on Db project, when run "Update-Database", otherwise, it will try to search inside Web project.

The permissions on the SQL server were not correctly set up. I have now resolved this by properly setting up the server permissions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!