Solved

Why the following code doesn't compile

Posted on 2006-11-12
6
225 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
[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
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
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!

 
LVL 63

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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