Thursday 2 January 2014

Connection string in asp.net using web.config / connection string in web.config

Posted on 03:29 in
Intoduction :
This article is helps to how to get connection string in asp.net using web.config file.after do this steps add web.config file.
  <connectionStrings>
    <add name="connection" connectionString="server=192.168.1.100;database=asp_dotnet;uid=sa;pwd=sa123;"/>
  </connectionStrings>

VB.NET:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim con As SqlConnection = New SqlConnection
        Try
            con.ConnectionString = WebConfigurationManager.ConnectionStrings("connection").ConnectionString
            con.Open()
        Finally
            con.Close()
        End Try
 End Sub
C#.NET:
protected void Page_Load(object sender, EventArgs e)
{
            SqlConnection con = new SqlConnection();
            try
            {
                con.ConnectionString = WebConfigurationManager.ConnectionStrings["connection"].ConnectionString;
                con.Open();
            }
            finally
            {
                con.Close();
            }

        }

0 comments:

Post a Comment