Solved

VS2008 C# - Not all code paths return a value

Posted on 2011-02-14
4
613 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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

785 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