Asp.net 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
<appsettings>
<add source="[ServerName]" value="" key="MyDBConnection">;Initial catalog =[dbname];user id=[username];password=[password];" />
</appsettings>
Example for connection string
Here, its an example of Windows Authentication Mode
<appsettings>
<add value="Data Source=SHRIGANESH-PC\SQLEXPRESS;Initial Catalog=firstdb;Integrated Security=True" key="conn">
</appsettings>
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();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
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.
Asp.net 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
process.
To resolve the above error, check that connection string
you have used for connectivity is correct.
No comments:
Post a Comment