sql connection is making exception in asp.net - c#

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.

Related

ConnectionString ASP.NET

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.

Connection string has quotation mark, how to pass it to SqlConnection

I have created a LocalDB database in my project and its connection string is :
Data Source=(LocalDB)\v11.0;AttachDbFilename="E:\Projects\visual studio 2013\Projects\sqlce\mydb.mdf";Integrated Security=True;Connect Timeout=30
How should I pass it to SqlConnection()?
Note that it has an address within quotation marks. Have I done anything wrong?
I guess even if I program it correctly it won't work in another computer which doesn't have that .mdf file in that exact place. Isn't it so?
How can I have a program with a portable database so I can easily publish my pp?
Add the mdf file to your solution and and change the property "Copy to Output Directory" to Copy Always. Don't hardcode the mdf file path in connection string. Add the connection string in app.config or web.config file like below:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\myDB.mdf;Initial Catalog=MyDatabaseName;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Then you can access the connection string in your C# code as below:
string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString()
If you face any error regarding accessing the mdf file, the you can set the DataDirectory in your C# code using AppDomain.CurrentDomain.SetData() method.
<connectionStrings>
<add
name="NorthwindConnectionString"
connectionString="Data Source=serverName;Initial
Catalog=Northwind;Persist Security Info=True;User
ID=userName;Password=password"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
you can access by
connString =rootWebConfig.ConnectionStrings.ConnectionStrings["NorthwindConnectionString"];
More about connection string
SqlConnection con = new SqlConnection(connstring);
or you can go in this way like
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].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

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:

Single declaration of connection string in form application

In windows forms application there is no web.config. So how can i declare the single connection string? And how can I call it in another pages?
In App.config page
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="CONSTRING" connectionString="Data Source=SQL-PC;Initial Catalog=DATABASE;Integrated Security=True"/>
</connectionStrings>
</configuration>
In Form I call this connectin string.
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace Sample {
public partial class Sample: Form
{
public string conn = ConfigurationManager.ConnectionStrings["CONSTRING"].ConnectionString;
}
Now there is error shows. ConfigurationManager does not exist in this current context. How can I solve it?
ConfigurationManager class resides in System.Configuration assembly. And to make your code work you need to add reference to System.Configuration assembly to the project.
You can declare various connections string in App.config and use use:
var conString =System.Configuration.ConfigurationManager.ConnectionStrings["connectionStringName"];
string strConnString = conString.ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand(String.Format("SELECT * FROM Table;"), con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
And declare a connection string in App.config:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=database;Integrated Security=True; MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
App.config is what you are looking for.
How to add configurations to windows forms.
Also use Configuration Manager to get your configurations.
you don't need any configuration file add below code in your connection string
static string ConStr = "Data Source=DataSourceName;Initial Catalog=DatabaseName;Integrated Security=True";
SqlConnection con = new SqlConnection(ConStr);
place this code before namespace
using System.Configuration;
Add reference to System.Configuration assembly to the project.
Then use below code in c# form page
var conString = System.Configuration.ConfigurationManager.ConnectionStrings["CONSTRING"];
string strConnString = conString.ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
Your app.config connectionstring should be like this.
<connectionStrings>
<add name="CONSTRING" connectionString="Data Source=MONAIT-PC;Initial Catalog=CMS;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
By using connection two ways: 1. Sql Server Authentication
2. Windows Authentication
{Sql Server Authentication}
<connection string>
<add name="somename" connectionstring="database=databasename;data source=servername;uid=somename;pwd=somename(or)numbers">
</connection string>
{Windows Server Authentication}
<connection string>
<add name="somename" connectionstring="database=databasename;data source=servername; integrated security = true">
</connection string>
In Windows form application there is Application config file in that you can add connection string and you can call that file to all page of that application.
You can right click to solution explorer and then add new Item and add App.Config file and in that there is tag for connection string same as in web application.

Categories