I have an ASP.Net MVC application which runs fine on my local development machine. But when deployed to IIS7 gives the following error when trying to log in:
Format of the initialization string does not conform to specification
starting at index 0
Most people who post this error resolve it by changing their connection string in some way. However my connection string on the local and deployed application are the same. The connection string is like this:
<add name="ApplicationServices" connectionString="Data Source=*server*\*instance*;Initial Catalog=*database*;Integrated Security=True;"
providerName="System.Data.SqlClient" />
What is causing this error in my case?
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
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.
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:
MSBuild web.config transforms not working by drneel
Replaceable token issue with config ConnectionString transforms by Francis
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:
In the Settings, select Configuration: Release
In the Settings,don't forget to paste your Connection String in the
"Remote Connection String" field
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.
Related
I am using Visual Studio 2015, Entity Framework v6, and SQL Server 2016 Express. In the past I created a database using a SqlConnection and SqlCommand and stuff the SQL into a string.
Now, I am teaching myself EF6 on Entity Framework Tutorial. On the simple code-first example (very simple), I literally copy and paste my code but still do not see the database created in SSMS. Neither does my code throw me any error.
Instead of pasting the code, I did a screenshot. I hope someone can point out what I am or the tutorial is missing.
[EDIT]
Following Sampath's suggestion, I end up getting the following error:
[EDIT - Solved, sort of]
I apply the same code to another machine of same setup and the code works. So I suspect there are some corruption in the SQL Server or perhaps some registry is incorrect. I uninstall EVERY SQL Server version and related tools, delete all folders and files manually, then freshly reinstall SQL Server Express 2016 and tools. Then my code works.
I don't see this as a solution, but if someone can suggest what may have cause this problem I will try to recreate it or post a real solution to it.
You have to give the connection string name on the web.config file as shown below.
Context :
public SchoolContext(): base("MySchoolDB")
{
}
App.config file
<add name="MySchoolDB" connectionString="Server=localhost;
Database=YourDBName;Trusted_Connection=True;" providerName="System.Data.SqlClient" />
You can get more details here : Database Initialization
You have to add entity to your database after configuring connection string, then DBContext will create database. Here is a connection string example:
<add name="Name" connectionString="Data Source=.; Initial Catalog=yourdbName; Integrated Security=True; MultipleActiveResultSets=True;"
providerName="System.Data.SqlClient" />
I've created an application that works perfectly on my PC using C# and SQL Server 2014.
My connection string is:
Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Youssef\Desktop\gone\gone\gone\gestion_incidents.mdf;Initial Catalog=BG_ONE;Integrated Security=True
I know that the connection string is the problem but how can I create a connection string that works on all computers?
Replace your database file path to a generic path that automatically get the location of the project data folder and it will not cause problem when you move project to another location. Follow below steps:
Step 1: First Add App_Data folder in your project and add database file there.
Step 2: Change your connection string replace path before the database file with |DataDirectory|
Example:
<connectionStrings>
<add name="ConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\nwind.mdb" providerName="System.Data.OleDb" />
</connectionStrings>
If you follow the above steps then your connection replace C:\Users\Youssef\Desktop\gone\gone\gone with |DataDirectory|. See modified connection string as below:
Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Youssef\Desktop\gone\gone\gone\gestion_incidents.mdf;Initial Catalog=BG_ONE;Integrated Security=True
I want to create an exe of my c# windows application project. I created the exe. But my problem is that I don't know how to include database with this exe. Because now am taking backup of my database and restore this backup to the system in which I want to install my exe. Database is created in sql server2012.
In my c# code connection string set to my system server name. so if I want to install it in another system, I need to change this connection string as server name of the system in which I want to install my exe. But it is not possible in all the time. so is there any method to done all these without changing in the code? I Created the exe using install shield.
Thanks.
Normally database settings should be configurable i.e. the user sets the settings through the application UI which are then written into a configuration file. If you give the settings through a configuration file with hardcoding, the exe need not be built everytime.
For getting the existing database, your application should be coded to create a blank database if the database in the server doesn't exist. The existing data can be imported through Administrator mode od your application or manually done in the SQL Server.
The following code shows how you can store connection strings in App.config file.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MyDBConnectionString" providerName="System.Data.SqlClient"
connectionString="Data Source=localhost;Initial Catalog=MySQLServerDB; Integrated Security=true" />
</connectionStrings>
</configuration>
Once you have saved your connection string in App.config file you can use System.Configuration.ConfigurationManager class to read this connection string in code.
ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];
ConnectionStringsSettings class provides properties to read connection string settings in your program as following code shows.
string name = conSettings.Name;
string providerName = conSettings.ProviderName;
string connectionString = conSettings.ConnectionString;
The above code has been taken from this link
For a detailed example check this article on CodeProject
What providerName should be used in Web.config file for MS SQL Compact Edition in MVC 4 in Visual Studio 2013?
Introduction
When I use:
<connectionStrings>
<add name="MusicStoreEntities"
connectionString="Data Source=|DataDirectory|MvcMusicStore.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
my website database works and Genre objects are retrieved from database and displayed
after I change of providerName to providerName="System.Data.SqlClient like this:
<connectionStrings>
<add name="MusicStoreEntities"
connectionString="Data Source=|DataDirectory|MvcMusicStore.sdf"
providerName=providerName="System.Data.SqlClient"/>
</connectionStrings>
I get:
So WHY I want to change anything?
Actual problem:
If I leave providerName="System.Data.SqlServerCe.4.0" when I want to add new controller by this menu:
I get:
but if I set providerName="System.Data.SqlClient the StoreManagerController is generated but the database does not work at all.
Question: What should I do to generate StoreManagerController? I follow this tutorial: http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-5 and link to the working project just before that step it: http://www.speedyshare.com/RGdqH/MvcMusicStore.zip
So bare in mind the "Mvc Music Store" was MVC 101 since the beginning of MVC. Given there have been a lot of changes, the chances of the documentation/walk-throughts being a little off are unfavorable. With that said:
The T4 template for the data-driven controller is obviously failing due to your connection string. This is most likely because SQL Express (in it of itself) has gone through a lot of cycles and is now following LocalDB. Given you're using MVC4, it's probably a good time to upgrade, and this means you can have all the benefits of SQL Express (including the System.Data.SqlClient provider) without the possible headaches from previous revisions.
With that said, change your connection string to use the new format:
<connectionStrings>
<add name="MusicStoreEntities"
connectionString="Data Source=(localdb)\v11.0;Integrated Security=true;AttachDbFileName=|DataDirectory|MvcMusicStore.mdf"
providerName="System.Data.SqlClient" />
</connectionStrings>
And (probably) re-run your DatabaseInitializer (if I recall correctly, this should happen automatically since it's using EF. If not, you can go to your Package Manager console and run Update-Database).
This now gives you the provider the Controller template wants while still keeping it as a local (dev) database.
Delete the database your local server(make sure u do a back up off data / your seed method has relevant data needed)
Delete all migration class files besides the configuration file from your Migrations Folder. (migrations need to be enabled in order for this to work "Enable migrations" )
In the Package Manage Console type the following command: “ add-migration update ”. Once that is successful
Type in “Update-Database” in the Package Manager Console.
Your Database will be recreated without ANY data.
I have the same problem for MVC5, so i change the web.comfig connection string for:
<add name="MusicStoreEntities" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|MvcMusicStore.mdf Catalog=MvcMusicStore" providerName="System.Data.SqlClient"/>
it works for me!
I have a c# assembly that uses the app.config to store its database connection string. When debugging the application I noticed that the connection to the database kept failing because the ConfigurationManager kept returning the machine.config connection string:
data source=.\SQLEXPRESS; Integrated Security;....
I added <clear/> before my connection string in the app.config and it fixed the issue on my dev machine. The problem returned when I deployed it to production. Can someone tell me how I can stop the machine.config connection string from being used?
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[0].ConnectionString);
<connectionStrings>
<clear/>
<add name="VersionConnectionString"
connectionString=" Data Source=localhost;Initial Catalog=VersionInfo;User ID=user;Password=password"
providerName="System.Data.SqlClient" />
</connectionStrings>
UPDATE
The following still gives me the machine.config connection string?!
Configuration appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
string dllConfigData =
appConfig.ConnectionStrings.ConnectionStrings[0].ConnectionString;
When using connection strings in a DLL, you need to add them to your exe's app.config as well, using the exact same name for the setting. Then, you can change the connection string in the exe's .config file and the DLL will pick it up automatically when loaded.
This is probably the only way you can have working custom connection strings in the app.config file when your DB persistence layer is implemented in a separate DLL. Don't even ask me how much time it took me to research and debug this.
I know this is an older question, but I was having the same problem today. The problem was that the app.config that I added to my project wasn't being copied to the output directory. To fix this, right click on the app.config and select Properties. Then change Build Action to Content.
Hope this helps!
Try getting an instance of your app.config file as a Configuration object:
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var myConnString = config.ConnectionStrings["VersionConnectionString"].ConnectionString;
This bypasses the machine config file completely.
You should be getting your connection string by NAME instead of INDEX - that will ensure you're getting what you're asking for.
Try
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["VersionConnectionString"].ConnectionString);