Connection to SQL Server Database inside Visual Studio connectionString error - c#

I've import my SQL 2005 database to my project and i am trying to connect to the database using the web.config
But I've this error when I add the connectionString to the web.config.
<add name="strConn" connectionString="Data Source=.\sqlexpress;Initial Catalog=intranet_db;Integrated Security=True"
providerName="System.Data.SqlClient"/>
HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.
Detailed Error Information:
Module IIS Web Core
Notification Unknown
Handler Not yet determined
Error Code 0x80070032
Config Error The configuration section 'add' cannot be read
because it is missing a section declaration
Config File \?\C:\Users\user\Documents\Visual Studio
2012\Projects\test\test\web.config
Requested URL localhost:64198/
Physical Path
Logon Method Not yet determined
Logon User Not yet determined
Request Tracing Directory
C:\Users\user\Documents\IISExpress\TraceLogFiles\
Am I doing it wrong? What should I add in the web.config?
Thanks.

Please make sure you put the "add" tag in the correct place. I'm able to reproduce your error when I place it i wrong section. Your web.config file should look like this:
<configuration>
<connectionStrings>
<add name="strConn" connectionString="Data Source=.\sqlexpress;Initial Catalog=intranet_db;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>

it should be something like this
<connectionStrings>
<add name="connectionString" connectionString="Provider=SQLOLEDB.1;Data Source=dbhostname;Persist Security Info=True;Password='password';User ID='username';Initial Catalog=dbname" />
</connectionStrings>

have a look at http://www.connectionstrings.com/ it has examples of what they should be.
looking at you connection string make sure the instance name ./SqlExpress is correct.
A quick test would be to load sql manager and connect with the ./SqlExpress
Standard Security
Server=myServerAddress;Database=myDataBase;User Id=myUsername; Password=myPassword;
Trusted Connection
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
to give you a better answer would require knowing how you want to connect to the database.

Related

I have two web.config files. Which one should I use?

I recently built an application using visual studio express. This application was built using a local DB provided by VS express. I now have a subscription (free trial) to Microsoft Azure. What changes do I need to make to my project in order to point my project to the newly created Azure DB?
I also noticed I have 2 web.config files. one in the root directory and one in the views folder.
I believe I have to make an update to my connection string, but in a specific config file or both?
Here is my root directory web.config connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source= LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MvcMovie-20160922101748.mdf;Initial Catalog=aspnet-MvcMovie-20160922101748;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
and my (views) web.config connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MvcMovie-20130603030321.mdf;Initial Catalog=aspnet-MvcMovie-20130603030321;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"/>
Any help would be highly appreciated.
I believe only one web.config with connection strings should be enough. Keep the one in the root.
Download Azure DB connection strings from the Azure portal and replace connectionString values in the web.config file with the Azure DB connection string(s).
You might also need to whitelist your machine's IP address in the Azure Portal to allow connections to Azure DB.
You only need to change the web.config on the root folder as thats the one used by the application.
And you are right, you need to point the connection string to your Azure database.
You definitely only need to update the one web.config in the root of the application with the connection string. The other web.config in the views folder is needed for your views to use html helpers and to make routing work as you expect in an mvc application so do not remove it.

host a database website with entity framework in the production server

I developed an asp.net website (using webforms) and for database connection I used Entity Framework. On my local computer everything is working fine. Now I try to host it I am having a problem. I have copied my files to the production server and attached my SQL Server database and set my connection string to:
<add name="ConnectionString"
connectionString="Data Source=SQL5003.Smarterasp.net;Initial Catalog=DB_9C4561_WFormBlog;User Id=DB_9C4561_WFormBlog_admin;Password=mydbpwd"
providerName="System.Data.EntityClient" />
but when I test my website am getting error:
Keyword not supported: 'data source'
When I removed "data source", it started complaining of "Initial catalog" keyword not supported.
Please help me out. You can refer me step by step tutorial or material that can put me through. Thanks in advance!
Try the below format, I have my database on smarterasp.net and it works well
<connectionStrings>
<clear />
<add name="ConnectionString" connectionString="Persist Security Info=False;database=DB_9C4561_WFormBlog;server=SQL5003.Smarterasp.net;user id=DB_9C4561_WFormBlog_admin;password=mydbpwd;Current Language=English;Connection Timeout=60;" />
</connectionStrings>

How to connect ASP.NET config to SQL Server instead of SQL Server Express?

