ConnectionString ASP.NET - c#

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlDataSource1"].ToString());
<connectionStrings>
<add name="SqlDataSource1"
connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=SqlDataSource3;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\SqlDataSource3.mdf"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Should that work now?
Is at ConnectionStrings["XXX"] has to be placed Name or Initial Catalog of connectionStrings?

As shown on this page, the index value is the name of the connection string. Therefore your code is correct.
Now wondering why you have posted the question. Does your code not work?

Try this:
<add name="SqlDataSource1" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=SqlDataSource3;Integrated Security=True"/>
and then:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlDataSource1"].ConnectionString);
Hope this helps.

Related

NullReferenceException Error Database data to table html

i have this statement that highlighted after i debug the program. i don't know how to fix this can someone help me please.
string constr = ConfigurationManager.ConnectionStrings["Server=LUIGEL-PC\\SQLExpress;Database=StudentInfo;Trusted_Connection=Yes"].ConnectionString;
is there a problem with this line? i'm connecting to a localdb.
In this line require key name of this value from web.config
ConfigurationManager.ConnectionStrings["Server=LUIGEL-PC\\SQLExpress;Database=StudentInfo;Trusted_Connection=Yes"].ConnectionString;
As it's not found any key name Server=LUIGEL-PC\\SQLExpress;Database=StudentInfo;Trusted_Connection=Yes in ConnectionStrings node, it's throwing NullReferenceException
Try like this
web.config
<connectionStrings>
<add
name="myConnectionString"
connectionString="Data Source=serverName;Initial
Catalog=Northwind;Persist Security Info=True;User
ID=userName;Password=password"
providerName="System.Data.SqlClient"/>
</connectionStrings>
C#
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

Dynamics Connection SQL Server C#

I would like some explanations on how to create and set up a dynamic connection to SQL Server DB engine in a C # project
if you want connection string in config and read it than you need to do like this , put possible connectionstring in config
<connectionStrings>
<add name="CharityManagement"
connectionString="Data Source=.;Initial Catalog=CharityManagement;Integrated Security=True"/>
<add name="CharityManagement_two"
connectionString="Data Source=.;Initial Catalog=CharityManagement_two;Integrated Security=True"/>
</connectionStrings>
and than read it base on condition using configurationmanager class
//to read first connection string
var connectionString=ConfigurationManager.ConnectionStrings["CharityManagement"].ConnectionString;
//to read second connection string
var connectionString=ConfigurationManager.ConnectionStrings["CharityManagement_two"].ConnectionString;
This is what you're after
In the config file
<connectionStrings>
<add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>
Then to read the connection string in your code you will do
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
Don't forget to use using System.Configuration;
Further reading here

sql connection is making exception in asp.net

I have this code
SqlConnection sqlConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["TestStoredProcedure"].ConnectionString);
This is my web.config
<add name="=TestStoredProcedure" connectionString="Data Source=.;Initial Catalog=testSqlServer;Integrated Security=True"/>
I got this exception:
Object reference not set to an instance of an object.
on the connection string line.
You're referencing ConnectionString on something that doesn't exist:
ConnectionStrings["TestStoredProcedure"].ConnectionString
Your config file has =TestStoredProcedure as the name. That doesn't match your code. Remove the =.
Include following namespace line in your header of the page.
using System.Configuration;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestStoredProcedure"].ConnectionString);
Web.Confiq:
<connectionStrings>
<add name="TestStoredProcedure" connectionString="Data Source=.;Initial Catalog=testSqlServer;Integrated Security=True"/>
</connectionStrings>
Also remove '=' from the name of the connection string.

How to solve database initialization error?

I have been testing out different things and have concluded that the following error is the reason my database will not initialize and throws an exception. The exception occurs when I reference the database and initialize it. My Web.config connection string is fine as you can see. I am using Entity Framework 6, MSSQL, and C#. Any help would be appreciated.
Exception:
Cannot open database \"xxx\" requested by the login. The login failed.\r\nLogin failed for user 'xxxx'.
Connection String:
<add name="WebDBContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=WebDB;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
Code:
private SchoolContext db = new SchoolContext();
public ViewMethod Method()
{
var students = from s in db.Students select s; //EXCEPTION IS THROWN HERE
}
You should mention userid and password in connection string
<add name="WebDBContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=WebDB;Integrated Security=SSPI;User ID=xyz;pwd=top$secret;" providerName="System.Data.SqlClient"/>
Your connection string is not in the format required by EF, it should actually look similiar to this:
<add name="XYZ" connectionString="metadata=res://*/ModEntity.csdl|res://*/ModEntity.ssdl|res://*/ModEntity.msl;provider=System.Data.SqlClient;provider connection string="Data Source=SomeServer;Initial Catalog=SomeCatalog;Persist Security Info=True;User ID=Entity;Password=SomePassword;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
Also use linq query.
var students = db.Students;
<connectionStrings>
<add name="WebDBContext" connectionString="metadata=res://*/Models.ModelName.csdl|res://*/Models.ModelName.ssdl|res://*/Models.ModelName.msl;provider=System.Data.SqlClient;provider connection string="data source=servername;initial catalog=databasename;user id=username;password=password;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
replace the ModelName to your Model and change connections on your db.

How to create a connection string in asp.net c#

I am working on asp.net c# project, for connection I used:
SqlConnection con = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\19-02\ABCC\App_Data\abcc.mdf;Integrated Security=True;User Instance=True");
but I want to get this connection string to get configure and be like this, so can any one help how to create this kind of connection.
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["itmall"].ConnectionString);
Demo :
<connectionStrings>
<add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>
Based on your question:
<connectionStrings>
<add name="itmall" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=D:\19-02\ABCC\App_Data\abcc.mdf;Integrated Security=True;User Instance=True" />
</connectionStrings>
Refer links:
http://www.connectionstrings.com/store-connection-string-in-webconfig/
Retrive connection string from web.config file:
write the below code in your file where you want;
string connstring=ConfigurationManager.ConnectionStrings["itmall"].ConnectionString;
SqlConnection con = new SqlConnection(connstring);
or you can go in your way like
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["itmall"].ConnectionString);
Note:
The "name" which you gave in web.config file and name which you used in connection string must be same(like "itmall" in this solution.)
string connectionstring="DataSource=severname;InitialCatlog=databasename;Uid=; password=;"
SqlConnection con=new SqlConnection(connectionstring)
add this in web.config file
<configuration>
<appSettings>
<add key="ConnectionString" value="Your connection string which contains database id and password"/>
</appSettings>
</configuration>
.cs file
public ConnectionObjects()
{
string connectionstring= ConfigurationManager.AppSettings["ConnectionString"].ToString();
}
Hope this helps.
Add this in your web.config file
<connectionStrings>
<add name="itmall"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=D:\19-
02\ABCC\App_Data\abcc.mdf;Integrated Security=True;User Instance=True" />
</connectionStrings>
Add this connection string tag in web.config file:
<connectionStrings>
<add name="itmall"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=D:\19-02\ABCC\App_Data\abcc.mdf;Integrated Security=True;User Instance=True"/>
</connectionStrings>
And use it like you mentioned. :)
It occurs when IIS is not being connected to SQL SERVER. For a solution, see this screenshot:

Categories