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
Solved

Why the following code doesn't compile

Posted on 2006-11-12
6
222 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 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