Solved

Why the following code doesn't compile

Posted on 2006-11-12
6
227 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
Technology Partners: 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

734 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