connection string is not working in asp.net webforms - c#

i registered my website on somee.com. for that i have uploaded MS SQL database. i was wrting this connection string in my code:
connectionString="metadata=res://*/nrcsaEntities.csdl|res://*/nrcsaEntities.ssdl|res://*/nrcsaEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=My-PC;initial catalog=nrcsa;integrated security=True;multipleactiveresultsets=True;App=EntityFramework""
now as i registered somee.com is providing me new connection string that is:
workstation id=nrcsadb.mssql.somee.com;packet size=4096;user id=DuaZoya_SQLLogin_1;pwd=abcd;data source=nrcsadb.mssql.somee.com;persist security info=False;initial catalog=nrcsadb
i have changed connectiong string in file web.config by replacing this first connection string with provided connection string by somee.com
PROBLEM:
This replacement is generating warning that:
System.ArgumentException: Keyword not supported: 'user id'.
how to solve this problem?

In the web.config file ....
<connectionStrings><add name="nameofConnection" connectionString="Data Source=servername; Initial Catalog=DatabaseName; User ID=UserName; Password=Password;"/> </connectionStrings>
<system.web>
<compilation debug="false" targetFramework="4.0" /> </system.web>
you can edit target Framework according to you.
from : http://dotnet-developers-cafe.blogspot.in/2013/08/create-connection-string-in-aspnet.html

You're using Entity Framework.
Entity Framework has its own connection string which contains a reference to the EF metadata (metadata=...) as well as the inner connection string to connect to the actual database.
You need to insert your actual database connection string inside the EF connection seting, in the provider connection string=... section.
You will also need to add multipleactiveresultsets=True to their connection string; EF needs that setting.

As you are using entity famework, then your connection string will look like
<connectionStrings>
<add name="BlogContext"
connectionString="metadata=res://*/BloggingModel.csdl|
res://*/BloggingModel.ssdl| res://*/BloggingModel.msl;
provider=System.Data.SqlClient
provider connection string="data source=[you somee.com connetion string];"" providerName="System.Data.EntityClient" />
</connectionStrings>
what you need to do is simply change the value of data source from the actual connectionstring provided by somee.com

Don't replace the whole connection string. You will need to remove the Integrated Security = true section and replace it with user=DuaZoya_SQLLogin_1;password=abcd.
Also change the data source to nrcsadb.mssql.somee.com.
You pretty much just need to replace values in your existing connection string with the values provided.

Related

EF creates database with adding guid to it's name

I want to create a new database using EF6. This is my connection string. I'm using code-first.
<connectionStrings>
<add name="local"
providerName="System.Data.SqlClient"
connectionString="Data Source=(localdb)\MSSQLLocalDB;AttachDbFileName=|DataDirectory|\SampleDbName.mdf;Integrated Security=True;MultipleActiveResultSets=True" />
</connectionStrings>
After running my asp.net app EF completely creates database, but it's name changes to SAMPLEDBNAME_349a392a7d84452287b4948d4f2ab5cf (it's probably a guid).
What do I have to do to create database with name specified in connection string?
Add this to your connection string:
Database=DatabaseName
or InitialCatalog=DatabaseName
like so(if registering with dependency injection):
string connection = #"Server=server_address;Database=databaseName;PersistSecurityInfo=False;User=username;Password=pa$$;MultipleActiveResultSets=False;";
services.AddDbContext<DbContextName>(options => options.UseSqlServer(connection));
Or in your case
connectionString="Data Source=(localdb)\MSSQLLocalDB;AttachDbFileName=|DataDirectory|\SampleDbName.mdf;Integrated Security=True;Database=DatabaseName;MultipleActiveResultSets=True" />
or if Database=DatabaseName won't work
try InitialCatalog=DatabaseName

Call connection string into another connection string in web.config

I have two connections in the web.config basically they are calling the same database. I want to manage this in a better manner because change in one config also needs to change the second connection string as well.
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;persist security info=True;user id=test;password=test123;database=db-AUTH" providerName="System.Data.SqlClient" />
<add name="dbEntities" connectionString="metadata=res://*/InsuranceFinderModel.csdl|res://*/InsuranceFinderModel.ssdl|res://*/InsuranceFinderModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=db-AUTH;persist security info=True;user id=test;password=test123;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
My question is, is there any way i can call the connection string into the other connection string.
For example. default connection connection string into db entities something like
<add name="dbEntities" connectionString="metadata=res://*/InsuranceFinderModel.csdl|res://*/InsuranceFinderModel.ssdl|res://*/InsuranceFinderModel.msl;provider=System.Data.SqlClient;provider connection string= DefaultConnection" providerName="System.Data.EntityClient" />
Any suggestions would be appreciated thanks.
You are not obliged to use connection string defined inside app.config(web.config) file for entity connection. You can change entity connection string at runtime. Read this article for that: http://www.c-sharpcorner.com/UploadFile/dacca2/pass-connection-string-in-run-time-to-entity-framework/ .
Also you can get another connnection string and separete every part of connection string( DataBase,DataSource and etc.) using StringConnectionBuilder class https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.initialcatalog.aspx.
The solution is: Get DefaultConnection string and change entity connection at runtime.

Connection string stored in web.config file doesn't open SQL Server connection

