Solved

not all code paths return a value

Posted on 2006-07-03
9
252 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
Independent Software Vendors: 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 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Change to file doesn't show up 16 93
Convert Silverlight ERP To Angularjs,HTML5 3 109
Return array 3 35
web api controller with multiple posts that accept different objects 1 25
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

759 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