Solved

Why the following code doesn't compile

Posted on 2006-11-12
6
220 Views
Last Modified: 2010-04-16
Hi,

Can you please let me know why the following code doesn't compile. I am getting an error "Not all path return the value".
Thanks for the help!

Run2004

    public class NewUserDataAggregator
    {
//  First check to see if the email already is taken care.

        public string checkForDuplicate(string strConnection, string email)
        {
            string sSql = "Select * from dbo.contactDetails where emailID = @emailID";
            using (SqlConnection myConn = new SqlConnection(strConnection))
            {
                myConn.Open();
                using (SqlCommand myCommand = new SqlCommand(sSql, myConn))
                {
                    myCommand.Parameters.Add("@emailID", SqlDbType.VarChar, 50).Value = email;
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        while (myReader.Read())
                        {
                            if (myReader[0].ToString() == null)
                            {
                                processRequest(strConnection, email, firstName, lastName, address,
                                   state, zip, homeNumber,
                                    cellNumber, country, password);
                            }
                            else
                            {
                                return("This Email id is already taken. Please try with different Email ID");                                
                            }                            
                        }
                    }
                }
            }
        }
0
Comment
Question by:run2004
6 Comments
 
LVL 35

Expert Comment

by:Raynard7
ID: 17927342
because you are supposed to be returning a string for this checkForDuplicate function.

The only return is in the else statement in the while loop, meaning if this else is not triggered then you will not end up returning a string.

Depending on the purpose of the code - you could add at the second last line

return("There Is No Problem With this Data");

which would mean that all possible options would at least be able to return a value
0
 
LVL 1

Author Comment

by:run2004
ID: 17927409
Can you send me the working code. I know something is not returned, but where???
0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17927451
below will return a string from all paths - if this is what you want or not I do not know.

        public string checkForDuplicate(string strConnection, string email)
        {
            string sSql = "Select * from dbo.contactDetails where emailID = @emailID";
            using (SqlConnection myConn = new SqlConnection(strConnection))
            {
                myConn.Open();
                using (SqlCommand myCommand = new SqlCommand(sSql, myConn))
                {
                    myCommand.Parameters.Add("@emailID", SqlDbType.VarChar, 50).Value = email;
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        while (myReader.Read())
                        {
                            if (myReader[0].ToString() == null)
                            {
                                processRequest(strConnection, email, firstName, lastName, address,
                                   state, zip, homeNumber,
                                    cellNumber, country, password);
                            }
                            else
                            {
                                return("This Email id is already taken. Please try with different Email ID");                                
                            }                            
                        }
                    }
                }
            }
return("There Is No Problem With this Data");
        }
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 17927519
Hi run2004;

The modification has been made to your code below to allow your function to compile. I add two return statement so that all path return a value, please read the comment at the statements I added.

      public string checkForDuplicate(string strConnection, string email)
      {
            string sSql = "Select * from dbo.contactDetails where emailID = @emailID";
            using (SqlConnection myConn = new SqlConnection(strConnection))
            {
                  myConn.Open();
                  using (SqlCommand myCommand = new SqlCommand(sSql, myConn))
                  {
                        myCommand.Parameters.Add("@emailID", SqlDbType.VarChar, 50).Value = email;
                        using (SqlDataReader myReader = myCommand.ExecuteReader())
                        {
                              while (myReader.Read())
                              {
                                    if (myReader[0].ToString() == null)
                                    {
                                          processRequest(strConnection, email, firstName, lastName, address,
                                                state, zip, homeNumber,
                                                cellNumber, country, password);

                                                        // This return statement is optional as long as the very last return I added
                                                        // is in place. This return should report that the data was processed
                                          return "The request has been processed";

                                    }
                                    else
                                    {
                                          return("This Email id is already taken. Please try with different Email ID");                                
                                    }                            
                              }

                                        // This return with a string value is needed if the While has no data to read the function
                                        // must return a value
                              return "There was not data to be processed";

                        }
                  }
            }
      }

Fernando
0
 
LVL 4

Expert Comment

by:boy8964
ID: 17928057
if (myReader[0].ToString() == null)
  {
           processRequest(strConnection, email, firstName, lastName, address,
             state, zip, homeNumber,
                cellNumber, country, password);

         RETURN "ok";
 }
           else
  {
           return("This Email id is already taken. Please try with different Email ID");                                
  }  

Problem is that you did not return anything at if block.
0
 
LVL 2

Expert Comment

by:Sinclair
ID: 17933996
On a sidenote, this function probably represents bad design. If I were writing this code, I'd probably return a bool (true if there are duplicates, false otherwise), or a list of duplicates (if I needed them), and let the UI code handle the job of formatting messages for the user.

Additionally, if your program does more database-type things in the same session, I'd consider caching the DB connection in a factory method or a member variable, or just passing it into checkForDuplicate. Opening a new DB connection can be expensive -- unless .NET implemented that connection pooling already ?
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
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…

773 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