Solved

VS2008 C# - Not all code paths return a value

Posted on 2011-02-14
4
616 Views
Last Modified: 2013-12-17
I'm running into a an error on compilation indicating that not all code paths return a value.  As far as I can tell, all paths do return a value.  Could you let me know what I'm missing.  Also is there some type of addin to VS that aids in debugging that will help to identify these and other issues more readily(It doesn't need to be free...) In this implementation I do need to use a string concatenation process rather than using the SqlConnectionStringBuilder which I will be implementing at a later time once the logic is in place.

Anyway, here's my code:

static public string ConnectionString(string Database,ServerLocation Server)
{
      SystemType System;
      String DataSource = "";
      String InitialCatalog = "";
      String UserID = "";
      String Password = "";
      String IntegratedSecurity = "false";
      String result;

      switch (Environment.MachineName)
      {
            case "XXXXXX-WEB1":
            case "XXXXXX-DB1":
            case "XXXXXX-DB2":
                  System = SystemType.HostedSystem;
                  break;
            default:
                  System = SystemType.LocalSystem;
                  break;
      }
      
      switch (Server)
      {
            case ServerLocation.WebServer:
                  switch (System)
                  {
                        case SystemType.HostedSystem:
                              DataSource = "XXXXXX-WEB1";
                              break;
                        case SystemType.LocalSystem:
                        default:
                              DataSource = "xx.xxx.xx.18";
                              break;
                  }
                  InitialCatalog = Database;
                  UserID = "NSC";
                  Password = "xxxxxxxx";
                  break;
            case ServerLocation.DevServer:

                  switch (System)
                  {
                        case SystemType.HostedSystem:
                              DataSource = "XXXXXX-DB1";
                              break;
                        case SystemType.LocalSystem:
                        default:
                              DataSource = "xx.xxx.xx.19";
                              break;
                  }
                  InitialCatalog = Database;
                  UserID = "NSC";
                  Password = "xxxxxxxx";
                  break;
            case ServerLocation.IdrServer:
                  switch (System)
                  {
                        case SystemType.HostedSystem:
                              DataSource = "XXXXXX-DB2";
                              break;
                        case SystemType.LocalSystem:
                        default:
                              DataSource = "xx.xx.xx.20";
                              break;
                  }
                  InitialCatalog = Database;
                  UserID = "NSC";
                  Password = "xxxxxxxx";

                  break;
            case ServerLocation.LocalServer:
            default:
                  DataSource = Environment.MachineName;
                  InitialCatalog = Database;
                  IntegratedSecurity = "SSPI";

                  break;
      }
      switch (IntegratedSecurity)
      {
            case "SSPI":
                  result = "DataSource=" + DataSource + ";" +
                                    "Initial Catalog=" + InitialCatalog + ";" +
                                    "Integrated Security=SSPI;";
                  break;
            case "false":
            default:
                  result = "DataSource=" + DataSource + ";" +
                                    "Initial Catalog=" + InitialCatalog + ";" +
                                    "User Id=" + UserID + ";" +
                                    "Password=" + Password + ";";
                  break;
      }

}

      public enum ServerLocation
      {
            LocalServer,
            WebServer,
            IdrServer,
            DevServer
      }
      
      public enum SystemType
      {
            HostedSystem,
            LocalSystem
      }
0
Comment
Question by:AndyH79
  • 2
4 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 34889203
static public string ConnectionString(string Database,ServerLocation Server)

never returns a string --

if you don't want it to return anything then you need to set it to void instead of string

static public void ConnectionString(string Database,ServerLocation Server)
0
 
LVL 22

Assisted Solution

by:p_davis
p_davis earned 250 total points
ID: 34889216
at the very end of the method, right before the end bracket you can just put

return result;
0
 
LVL 12

Accepted Solution

by:
Mamtha1982 earned 250 total points
ID: 34889269

static public string ConnectionString(string Database,ServerLocation Server)
{
      SystemType System;
      String DataSource = "";
      String InitialCatalog = "";
      String UserID = "";
      String Password = "";
      String IntegratedSecurity = "false";
      String result;

      switch (Environment.MachineName)
      {
            case "XXXXXX-WEB1":
            case "XXXXXX-DB1":
            case "XXXXXX-DB2":
                  System = SystemType.HostedSystem;
                  break;
            default:
                  System = SystemType.LocalSystem;
                  break;
      }
     
      switch (Server)
      {
            case ServerLocation.WebServer:
                  switch (System)
                  {
                        case SystemType.HostedSystem:
                              DataSource = "XXXXXX-WEB1";
                              break;
                        case SystemType.LocalSystem:
                        default:
                              DataSource = "xx.xxx.xx.18";
                              break;
                  }
                  InitialCatalog = Database;
                  UserID = "NSC";
                  Password = "xxxxxxxx";
                  break;
            case ServerLocation.DevServer:

                  switch (System)
                  {
                        case SystemType.HostedSystem:
                              DataSource = "XXXXXX-DB1";
                              break;
                        case SystemType.LocalSystem:
                        default:
                              DataSource = "xx.xxx.xx.19";
                              break;
                  }
                  InitialCatalog = Database;
                  UserID = "NSC";
                  Password = "xxxxxxxx";
                  break;
            case ServerLocation.IdrServer:
                  switch (System)
                  {
                        case SystemType.HostedSystem:
                              DataSource = "XXXXXX-DB2";
                              break;
                        case SystemType.LocalSystem:
                        default:
                              DataSource = "xx.xx.xx.20";
                              break;
                  }
                  InitialCatalog = Database;
                  UserID = "NSC";
                  Password = "xxxxxxxx";

                  break;
            case ServerLocation.LocalServer:
            default:
                  DataSource = Environment.MachineName;
                  InitialCatalog = Database;
                  IntegratedSecurity = "SSPI";

                  break;
      }
      switch (IntegratedSecurity)
      {
            case "SSPI":
                  result = "DataSource=" + DataSource + ";" +
                                    "Initial Catalog=" + InitialCatalog + ";" +
                                    "Integrated Security=SSPI;";
                  break;
            case "false":
            default:
                  result = "DataSource=" + DataSource + ";" +
                                    "Initial Catalog=" + InitialCatalog + ";" +
                                    "User Id=" + UserID + ";" +
                                    "Password=" + Password + ";";
                  break;
      }
 return result;
}

0
 

Author Closing Comment

by:AndyH79
ID: 34889368
Well that's embarrassing.. I spent all my time looking for the issue with my switch statements I never thought to look to see if the function itself returned a result.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question