How to retreve connection string programmaticaly from this

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dbuchanan

    How to retreve connection string programmaticaly from this

    Here is part of the web.config - it is given to me like this.
    =============== ========
    <configuratio n>
    <appSettings/>
    <connectionStri ngs>
    <add name="MyConnect ionName" connectionStrin g="Data
    Source=MyServer Name;Initial Catalog=MyDatab aseName;Integra ted Security=True"
    providerName="S ystem.Data.SqlC lient"/>
    </connectionStrin gs>
    <system.web>
    =============== ========
    How do I get the connection string?

    The follwoing code is my attempt to connect

    string cnStr;
    cnStr = ConfigurationSe ttings. << At this point intellisense gives me the
    following:

    AppSettings
    Equals
    GetConfig
    ReferencEquals

    Introducing Microsoft ASP.NET 2.0 Shows a different suggests the following
    code

    cnStr =
    ConfigurationSe ttings.connecio nStrings["MyConnectionNa me"].ConnectionStri ng;

    However I get the following error
    Error 1 'System.Configu ration.Configur ationSettings' does not contain a
    definition for 'connecionStrin gs' C:\Documents and Settings\Admini strator\My
    Documents\Visua l Studio 2005\WebSites\W ebSiteCompletio nReport\Default .aspx
    13 41 C:\...\WebSiteC ompletionReport \


  • Steven Cheng [MSFT]

    #2
    RE: How to retreve connection string programmaticaly from this

    Hi Doug,

    From your description, you encountered some problem when tried retrieving
    connectionstrin gs from ASP.NET web.config programmtically , correct?

    According to the code you provided, I think the problem is due to the
    "ConfigurationS ettings" class you used. In .NET framework 2.0 application,
    you can use the following means to read connectionstrin g (in
    <connectionStri ngssection in app.config or web.config):

    1. For ASP.NET application, you can use the
    System.Web.Conf iguration.WebCo nfigurationMana ger class's
    "ConnectionStri ngs" property to access connectionstrin gs defined in
    web.config file, e.g.

    =============== ==========
    using System.Web.Conf iguration;
    ......

    protected void Page_Load(objec t sender, EventArgs e)
    {
    foreach (ConnectionStri ngSettings connstr in
    WebConfiguratio nManager.Connec tionStrings)
    {
    Response.Write( "<br/>" + connstr.Name + ": " +
    connstr.Connect ionString);
    }


    }
    =============== ===

    2. Or you can use "System.Configu rationManager.C onnectionString s" property,
    this will work in not only ASP.NET Web application, but also other desktop
    application(con sole or winform):

    =============== =====
    protected void Page_Load(objec t sender, EventArgs e)
    {
    foreach (ConnectionStri ngSettings connstr in
    ConfigurationMa nager.Connectio nStrings)
    {
    Response.Write( "<br/>" + connstr.Name + ": " +
    connstr.Connect ionString);
    }
    }
    =============== ==========

    Hope this helps.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    msdnmg@microsof t.com.

    =============== =============== =============== =====
    Get notification to my posts through email? Please refer to
    Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

    ications.

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscripti...t/default.aspx.
    =============== =============== =============== =====
    This posting is provided "AS IS" with no warranties, and confers no rights.

    --------------------
    >From: "dbuchanan" <dbuchanan@news group.nospam>
    >Subject: How to retreve connection string programmaticaly from this
    >Date: Tue, 15 Jul 2008 01:25:38 -0400
    >
    >Here is part of the web.config - it is given to me like this.
    >============== =========
    ><configuration >
    <appSettings/>
    <connectionStri ngs>
    <add name="MyConnect ionName" connectionStrin g="Data
    >Source=MyServe rName;Initial Catalog=MyDatab aseName;Integra ted
    Security=True"
    >providerName=" System.Data.Sql Client"/>
    </connectionStrin gs>
    <system.web>
    >============== =========
    >How do I get the connection string?
    >
    >The follwoing code is my attempt to connect
    >
    >string cnStr;
    >cnStr = ConfigurationSe ttings. << At this point intellisense gives me the
    >following:
    >
    >AppSettings
    >Equals
    >GetConfig
    >ReferencEqua ls
    >
    >Introducing Microsoft ASP.NET 2.0 Shows a different suggests the following
    >code
    >
    >cnStr =
    >ConfigurationS ettings.conneci onStrings["MyConnectionNa me"].ConnectionStri ng
    ;
    >
    >However I get the following error
    >Error 1 'System.Configu ration.Configur ationSettings' does not contain a
    >definition for 'connecionStrin gs' C:\Documents and
    Settings\Admini strator\My
    >Documents\Visu al Studio 2005\WebSites\W ebSiteCompletio nReport\Default .aspx
    >13 41 C:\...\WebSiteC ompletionReport \
    >
    >
    >

    Comment

    • Stan

      #3
      Re: How to retreve connection string programmaticaly from this

      On 15 Jul, 06:25, "dbuchanan" <dbucha...@news group.nospamwro te:
      Here is part of the web.config - it is given to me like this.
      =============== ========
      <configuratio n>
       <appSettings/>
       <connectionStri ngs>
        <add name="MyConnect ionName" connectionStrin g="Data
      Source=MyServer Name;Initial Catalog=MyDatab aseName;Integra ted Security=True"
      providerName="S ystem.Data.SqlC lient"/>
       </connectionStrin gs>
       <system.web>
      =============== ========
      How do I get the connection string?
      >
      The follwoing code is my attempt to connect
      >
      string cnStr;
      cnStr = ConfigurationSe ttings.  << At this point intellisense gives me the
      following:
      >
      AppSettings
      Equals
      GetConfig
      ReferencEquals
      >
      Introducing Microsoft ASP.NET 2.0 Shows a different suggests the following
      code
      >
      cnStr =
      ConfigurationSe ttings.connecio nStrings["MyConnectionNa me"].ConnectionStri ng­;
      >
      However I get the following error
      Error 1 'System.Configu ration.Configur ationSettings' does not contain a
      definition for 'connecionStrin gs' C:\Documents and Settings\Admini strator\My
      Documents\Visua l Studio 2005\WebSites\W ebSiteCompletio nReport\Default .aspx
      13 41 C:\...\WebSiteC ompletionReport \
      Hi

      Do this:

      //put this namespace in scope

      using System.Web.Conf iguration;


      //then use the WebConfiguratio nManger to read the connection string

      string ConStr =
      WebConfiguratio nManager.Connec tionStrings["MyConnectionSt ring"].ConnectionStri ng;

      A bit confusing I know but you'll get used to it.

      BTW use the same for any AppSettings in ASP.NET 2 thus:

      string Astring = WebConfiguratio nManager.AppSet tings["MyAppsetti ng"];

      HTH

      Comment

      • Steven Cheng [MSFT]

        #4
        RE: How to retreve connection string programmaticaly from this

        Hi Doug,

        Does the information in previous message help you? Please feel free to post
        here if there is anything else we can help on this.

        Sincerely,

        Steven Cheng

        Microsoft MSDN Online Support Lead

        Delighting our customers is our #1 priority. We welcome your comments and
        suggestions about how we can improve the support we provide to you. Please
        feel free to let my manager know what you think of the level of service
        provided. You can send feedback directly to my manager at:
        msdnmg@microsof t.com.

        =============== =============== =============== =====
        Get notification to my posts through email? Please refer to
        Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

        ications.
        =============== =============== =============== =====
        This posting is provided "AS IS" with no warranties, and confers no rights.



        --------------------
        >From: stcheng@online. microsoft.com (Steven Cheng [MSFT])
        >Organization : Microsoft
        >Date: Tue, 15 Jul 2008 06:40:10 GMT
        >Subject: RE: How to retreve connection string programmaticaly from this
        >
        >Hi Doug,
        >
        >From your description, you encountered some problem when tried retrieving
        >connectionstri ngs from ASP.NET web.config programmtically , correct?
        >
        >According to the code you provided, I think the problem is due to the
        >"Configuration Settings" class you used. In .NET framework 2.0
        application,
        >you can use the following means to read connectionstrin g (in
        ><connectionStr ingssection in app.config or web.config):
        >
        >1. For ASP.NET application, you can use the
        >System.Web.Con figuration.WebC onfigurationMan ager class's
        >"ConnectionStr ings" property to access connectionstrin gs defined in
        >web.config file, e.g.
        >
        >============== ===========
        >using System.Web.Conf iguration;
        >.....
        >
        protected void Page_Load(objec t sender, EventArgs e)
        {
        foreach (ConnectionStri ngSettings connstr in
        >WebConfigurati onManager.Conne ctionStrings)
        {
        Response.Write( "<br/>" + connstr.Name + ": " +
        >connstr.Connec tionString);
        }
        >
        >
        }
        >============== ====
        >
        >2. Or you can use "System.Configu rationManager.C onnectionString s"
        property,
        >this will work in not only ASP.NET Web application, but also other desktop
        >application(co nsole or winform):
        >
        >============== ======
        protected void Page_Load(objec t sender, EventArgs e)
        {

        Comment

        Working...