Monday 10 December 2012

How to Write DataBase connection in web.config file in asp.net.


Go to web.config file -> Inside configuration -> write the code given below
<connectionStrings>
<add name="Connection" connectionString="Data Source=SUSHIL-PC;Initial Catalog=modi;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

After that You can write the connection code in code behind page like this

Add Following NameSpace:-
·         using System.Configuration;
·         using System.Data;
·         using System.Data.SqlClient;

See the full code:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class gridView_CheckBoxInGridview : System.Web.UI.Page
{
    string strConn = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
    SqlConnection con;
    protected void Page_Load(object sender, EventArgs e)
    {
             con = new SqlConnection(strConn); // use this connection in your method
    }
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.