Solved

not all code paths return a value

Posted on 2006-07-03
9
249 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

920 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

14 Experts available now in Live!

Get 1:1 Help Now