Solved

not all code paths return a value

Posted on 2006-07-03
9
253 Views
Last Modified: 2010-04-16
not all code paths return a value


public object BindDeductions()
 
            {
                  string ConnectionString = ConfigurationSettings.AppSettings["connectLocal"];
                  SqlConnection  myConnection = new SqlConnection(ConnectionString);

                  SqlCommand myCommand = new SqlCommand("sp_GetDeductions", myConnection);

                  try
                  {
                        myConnection.Open();
                              return myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                  }
                  catch
                        (SqlException SQLexc)
                  {
                        lblStatus.Text = "Error while Generating Data. Error is " + SQLexc.ToString();
                  }

      }

Got the above error,

Do you know any way around this
thanks
0
Comment
Question by:SirReadAlot
[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
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17030415
Its basically complaining because the only return statement in your code is inside the Try block. You either need to add a return statement in the Catch, or after the try/catch block.
0
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 125 total points
ID: 17030428
You could return a null if an exception occurred...

public object BindDeductions()
 
          {
               string ConnectionString = ConfigurationSettings.AppSettings["connectLocal"];
               SqlConnection  myConnection = new SqlConnection(ConnectionString);

               SqlCommand myCommand = new SqlCommand("sp_GetDeductions", myConnection);

               try
               {
                    myConnection.Open();
                    return myCommand.ExecuteReader(CommandBehavior.CloseConnection);
               }
               catch
                    (SqlException SQLexc)
               {
                    lblStatus.Text = "Error while Generating Data. Error is " + SQLexc.ToString();
                    return null;
               }

     }
0
 

Author Comment

by:SirReadAlot
ID: 17030432
will try this
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 44

Assisted Solution

by:bruintje
bruintje earned 250 total points
ID: 17030435
Hi SirReadAlot,
----------

you can put the return below the try catch so it will always return something like and check in the calling code for the object to be null or filled  

public SqlDataReader BindDeductions()
 
          {
               SqlDataReader dr = null;
               string ConnectionString = ConfigurationSettings.AppSettings["connectLocal"];
               SqlConnection  myConnection = new SqlConnection(ConnectionString);

               SqlCommand myCommand = new SqlCommand("sp_GetDeductions", myConnection);

               try
               {
                    myConnection.Open();
                    dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
               }
               catch
                    (SqlException SQLexc)
               {
                    lblStatus.Text = "Error while Generating Data. Error is " + SQLexc.ToString();
               }
                return dr;
     }

----------
bruintje
share what you know, learn what you don't
0
 

Author Comment

by:SirReadAlot
ID: 17030438
thanks
0
 

Author Comment

by:SirReadAlot
ID: 17030462
guys,
I now have 3 version which is the best
//            public object BindDeductions()
//            {
//                  SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectLocal"]);
//                  SqlCommand myCommand = new SqlCommand("sp_GetDeductions", myConnection);
//                  
//                  myCommand.CommandType = CommandType.StoredProcedure;
//                  myConnection.Open();
//                  return myCommand.ExecuteReader(CommandBehavior.CloseConnection);
//            }

//            public object BindDeductions()
//
//            {
//                  string ConnectionString = ConfigurationSettings.AppSettings["connectLocal"];
//                  SqlConnection  myConnection = new SqlConnection(ConnectionString);
//
//                  SqlCommand myCommand = new SqlCommand("sp_GetDeductions", myConnection);
//
//                  try
//                  {
//                        myConnection.Open();
//                        return myCommand.ExecuteReader(CommandBehavior.CloseConnection);
//                  }
//                  catch
//                        (SqlException SQLexc)
//                  {
//                        lblStatus.Text = "Error while Generating Data. Error is " + SQLexc.ToString();
//                        return null;
//                  }
//
//            }
//
//


            public SqlDataReader BindDeductions()
 
            {
                  SqlDataReader dr = null;
                  string ConnectionString = ConfigurationSettings.AppSettings["connectLocal"];
                  SqlConnection  myConnection = new SqlConnection(ConnectionString);

                  SqlCommand myCommand = new SqlCommand("sp_GetDeductions", myConnection);

                  try
                  {
                        myConnection.Open();
                        dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                  }
                  catch
                        (SqlException SQLexc)
                  {
                        lblStatus.Text = "Error while Generating Data. Error is " + SQLexc.ToString();
                  }
                  return dr;
            }




0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 125 total points
ID: 17030485
I'd go with the latter.
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17030490
returning outside the try catch as Carl said and bruintje demonstrated is the neater option, mine explicitly returned null even though it didnt need to because dr is declared null to begin with
0
 

Author Comment

by:SirReadAlot
ID: 17030498
okay thanks
0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

617 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