Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9148437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:16:23+00:00 2026-06-17T11:16:23+00:00

I am modifying some old code someone wrote and they had placed a connection

  • 0

I am modifying some old code someone wrote and they had placed a connection string in the web.config and due to business rules, I am having to remove it. I have made a singleton class that generates the connection string and want to set it in the asp.net markup in a similar fashion like this:

<asp:SqlDataSource ID="DataSource" runat="server" 
    ConnectionString='<% MyNameSpace.Credentials.Instance.DatabaseConnectionString %>'
    ProviderName="Oracle.DataAccess.Client" 
    SelectCommand="SELECT * from aTable">
</asp:SqlDataSource>

Unfortunately, that doesn’t work and is taking that line and using it as the literal connection string. Anyone know how to access a property of a class inline like this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:16:26+00:00Added an answer on June 17, 2026 at 11:16 am

    Consider the following (not unrealistic) scenario. It is a normal weekday morning when your exception monitoring suddenly starts sending large number of SQL logon errors. After a few panicked phone calls to your infrastructure team it becomes apparent that the RAID controller on the database server has failed. The nearest replacement is in another country and will take at least 48 hours to arrive.

    Fortunately your DBAs are on the ball and have started a restore onto another server which will be available in less than an hour, one of them is due to phone you with the connection details shortly.

    At this point, if your application stores database connection details in a single central location, you should be able to bring the application back online almost as soon as the backup database is restored – after quick testing to ensure no data corruption has occurred and normal functionality is available.

    If you have to search through each and every page and class in the project then it could take hours to update all the different connection strings, and you can’t be sure you haven’t missed one.

    Instead of having the application restored before lunch, you’re working well into the night with users angry at you for a lost day.

    Admittedly Find/Replace tools will make it easier to track down references to the server/database name without having to manually hunt through every line of code, but you are still making life a lot harder than it needs to be.

    Microsoft created the connection strings element in the web.config file for good reason, cater for many high security environments, and even provide encryption for the strings to keep them safe. In contract, even compiled code can almost instantly be made human readable using a solution like ReSharper, dotPeek or JustDecompile. If your managers believe keeping connection strings in compiled code is safer then they are wrong.

    If you must store the connection strings outside of the web.config, then I would suggest you create a utility class for the purpose-

    using System;
    
    namespace YourApplicationNameSpace
    {
        public static class Common
        {
            public static string DatabaseConnectionString
            {
                get { return "server=myserver;database=Products;uid=salesUser;pwd=sellMoreProducts"; }
            }
        }
    }
    

    The string can then be added to the declarative data source of your page like so-

    <asp:SqlDataSource ID="DataSource" runat="server" ProviderName="Oracle.DataAccess.Client" 
    SelectCommand="SELECT * from aTable"></asp:SqlDataSource>
    

    In the code behind-

    protected void Page_Load(object sender, EventArgs e)
    {
        DataSource.ConnectionString = YourApplicationNameSpace.Common.DatabaseConnectionString;
    }
    

    This looks like the pattern your company uses.

    I believe this would also work (note the addition of the equals sign)-

    <asp:SqlDataSource ID="DataSource" runat="server" 
    ConnectionString='<%= MyNameSpace.Credentials.Instance.DatabaseConnectionString %>'
    ProviderName="Oracle.DataAccess.Client" 
    SelectCommand="SELECT * from aTable">
    </asp:SqlDataSource>
    

    If you have to keep connection strings confined to individual pages then do-

    protected void Page_Load(object sender, EventArgs e)
    {
        DataSource.ConnectionString = "server=myserver;database=Products;uid=salesUser;pwd=sellMoreProducts";
    }
    

    Then start looking for a new job as the code will be a nightmare to maintain.

    Side note that you should make sure you are using parameterized commands for your SQL, especially when updating or deleting data in order to avoid SQL injection – or even better Stored Procedures (also with properly constructed parameters).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am modifying some Flex code written by someone else. There is an mx:text
I'm modifying some legacy code and I found a web page that loads the
I'm knee deep in modifying some old logging code that i didn't write and
I am modifying some code for a Blackfin processor using VisualDSP++ v. 5.0. I
I'm working on modifying some existing code for a payment gateway and I'm not
I'm modifying some code that calls enumerate on a list declared via a list
I've been modifying some code to work between Mac OS X and iPhone OS.
I'm new to C# and directly diving into modifying some code for a project
I'm constantly modifying some text on a web page with JavaScript. I want the
I'm modifying some c# code and attempting to add a date+time field to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.