I configured my SQL Server database using aspnet_regsql.exe. Everything worked great. I then added the new connection string in my web.config file:
<connectionStrings>
<clear />
<remove name="LocalSqlServer" />
<add name="SQLConnectionString"
connectionString="Provider=ProvName;Server=Sname;Database=dbName;Uid=user; Pwd=pWord" />
</connectionStrings>
When I try to access the database from the website I get an error:
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: The connection name 'LocalSqlServer' was not found in the applications configuration or the connection string is empty.
Source Error:
I am running this site on IIS via Visual Studio 10. Any ideas what steps I'm missing?
<add name="LocalSqlServer" connectionString="Data Source=.;Initial Catalog=DBName;Uid=user;Pwd=pWord" provider="System.Data.SqlClient"/>
Something like above should work.
Your code is looking for a connection string named "LocalSqlServer", yet you are adding a connection string named "SQLConnectionString".
Change this to:
<add name="LocalSqlServer" connectionString="Provider=ProvName;Server=Sname;Database=dbName;Uid=user; Pwd=pWord" />
Do a search for the phrase "LocalSqlServer" in all your config and code (.cs?) files. It is being references somewhere else...not just the connection-strings config area.
The "name" attribute is used in other places in the code to look up the actual connection string.
I like Agent Ransack for looking in files for a specific string.
When you find the other reference, you can replace it with "MySuperSpecialConnectionStringName" (as seen below, an alteration of your original code).

ConnectionString Error on publish in web server

I have a project in asp.net MVC with Entity Framework and my connectionsString at web.config
<add name="DefaultConnection" connectionString="data source=localhost;Initial Catalog=telexpo_bd;Integrated Security=SSPI;User Id=telexpo_bd;Password=hey76jdhdyU;" providerName="System.Data.SqlClient" />
<add name="telexpoEntities" connectionString="metadata=res://*/telexpoEF.csdl|res://*/telexpoEF.ssdl|res://*/telexpoEF.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost;initial catalog=telexpo_bd;integrated security=True;user id=telexpo_bd;password=hey76jdhdyU;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
this work perfectly in my PC when I emulate via Visual Studio but when I publish my application in web server doesn't work. Give the error page built by me but I was connected my web server company and after long talks they give to me this error:
Configuration Error
Description:An error occurred during the processing of a configuration file required to service this request. Please review the
specific error details below and modify your configuration file
appropriately.
Parser Error Message: The connection name 'LocalSqlServer' was not found in the applications configuration or the connection string is
empty.
Source Error:
Line 255:
Line 256:
So, my web.config just have 193 lines and the error in web.config is at line 259!??
I was investigate by 'LocalSqlServer', "AspNetSqlMembershipProvider" and doesn't found nothing...
After some investigation across the web I found that one which generated the AspNetSqlMembershipProvider and LocalSqlServer was the machine.config at C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config so I was thinking at server was the same but I can't change it because I have a dedicated server.
After time and tried a lot of things...but I can't find the solution about it... they insist the problem is about connectionstring. How could be?
Can someone give some suggestions please?
Yes it is because of connection string, that it cannot find the connection string value under the name 'LocalSqlServer'. Try changing the connection string that matches the server in the app.config file. If you have separate projects for web and entity then include the connection string in the web.config file of web application which will resolve.

Configuring local db location in Entity Framework

I am trying to configure a different location for my local db.
I read an article on configuring connection strings and it suggests I can replace |DataDirectory| in the web config with ~/MyDirectory where ~ is the web app root. So I have input the connection string as:
<connectionStrings>
<add name="Context" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=Context-20130829161219;Integrated Security=SSPI;AttachDBFilename=~/MyDirectory/Context-20130829161219.mdf"
providerName="System.Data.SqlClient" />
Unfortunately, when I run the app it doesn't seem to create the db at all. I can imagine it's only something very small that I am doing wrong but I would appreciate if someone can point me in the right direction.
Many Thanks
Lets try with below string it will help you
<add name="Context" connectionString="metadata=res://*/Models.Datacontext.csdl|res://*/Models.Datacontext.ssdl|res://*/Models.Datacontext.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDb)\SQLInstance;initial catalog=Context-20130829161219;persist security info=True;user id=sa;password=Yourpassword;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
or replace your Source=(LocalDb)\SQLInstance;initial with Source=.\SQLInstance;initial
Try running the Update-Database cmdlet from the package manager console - you should at least see some output indicating why it doesn't work.

Categories