Solved

VS2008 C# - Not all code paths return a value

Posted on 2011-02-14
4
621 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:
Praveen Kumar 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

632 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