/* ------------------------ My Meta Content Here SEO ------------------------ */

Pages

Main Menu

Friday, April 6, 2012

Disable and Restrict Copy Cut and Paste from on a textbox


You will need two javascript functions for this:

Read More »

Monday, April 2, 2012

Write Read AppSettings Connection String From Web.Config Asp.Net

Most of the time in Asp.Net web or windows forms application we need to connect to Sql Server database, and for connecting we need to Write Or Read connection string from Web.Config Or App.Config file In Asp.Net.

This post explains How to Write Or Read this Information From configuration file for Web Applications or Winforms.

We can either use connectionStrings section or AppSettings and can Modify Programmatically At Run Time as well.

Sql Server information is usually written inside

   1:  <configuration>
   2:  <connectionStrings>
   3:   
   4:  <add name="MyCon" 
   5:  connectionString="Data Source=AMITJAIN\SQL;
   6:  Initial Catalog=Northwind;User ID=amit;Password=password"
   7:  providerName="System.Data.SqlClient" />
   8:   
   9:  connectionStrings>
  10:  configuration>

Where Data Source is db Server Address, Initial Catalog is DataBase Name
To use Windows Authentication instead of Sql Server Authentication write
   1:  <connectionStrings>
   2:   
   3:  <add name="MyCon" 
   4:  connectionString="Data Source=AMITJAIN\SQL;
   5:  Initial Catalog=Northwind;Integrated Security=True"
   6:  providerName="System.Data.SqlClient" />
   7:   
   8:  connectionStrings>

For database attached in App_Data Folder we can write
   1:  <connectionStrings>
   2:   
   3:  <add name="MyCon" 
   4:  connectionString="Data Source=.\SQLEXPRESS;
   5:  AttachDbFilename=|DataDirectory|\NORTHWND.MDF;
   6:  Integrated Security=True;User Instance=True"
   7:  providerName="System.Data.SqlClient" />
   8:   
   9:  connectionStrings>

USING APPSETTINGS

We can also write data in Key Value pairs.
   1:  <configuration>
   2:  <appSettings>
   3:  <add key="MyCon" 
   4:       value="Data Source=AMITJAIN\SQL;
   5:              Initial Catalog=Northwind;
   6:              User ID=amit;Password=password"/>
   7:  appSettings>
   8:  configuration>

READ CONNECTION STRING FROM WEB.CONFIG FILE IN ASP.NET

To read Settings we can write code as mentioned below.

C# CODE

1using System.Configuration;
2string strConn = ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString;

1using System.Configuration;
2string strConn = ConfigurationManager.AppSettings["MyCon"].ToString();

VB.NET CODE

1Imports System.Configuration
2Dim strConn As String = ConfigurationManager.ConnectionStrings("MyCon").ConnectionString
3 Dim strConn As String = ConfigurationManager.AppSettings("MyCon").ToString()

Read More »

My Blog List