Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

not all code paths return a value

Posted on 2006-07-03
9
Medium Priority
?
254 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 500 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 44

Assisted Solution

by:bruintje
bruintje earned 1000 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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

722 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