I am trying to learn to use VS2015 and I have seen a number of similar postings on the internet relating to this problem and none of them have gotten me any closer to a solution.
I am trying to write into a SQL Server database and have tested both my database and connection string and confirm they are both working well.
With that out of the way I went into web.config and created a definition for said connection string:
<connectionStrings>
<add name="Name"
connectionString="Data Source=<desktopname>\\SQLEXPRESS01;Integrated Security=SSPI;Initial Catalog=Database1"/>
</connectionStrings>
As far as I can tell this works fine...
However, when I declare the string as follows:
MyConnectionString = ConfigurationManager.ConnectionStrings["Name"].ConnectionString;
Somehow what this returns is not understandable by:
SqlConnection iData = new SqlConnection(myConnectionString);
As when I try and "open" the connection it fails... Despite the exact same connection string works when declared like this within the C# code:
SqlConnection iData = new SqlConnection("Data Source=<desktopname>\\SQLEXPRESS01;Integrated Security=SSPI;Initial Catalog=Database1");
Does anyone have any ideas?
The problem lies with how the information is handled from web.config to the string to the SqlConnection as far as I can tell.
Notably everything runs perfectly unless I use the string as the connection string...
Thanks in advance!
In C# the backslash needs to be doubled, because the compiler treats the backslash as an escape character. And then the \\ compiles to \ in the actual string used at runtime.
In XML that is not the case, it uses a different parser, so in Web.Config you should use a single \:
<add name="Name" connectionString="Data Source=<desktopname>\SQLEXPRESS01;Integrated Security=SSPI;Initial Catalog=Database1"/>
On top of that you may also need to add providerName="System.Data.SqlClient":
<add
name="Name"
connectionString="Data Source=<desktopname>\SQLEXPRESS01;Integrated Security=SSPI;Initial Catalog=Database1"
providerName="System.Data.SqlClient"
/>
Reference: https://msdn.microsoft.com/en-us/library/bf7sd233(v=vs.85).aspx
Try to add : providerName="System.Data.SqlClient" in your "< add name"
<add name ="Name" connectionString="Data Source=<desktopname>\\SQLEXPRESS01;Integrated Security=SSPI;Initial Catalog=Database1" providerName="System.Data.SqlClient"/>

Entity Framework error Keyword not supported: provider \r\nconnection string

I have made MVC API using Entity Framework. It worked fine on my PC as localhost on Visual studio
when I published it and I hosted the api and the database on myasp.net to test it and just replaced this connectionstring :
<add name="ENGeekEntities" connectionString="metadata=res://*/Models.operation.csdl|res://*/Models.operation.ssdl|res://*/Models.operation.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=ENGeek;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
with the site connectionstring:
<add name="ENGeekEntities" connectionString="metadata=res://*/Models.operation.csdl|res://*/Models.operation.ssdl|res://*/Models.operation.msl;provider=System.Data.SqlClient;provider
connection string="Data Source=SQL5025.myASP.NET;Initial Catalog=DB_A0A604_ENGeek;User Id=DB_A0A604_ENGeek_admin;Password=MyPassword;App=EntityFramework"" providerName="System.Data.EntityClient" />
When I try to get data it gives me this error:
"Message":"An error has occurred.","ExceptionMessage":"Keyword not supported: 'provider \r\nconnection string'"
Any Ideas !!
The reason you were getting this error because of the " in your connection string.
Replace " with single quotes ' like below.
<add name="ENGeekEntities" connectionString="metadata=res://*/Models.operation.csdl|res://*/Models.operation.ssdl|res://*/Models.operation.msl;provider=System.Data.SqlClient;provider
connection string='Data Source=SQL5025.myASP.NET;Initial Catalog=DB_A0A604_ENGeek;User Id=DB_A0A604_ENGeek_admin;Password=MyPassword;App=EntityFramework'" providerName="System.Data.EntityClient" />
Solved.
There were two problems.
The first problem was in serializing IEmunerable ,I repalced it with JavaScriptSerializer.
The second one solved by adding single quotes Like mentioned

ASP.Net can't see my database from the ISS

I decided to learn how to make a simple ASP.Net project, with a reference to a database project through the Repository Pattern.
I have my Controller calling for a List<Weight> to handle:
public IActionResult MyWeight()
{
var repo = new Database.Repositories.WeightRepository();
var data = repo.GetWeight().Result;
return View(data);
}
When repo.GetWeight() is called, I get an AggregateException error, with an inner exception saying:
"No connection string named 'MyDatabaseConnection' could be found in the application config file."
So for clarity, let me outline the solution's structure:
aspProj
Controllers
Views
Service
App.config (1)
Web.config
...
Database
Entities
Repositories
App.config (2)
...
Database.Test
Test.cs
App.config (3)
...
I've added the following connectionString to all App.configs and the Web.config:
<connectionStrings>
<add
name="MyDatabaseConnection"
connectionString="Data Source=(localdb)\MSSQLLocalDB;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
I have tested the database both from Visual Studio's Server Explorer, and through the Test.cs file from the test-project. I can insert data and retrieve without a problem.
But when the ASP.Net-part wants to access it, there is no love.
I thought it might be the ISS which did not know the path from where it is...
Any thoughts?
__
Edit:
My Web.config:
My AppSetting.json:
Well the problem is clear - ASP.NET is trying to access the database using a connection string with the name MyDatabaseConnection:
<connectionStrings>
<add name="MyDatabaseConnection" connectionString="put the connection to the db here..." />
</connectionStrings>
And in your Web.config you only have a connection string with the name WeightDatabaseConnection:
<connectionStrings>
<add name="WeightDatabaseConnection" connectionString="put the connection to the db here..." />
</connectionStrings>
Just add a new element for MyDatabaseConnection under <connectionStrings> in the Web.config file and it should work

Categories