Solved

VS2008 C# - Not all code paths return a value

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now