I am Trying to teach myself C#. I'm struggling with getting a connection to SQL server. I have a static class, GetSQLConnectionString that returns a connection string.
When I try to call in in my forms class I get the following:
private void button1_Click(object sender, EventArgs e)
{
string conn = GetSQLConnectionString; (Error displayed here
}

Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Nov 24, 2023, 12:26 PM EST
To achieve this define a static method in the static class like.
static string GetSQLConnectionString() { // This might involve reading from a configuration file, etc. // Replace the return statement with your actual logic. return "your_connection_string"; }Now call the connection string method in where you need.
static void Main() { // Get the connection string string connectionString = GetSQLConnectionString(); using (SqlConnection connection = new SqlConnection(connectionString)) { // You code here } }Sam HobbsRank #107 · 20.5K reputationPosted Nov 24, 2023, 2:06 PM EST
What is the error? It helps to also show the error message.
I do not see () after GetSQLConnectionString. You can see in the other reply that you need to use GetSQLConnectionString(), not GetSQLConnectionString.