Connection Strings in web.config configuration file (Part - 3)
In an asp.net web application, the configuration strings can be stored in web.config file, as shown below. Give a meaningful name to your connection string. Since we are working with sql server, the provider name is System.Data.SqlClient.
If we have 100 web forms In 1 application Instead of creating every time connection will be stored in web.config file
connectionString="data source=.; database=Sample_Test_DB; Integrated Security=SSPI"
providerName="System.Data.SqlClient" />
<connectionStrings>
Comming to the Visual studio Take an Grid view
Create a name space Like
using System.Configuration;
View Code In C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
namespace WebApplication2
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string ConnectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection( ConnectionString ))
// Using is forced to close the conncetion so no need to close the conncetion
{
SqlCommand cmd = new SqlCommand("Select * from tblProductInventory", con);
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
}
}
}
}
0 comments:
Post a Comment