ConnectionString in web.config (Configuration File) Asp.net



A) Add Web.Config File:-
B) adding Gridview in the form
Web.Config: file

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
     <connectionStrings>
     <add name="DBCS"
     connectionString="Data Source=ITIKA-PC\SQLEXPRESS;Initial Catalog=Simple;Integrated Security=True"
     providerName="System.Data.SqlClient"/>
   
  </connectionStrings>
       <system.web>
              <compilation debug="true" targetFramework="4.0"/>
       </system.web>
</configuration>



B) Source Code:-   Soruce.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class HowtoCreateConnectionStringUsingWebconfigFile: System.Web.UI.Page
{
    protected voidPage_Load(object sender, EventArgs e)
    {
       String CS= ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
        using (SqlConnectioncon = new SqlConnection(CS))
        {
            SqlCommand cmd = newSqlCommand("Select *from ProductTable", con);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            GridView1.DataSource = dr;
            GridView1.DataBind();
        }
    }}
 


Comments

Popular posts from this blog

Creating a DDL File & link ddl file in C#

SQL Command in asp.net