Solved

not all code paths return a value

Posted on 2006-07-03
9
248 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
  • 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

12 Experts available now in Live!

Get 1:1 Help Now