Solved

VS2008 C# - Not all code paths return a value

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# DataTable (in memory) vs SQL Lookups 4 75
array not updating 8 42
XML XDocument extract - Keeping a node list like a menu path 8 34
.NET universe documentation poster 2 25
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…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 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