6
Answers

Web.config File And Connection string

Aman Jen

Aman Jen

11y
1.5k
1
Hi,
    Is it possible to connection string declared in web config file and use in subpages of Master Page once it defined  in master page....because I dont want to declare it again and again in sub pages because that again the same thing like declaring connection string locally like below on every subpage of Master Page

Public connStr As String = ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString
    Public cn As New SqlConnection(connStr)
    Public cmd As New SqlCommand


...plz help..



I Hope i am clear with my query
Answers (6)
0
Sanjeeb Lenka

Sanjeeb Lenka

NA 22.1k 1.3m 11y
hi,

As iftikar said you create a Data access layer and get that connection string into that from web.config and use it in any page by creating object of that class.
0
Iftikar Hussain

Iftikar Hussain

NA 18.9k 275.5k 11y
better create one DataAccess layer and defined your connection over there. So that this DAL can be used throughout your project for any sql operation

Regards,
Iftikar
0
Sanjeeb Lenka

Sanjeeb Lenka

NA 22.1k 1.3m 11y
Nothing required  just declare the connection string in web.config like this

<connectionStrings>
<add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB" providerName="System.Data.SqlClient"/>
</connectionStrings >


then which page you want connection string !st add the reference


Imports System.Configuration

then you can able to get the connection string in page like this

Dim strcon As String = ConfigurationManager.ConnectionStrings("dbconnection").ConnectionString

now you can use it
0
Aman Jen

Aman Jen

NA 137 31.7k 11y
Hi,
    Can we write it as something like master page function after declaring in web.config file, to use  like function in sub pages

Or in Function in Dll

I hope M clear
0
Sanjeeb Lenka

Sanjeeb Lenka

NA 22.1k 1.3m 11y
refer to this link for clear details

http://www.aspdotnet-suresh.com/2011/11/write-connection-strings-in-webconfig.html

if you declare connection string in web.config you can access it any page within your project

0
Iftikar Hussain

Iftikar Hussain

NA 18.9k 275.5k 11y
Hi,
        You no need to declare you connection string again and again until it is different for sub page. Just declare once in your main web.config

<connectionStrings>
    <add name="conString"
         connectionString="Data Source=SQLEXPRESS;Initial Catalog=MyDb;Persist Security Info=True;User ID=MyUserId;Password=MyPassword"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

Regards,
Iftikar
Next Recommended Forum