Solved

Why the following code doesn't compile

Posted on 2006-11-12
6
218 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

22 Experts available now in Live!

Get 1:1 Help Now