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 8355869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:59:38+00:00 2026-06-09T09:59:38+00:00

I’m slowly learning C# and bit confused about the code. I have here a

  • 0

I’m slowly learning C# and bit confused about the code. I have here a simplified version of my class for the database connection settings

public class DatabaseSettings
{
    private static string connectionString = string.Empty;

    public static string ConnectionString
    {
        get 
        {
            return connectionString;
        }
        private set
        {
            connectionString = value;
        }
    }

    public bool TestConnection()
    {
        bool _returnVal = false;
        string _connectionString = String.Format("Data Source={0};Persist Security Info=False", databaseLocation);

        using (SqlCeConnection connection = new SqlCeConnection(_connectionString))
        {
            try
            {
                connection.Open();
                connectionString = _connectionString; // sets the value of the connection string here
                _returnVal = true;
            }
            catch (SqlCeException e)
            {
                // other codes here
                _returnVal = false;
            }
            finally
            {
                connection.Close();
            }
        }
        return _returnVal;
    }

    // other methods here
}

now on my Main Class, I am confused what is happening here. When i tried this code:

string _databaseLocation = "database path";
DatabaseSettings _dbaseSetting = new DatabaseSettings(_databaseLocation, true);
_dbaseSetting.TestConnection();
string newConnectionString = _dbaseSetting.ConnectionString;
//                                        ^ i got an error here
// Member 'SQLCE_Sample.ClassList.DatabaseSettings.ConnectionString.get' 
// cannot be accessed with an instance reference; 
// qualify it with a type name instead  

this one below works without an error but the problem is I got an empty string:

string _databaseLocation = "database path";
DatabaseSettings _dbaseSetting = new DatabaseSettings(_databaseLocation, true);
string newConnectionString = DatabaseSettings.ConnectionString;

What I really want is I need to create a class that has a public method which tests the connection from the application to the database. Inside that method contains a syntax that sets the value of the connection string. Then I need also a property that retrieves the value of the connection string without instantiating the class (that’s why i added the static keyword). How can I possibly to 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-09T09:59:39+00:00Added an answer on June 9, 2026 at 9:59 am

    It appears you want to access (statically) the value of a property that is set by an instance of your class. So, all you need to do is listen to the compiler error — access the property as a static reference rather than an instance reference. Change:

    string newConnectionString = _dbaseSetting.ConnectionString;
    

    To:

    string newConnectionString = DatabaseSettings.ConnectionString;
    

    You’ll of course need to make sure that you call TestConnection on the instance before you ever try to access the propery statically like this; otherwise ConnectionString will be null.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a reasonable size flat file database of text documents mostly saved in
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't

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.