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

  • SEARCH
  • Home
  • 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 6156919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:41:46+00:00 2026-05-23T20:41:46+00:00

Last time I had a similar question, but we figured out if initialize and

  • 0

Last time I had a similar question, but we figured out if initialize and set a value for the variable before the logic statement then I can use the value that was generated within the logic statement.

This time, I want to call one of two method overloads depending on if the connection string is empty or not. Like so.

if (ConnectionString != "") // if there is something in the config file work with it
{
  SqlConnection dataConnection = new SqlConnection(ConnectionString);
}
else
{
  SqlConnection dataConnection = new SqlConnection();
}

try {
  // ...

The problem is that anything in the try block fails because it does not know about the dataConnection.

How can do I do this in a way to make it work?

  • 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-05-23T20:41:47+00:00Added an answer on May 23, 2026 at 8:41 pm

    Declare it (uninitialized) outside:

    SqlConnection conn;
    if(string.IsNullOrEmpty(connectionString)) {
        conn = new SqlConnection();
    } else {
        conn = new SqlConnection(connectionString);
    }
    

    If the logic is simple, a conditional is possible too:

    SqlConnection conn = string.IsNullOrEmpty(connectionString)
         ? new SqlConnection() : new SqlConnection(connectionString);
    

    The latter is much easier to use with a using block, as it can be done inline.

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

Sidebar

Related Questions

I'm not sure how I got to this state, but last time I had
it’s been a while since the last time I had to use regex, I
How do you find out the last time a MySQL database was read or
Last time I had to deal with Java was 2005 and I forgot almost
I have asked a similar question to this but for GMail and I was
I am sorry the last time I had used this website was my first
I'm sure a similar question has been asked before, and I've looked around for
Well this is not the first time im asking these question here but I
last time I asked how to populate a data structure here . Now I
Last time I asked about the reverse process , and got some very efficient

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.