Friday, June 15, 2007 Connectivity with SQL Server 2005 Connectivity with SQL Server 2005

You will get number of queries on forums for connecting application to database. Most of time they are unable to connect because of incorrect connection string.

Step 1: Adding connection string code to web.config file

Syntax for connection string

<add source="[ServerName]" value="" key="MyDBConnection">;Initial catalog =[dbname];user id=[username];password=[password];" />

Example for connection string

Here, its an example of Windows Authentication Mode

<add value="Data Source=SHRIGANESH-PC\SQLEXPRESS;Initial Catalog=firstdb;Integrated Security=True" key="conn">

Step 2: Writing Code

Lets write code to display data in gridview.

private void BindGrid()
string sqlconn = ConfigurationManager.AppSettings["conn"].ToString();
SqlConnection conn = new SqlConnection(sqlconn);
SqlDataAdapter da = new SqlDataAdapter("select * from employee", sqlconn);
DataSet ds = new DataSet();
GridView1.DataSource = ds.Tables[0].DefaultView;

Solution for everyone, anytime

For all who are facing problem while connection application to backend, I would
suggest them to connect the application SqlDataSource Control and configure the
datasource through wizard, so that a misprint of character in your connection
string can be avoided and you can be on work without posting query on forum.

Finally Exception that you might face while connecting to .net application with sql server 2005 on windows vista operating system. connectivity with Sql Server 2005 is same as connecting to Sql Server
2000, but you can be put into trouble due to incorrect connection string and may
receive following error exception

The user is not associated with a
trusted SQL Server connection.

A connection was successfully
established with the server, but then an error occurred during the login

To resolve the above error, check that connection string
you have used for connectivity is correct.

No comments:

Most Recent Post

Community Updates

Subscribe Blog via Email

Enter your email address:

Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape