| | |
Error 'not all code paths return a value'
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2005
Posts: 3
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jul 2005
Posts: 483
Reputation:
Solved Threads: 19
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
to
C# Syntax (Toggle Plain Text)
catch (Exception ex) { Console.Write ("Error in connection : "+ex.Message); }
C# Syntax (Toggle Plain Text)
catch (Exception ex) { Console.Write ("Error in connection : "+ex.Message); return new SqlCommand(); }
•
•
Join Date: Jul 2005
Posts: 483
Reputation:
Solved Threads: 19
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
Then, when you call the function you can do something like:
Hope this makes sense
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)
catch (Exception ex) { Console.Write ("Error in connection : "+ex.Message); command = new SqlCommand(); command.CommandText = "Error"; } finally{ return command; }
Then, when you call the function you can do something like:
C# Syntax (Toggle Plain Text)
SqlCommand newCommand = fncreateCmdObj(name, Parameters, transaction); if (newCommand.CommandText !="Error") { //do whatever }
Hope this makes sense
•
•
Join Date: Aug 2005
Posts: 3
Reputation:
Solved Threads: 0
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();
}
}
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();
}
}
![]() |
Similar Threads
Other Threads in the C# Forum
- Previous Thread: I need a code for sending e-mail
- Next Thread: Help with database
| Thread Tools | Search this Thread |
.net access algorithm api array asp.net barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom database databaseconnection datagrid datagridview dataset datetime dbconnection decryption degrees design development draganddrop drawing encryption enum eventhandlers excel file firefox form format forms function gdi+ grantorrevokepermissionthroughc#.net image index input install java label libraries list listbox loop mandelbrot marshalbyrefobject math mouseclick movingimage mysql operator path photoshop php picturebox pixelinversion platform post programming radians regex remoting resourcefile richtextbox server sleep socket sql statistics stream string study system.servicemodel table tcpclientchannel text textbox thread time timer update usercontrol validation visualstudio webbrowser windows winforms wpf wpfc# xml





