Connection string in web.config not working - asp.net

I have a very basic ASP.NET web application that is connecting to a SQL Server database (I am using SQL Server 2008 Express). The problem I am having it is very odd: if I set the connection string for the SqlConnection object directly in code, the application works fine, but if I move my connection string to the web.config file, the application fails.
I show you both the variable and the connection section in the web.config to see if you detect any error:
In the variable:
// works fine
string cs = "data source=.; database=myDataBase; integrated security=SSPI";
SqlConnection conn = new SqlConnection(cs);
Now in the web.config:
<configuration>
<connectionStrings>
<add name="myCS"
connectionString="data source=.; database=myDataBase; integrated security=SSPI"
providerName="System.Data.SqlClient" />
</connectionStrings>
...
// Now, if I do this in C#, it does not work:
string cs = ConfigurationManager.ConnectionStrings["myCS"].ConnectionString;
SqlConnection conn = new SqlConnection(cs);
It tells me that there is already a database with that name. Now, I have read things everywhere. Some guy said that instead of using "database" in the connectionString, I could use "initial catalog" and all would be fine. Well, it wasn't fine... I have the same problem... Does anyone know what I am doing wrong? Why is working in one place and not in another? If the connection string was wrong, it should fail in both places, and it is only crashing in the webConfig file... Thanks very much...

If you are running this from Visual Studio, have you tried stopping the Dev Server (running in your system tray) and trying? the Web.config file is normally cached and could cause issues.

Related

'Connection string "SQLServerConnectionString" was not found.' - Using Azure DB

Basically, I'm busy with a university project and this is the error I keep running into. I've tried multiple solutions from Microsoft's sites, but got nothing.
I added the connection string to my web.config, however, it still doesn't come through for me.
The connection link I took directly off of Azure's connection strings, on the DB I created.
<connectionStrings>
<add
name="SQLServerConnectionString"
connectionString= "Server=tcp:[dblink],1433;Initial Catalog=SACLADB;Persist Security Info=False;User ID=[id];Password=[pass];MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
providerName="System.Data.SqlClient" />
</connectionStrings>
The code used to attempt and connect the database which should use the web.config connection string.
#{
ViewBag.Title = "Papers";
var db = Database.Open("SQLServerConnectionString");
var selectQueryString = "Select * FROM Papers ORDER BY Author";
}
Found the error of my ways;
I was editing the wrong Web.Config. I wasn't editing the one in the root folder where it reads the connection strings, however, all issues are now fixed.

A network-related or instance-specific error occurred while establishing a connection to sql server error 26 on stimulsoft Report

I use this connection string in my asp.net
<add name="DB_PersonnelEntities"
connectionString="metadata=res://*/Models.Model.csdl|res://*/Models.Model.ssdl|res://*/Models.Model.msl;provider=System.Data.SqlClient;provider connection string="data source=REGAPP\SQL2014;initial catalog=DB_Personnel;user id=personnel;password=qwe123!;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
and this code for StimulSoft
string connectionstring = ConfigurationManager.ConnectionStrings["DB_PersonnelEntities"].ConnectionString;
string serverlocation = HttpContext.Current.Server.MapPath(string.Empty);
StiReport mystireport = new StiReport();
mystireport.Dictionary.Databases.Add(new Stimulsoft.Report.Dictionary.StiSqlDatabase("DB_PersonnelEntities", connectionstring));
mystireport.Load(serverlocation + "\\Report\\RptPersonnel.mrt");
mystireport.Dictionary.Variables["pid"].Value = id.ToString();
StiWebViewer1.Report = mystireport;
mvList.SetActiveView(vwReport);
The connection string works properly in all SQL connections of the program, except for Stimulsoft Report with the code shown above.
Note: this code and connection work properly for StimulSoft SQL connection and other sql connections on my PC and run from Visual Studio, but when the program is published on the server, the StimulSoft SQL connection shows an error 26.
I changed the connection string like this
<add name="Conn"
connectionString="Data Source=REGAPP\SQL2014;Initial Catalog=DB_Personnel;User Id=personnel;Password=qwe123!"
providerName="System.Data.SqlClient" />
and changed instance name / disabled firewall / checked user permission for procedure.
THANKS FOR HELP
The connection string you showed is an Entity Framework connection string, which I'm pretty sure a third-party reporting tool won't be able to use.
But embedded in that EF connection string is a "raw" ADO.NET connection string - have you tried that??
<add name="Report_PersonnelEntities"
connectionString="Data Source=REGAPP\SQL2014;Initial Catalog=DB_Personnel;User id=sa;password=qwe123!;MultipleActiveResultSets=True;"
providerName="System.Data.SqlClient" />
Word of warning: it is generally recommended NOT to use the sa (system administrator) account which can do literally everything on your server for any programmatic database work - especially not for reporting! Create your own reporting user in SQL Server and use that instead of the all-powerful system admin account!
The StimulSoft Save connection string (in Report.mrt file) That created on my PC. When I Publish My Program, The StimulSoft report Use the connection string embedded in Report.mrt file NOT the web.config.

.NET 4.0 Table Adapter connection string issue

I have a weird issue occuring when using table adaper connection strings in a .net 4.0 web application.
I'm using the same connection string name with different server/credentials for the test
and production environments. I comment out the connection string that I'm not using(see example
below) I'm having an issue with the table adapter not using the current connection string that is uncommented in the web config file. For example, I make any changes against the test database using the test connection string. However, when I uncomment the production connection string(and comment the test connection string) and deploy to the production server, the test connection is still being used. Some of the connections are not defined in the code behind - some are grids that are bound to objectdatasources. Can anyone offer any advice on to fix this problem?
<!-- Test Conn -->
<add name="Connection1" connectionString="Data Source=server1; Initial Catalog=database1; User ID=username1; Password=password1" providerName="System.Data.SqlClient"/>
<!-- Production
<add name="Connection1" connectionString="Data Source=server2; Initial Catalog=database1;User ID=username2; Password=password2" providerName="System.Data.SqlClient"/>
-->
Try restarting IIS after you deploy.

Keyword not supported: 'server'

I've been trying to edit my connection string for uploading my website to a server.
I am not really experienced with this. I got this exception: the Keyword not supported: 'server'.
Here is my connection string:
<add name="AlBayanEntities" connectionString="Server=xx.xx.xxx.xxx,xxxx;Database=AlBayan;Uid=bayan;Password=xxxxx;" providerName="System.Data.EntityClient" />
I've tried embed this string into my old connection string which works very well locally, but it didn't fit : S
For Entity Framework (database-first or model-first; when you have a physical EDMX model file) you need to use a special type of connection string which is quite different from the straight ADO.NET connection strings everyone else has mentioned so far...
The connection string must look something like:
<add name="testEntities"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=(local);initial catalog=test;integrated security=True;multipleactiveresultsets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
Inside this connection string, you'll find the provider connection string= attribute which is basically your ADO.NET connection string:
provider connection string="data source=(local);initial catalog=test;integrated security=True;multipleactiveresultsets=True;App=EntityFramework""
So here, you need to change your server name and possibly other settings.
data source=.... stands for your server (you can also use server=.....)
initial catalog=..... stands for your database (you can also use database=....)
In MVC5 using EntityFramework 6.xx and Code First Approach
I had the same problem and I solved this by modifying my providerName
from
providerName="System.Data.EntityClient"
to
providerName="System.Data.SqlClient"
This exception is thrown on azure websites when you store the connection string in the App Service itself (on the 'Application Settings' blade).
If the connection string is an Entity Framework connection string the quotes will be encoded as " by default in your web.config file.
You need to change these back to actual quotes so the connection string can be parsed properly.
I always either run a connection wizard to build my string or I use connectionstrings.com.
Assuming SQL Server:
Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
Comparing to yours it is very different.
Server=xx.xx.xxx.xxx,xxxx;Database=AlBayan;Uid=bayan;Password=xxxxx;
Same happened to me when using Rider IDE for a .net core 2.2 project.
As Rider automatically sets up Sqlite in Startup.cs in the "ConfigureServices" method.
By changing
services.AddDbContext<ApplicationDbContext>(options =>
options.**UseSqlite**(
Configuration.GetConnectionString("DefaultConnection")));
into
services.AddDbContext<ApplicationDbContext>(options =>
options.**UseSqlServer**(
Configuration.GetConnectionString("DefaultConnection")));
issue was resolved.
Of course, first you have to install nuget packages for EF SQLServer and add the connection string to appsettings.json.
Try this
<add name="AlBayanEntities" connectionString="Data Source=xx.xx.xxx.xxx,xxxx;Initial Catalog=AlBayan;User Id=bayan;Password=1abcd;" providerName="System.Data.EntityClient" />
EntityConnectionStringBuilder bb = new EntityConnectionStringBuilder();
bb.Metadata = "res://*/dao.bdmi.csdl|res://*/dao.bdmi.ssdl|res://*/dao.bdmi.msl";
//same as below client tobe used
bb.Provider = "MySql.Data.MySqlClient";
MySql.Data.MySqlClient.MySqlConnectionStringBuilder mbb = new MySql.Data.MySqlClient.MySqlConnectionStringBuilder();
mbb.Server = "";
mbb.Database = "";
mbb.UserID = "";
mbb.Password = "";
mbb.PersistSecurityInfo = true;
//use providerconnectionstring insted of connectionstring
bb.ProviderConnectionString = mbb.ToString();
return bb.ToString();
Through this way, you can change your ConnectionString as you want.
In my case, I found that my project had the EFSQLLite nuget package installed, which seems doesn't recognise the Server= keyword.
I uninstalled that nuget and installed the full EFSQLSERVER one, and it worked fine

ASP.NET A problem when connect from remote DB to localhost instance

I have made the remote database backup (MSSQL Server 2005 Express) to the localhost, and I want to connect with that in my app. To do so, I've changed the connection string:
from
Data Source=190.190.200.100;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;
to
Data Source=TOMEK\SQLExpress;Database=myDataBase;User ID=myUsername;Password=myPassword;
but the error appears: Format of the initialization
string does not conform to specification starting at index 0
I can login to the Management studio with that login/pass, and also the user name that I login into sql server has role membership db_owner to that database
what's wrong with that connection string ?
Try one of the 2 options.
Data Source=foo\SQLExpress;Initial Catalog=bar;User Id=user;Password=pwd;
or
Server=foo\SQLExpress;Database=bar;User ID=user;Password=pwd;
They're equivalent, but not sure if you can mix/match
Server/Data Source
Database/Initial Catalog
Are you using a hardcoded string, or from a .config file? Does your username or pwd contain any characters that might need escaping in the .config file? i.e. slash, ampersand?
string connstr = #"Server=foo\SQLExpress;Database=bar;User ID=user;Password=pwd;";
SqlConnection conn = new SqlConnection(connstr);
<add name="ConnectionString"
connectionString="Data Source=.\SQLEXPRESS;Database=bar;User ID=user1;Password=&foo;"
providerName="System.Data.SqlClient" />
Maybe try comparing what you are doing to what is found here:
http://www.connectionstrings.com/sql-server-2005

Resources