943,962 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 93588
  • C# RSS
Aug 3rd, 2005
0

Error 'not all code paths return a value'

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shrikant76 is offline Offline
3 posts
since Aug 2005
Aug 3rd, 2005
1

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

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
C# Syntax (Toggle Plain Text)
  1. catch (Exception ex)
  2. {
  3. Console.Write ("Error in connection : "+ex.Message);
  4. }
to
C# Syntax (Toggle Plain Text)
  1. catch (Exception ex)
  2. {
  3. Console.Write ("Error in connection : "+ex.Message);
  4. return new SqlCommand();
  5. }
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
Aug 4th, 2005
0

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

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 ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shrikant76 is offline Offline
3 posts
since Aug 2005
Aug 4th, 2005
0

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

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
C# Syntax (Toggle Plain Text)
  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:
C# Syntax (Toggle Plain Text)
  1. SqlCommand newCommand = fncreateCmdObj(name, Parameters, transaction);
  2. if (newCommand.CommandText !="Error")
  3. {
  4. //do whatever
  5. }

Hope this makes sense
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
Aug 5th, 2005
0

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

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();
}

}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shrikant76 is offline Offline
3 posts
since Aug 2005
Aug 5th, 2005
0

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

not sure about the error. But the way you have it set up. The finally block is never going to be reached.
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
Apr 17th, 2007
0

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

I'm receiving the same error, when returning a string. How would I return an empty string?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MelissaSS is offline Offline
1 posts
since Apr 2007
Apr 17th, 2007
0

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

return "";
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
Oct 7th, 2010
0
Re: Error 'not all code paths return a value'
C# Syntax (Toggle Plain Text)
  1. private bool isLogin(String username, String pwd)
  2. {
  3.  
  4. string CurrentUser = "";
  5. string CurrentPwd = "";
  6. XmlDocument xd = new XmlDocument();
  7. xd.Load(path);
  8.  
  9. XmlNodeList xnl = xd.GetElementsByTagName("User");
  10.  
  11. foreach (XmlNode xn in xnl)
  12. {
  13. XmlNodeList cxnl = xn.ChildNodes;
  14. foreach (XmlNode cxn in cxnl)
  15. {
  16. if (cxn.Name == "username")
  17. {
  18. if (cxn.InnerText == username)
  19. {
  20. CurrentUser = username;
  21. }
  22. }
  23.  
  24. if (cxn.Name == "password")
  25. {
  26. if (cxn.InnerText == pwd)
  27. {
  28. CurrentPwd = pwd;
  29. }
  30. }
  31. }
  32.  
  33. if ((CurrentUser != "") & (CurrentPwd != ""))
  34. {
  35. return true;
  36. }
  37. else
  38. return false;
  39. }
  40.  
  41. }

having erros that all part does not return value
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ravichaudhary is offline Offline
1 posts
since Oct 2010
Oct 8th, 2010
0
Re: Error 'not all code paths return a value'
The reason is that you are returning True/False withing Foreach loop. cosider the case if your XML file doesn't have "User" node. Then your ForEach loop never gets execute. So your function will never return a value..

So put your If condition outside the ForEach loop... like below
C# Syntax (Toggle Plain Text)
  1. foreach((XmlNode xn in xnl)
  2. {
  3. }
  4. if ((CurrentUser != "") & (CurrentPwd != ""))
  5. return true;
  6. else
  7. return false;

Try by this way and let us know if you still face any trouble...


C# Syntax (Toggle Plain Text)
  1. private bool isLogin(String username, String pwd)
  2. {
  3.  
  4. string CurrentUser = "";
  5. string CurrentPwd = "";
  6. XmlDocument xd = new XmlDocument();
  7. xd.Load(path);
  8.  
  9. XmlNodeList xnl = xd.GetElementsByTagName("User");
  10.  
  11. foreach (XmlNode xn in xnl)
  12. {
  13. XmlNodeList cxnl = xn.ChildNodes;
  14. foreach (XmlNode cxn in cxnl)
  15. {
  16. if (cxn.Name == "username")
  17. {
  18. if (cxn.InnerText == username)
  19. {
  20. CurrentUser = username;
  21. }
  22. }
  23.  
  24. if (cxn.Name == "password")
  25. {
  26. if (cxn.InnerText == pwd)
  27. {
  28. CurrentPwd = pwd;
  29. }
  30. }
  31. }
  32.  
  33. if ((CurrentUser != "") & (CurrentPwd != ""))
  34. {
  35. return true;
  36. }
  37. else
  38. return false;
  39. }
  40.  
  41. }

having erros that all part does not return value
Reputation Points: 17
Solved Threads: 56
Posting Whiz in Training
rohand is offline Offline
293 posts
since Mar 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC