Error 'not all code paths return a value'

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 3
Reputation: shrikant76 is an unknown quantity at this point 
Solved Threads: 0
shrikant76 shrikant76 is offline Offline
Newbie Poster

Error 'not all code paths return a value'

 
0
  #1
Aug 3rd, 2005
Hi All
I am getting above error when I am trying to build the project.Could anyone help me out ?

Error:
'NCCDAL.clsDataAccessLayer.fncreateCmdObj(string, System.Data.SqlClient.SqlParameter[], System.Data.SqlClient.SqlTransaction)': not all code paths return a value'

Code:
public static SqlCommand fncreateCmdObj(string spName ,SqlParameter[] commandParameters,SqlTransaction transaction)
{

SqlCommand command ;
try
{
command = new SqlCommand();
//if we were provided a transaction, assign it and Make Connection .
//Else associate the connection with the command
SqlConnection mObjSqlCn = new SqlConnection();
if (mObjSqlCn.State != ConnectionState.Open)
{
mObjSqlCn.Open();
}

if (transaction != null)
{
command.Transaction = transaction;
command.Connection = transaction.Connection ;
}
else
{
command.Connection = mObjSqlCn ;
}

//set the command text (stored procedure name or SQL statement)
command.CommandText = spName;

//set the command type
command.CommandType = CommandType.StoredProcedure;

//attach the command parameters if they are provided
if (commandParameters != null)
{
foreach (SqlParameter p in commandParameters)
{
//check for derived output value with no value assigned
if ((p.Direction == ParameterDirection.InputOutput) && (p.Value == null))
{
p.Value = DBNull.Value;
}

command.Parameters.Add(p);
}
}

return command;

}
catch (Exception ex)
{
Console.Write ("Error in connection : "+ex.Message);
}
finally
{

}

}

Thank you
Shrikant
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Error 'not all code paths return a value'

 
0
  #2
Aug 3rd, 2005
problem is nothing is returend if the try block throws an exception and it is caught by the catch block. Simplest way to fix this is to return a "blank" sqlcommand. Change
  1. catch (Exception ex)
  2. {
  3. Console.Write ("Error in connection : "+ex.Message);
  4. }
to
  1. catch (Exception ex)
  2. {
  3. Console.Write ("Error in connection : "+ex.Message);
  4. return new SqlCommand();
  5. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 3
Reputation: shrikant76 is an unknown quantity at this point 
Solved Threads: 0
shrikant76 shrikant76 is offline Offline
Newbie Poster

Re: Error 'not all code paths return a value'

 
0
  #3
Aug 4th, 2005
Thanks A lot Campkev
It worked ...But I dont understand the concept bhind this ...
Why we need to retrun new instant of an object in the Catch block ?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Error 'not all code paths return a value'

 
0
  #4
Aug 4th, 2005
you don't have to return a new instance, but you have to return something in all cases. The way you had it set up, if it threw an exception, you caught the exception and put up an error but never returned anything.
Another way you could handle it would be to get rid of the return from within the try block. In the catch block, set command to something. And put the return statement in the finally block. Which is probably how I would handle it.
Maybe something like
  1. catch (Exception ex)
  2. {
  3. Console.Write ("Error in connection : "+ex.Message);
  4. command = new SqlCommand();
  5. command.CommandText = "Error";
  6. }
  7. finally{
  8. return command;
  9. }

Then, when you call the function you can do something like:
  1. SqlCommand newCommand = fncreateCmdObj(name, Parameters, transaction);
  2. if (newCommand.CommandText !="Error")
  3. {
  4. //do whatever
  5. }

Hope this makes sense
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 3
Reputation: shrikant76 is an unknown quantity at this point 
Solved Threads: 0
shrikant76 shrikant76 is offline Offline
Newbie Poster

Re: Error 'not all code paths return a value'

 
0
  #5
Aug 5th, 2005
Thanks a lot for your help
I have got another small problem in code and giving following error while building my project
"'DAL.clsDataAccessLayer.mObjSqlCn' denotes a 'field' where a 'class' was expected."

I want to dispose the object in finally block but it is giving me above error.

public static SqlConnection fnConnection()
{
string lstrConn ;

try
{
//Retrive the connection string from the configuration file
lstrConn = System.Configuration.ConfigurationSettings.AppSettings["Connstr"];
//create a SqlConnection, and dispose of it after we are done.
SqlConnection mObjSqlCn = new SqlConnection(lstrConn);
//Open Connection
mObjSqlCn.Open() ;
return mObjSqlCn;

}
catch (Exception ex)
{
Console.Write ("Error in connection : "+ex.Message);
return new SqlConnection() ;
}
finally
{
mObjSqlCn.Dispose();
}

}
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Error 'not all code paths return a value'

 
0
  #6
Aug 5th, 2005
not sure about the error. But the way you have it set up. The finally block is never going to be reached.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 1
Reputation: MelissaSS is an unknown quantity at this point 
Solved Threads: 0
MelissaSS MelissaSS is offline Offline
Newbie Poster

Re: Error 'not all code paths return a value'

 
0
  #7
Apr 17th, 2007
I'm receiving the same error, when returning a string. How would I return an empty string?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Error 'not all code paths return a value'

 
0
  #8
Apr 17th, 2007
return "";
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC