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

Edited 3 Years Ago by pyTony: fixed formating

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

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

to

catch (Exception ex)
{
Console.Write ("Error in connection : "+ex.Message);
return new SqlCommand();
}
Comments
Thanks, I did not know what I was doing wrong as I thought I returned a value. I didn't know C# checks all situations.

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 ?

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

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:

SqlCommand newCommand = fncreateCmdObj(name, Parameters, transaction);
if (newCommand.CommandText !="Error")
{
//do whatever
}

Hope this makes sense

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

        }

Edited 3 Years Ago by pyTony: fixed formating

not sure about the error. But the way you have it set up. The finally block is never going to be reached.

I'm receiving the same error, when returning a string. How would I return an empty string?

private bool isLogin(String username, String pwd)
        {

            string CurrentUser = "";
            string CurrentPwd = "";
         XmlDocument xd = new XmlDocument();
        xd.Load(path);

        XmlNodeList xnl = xd.GetElementsByTagName("User");

        foreach (XmlNode xn in xnl)
        {
            XmlNodeList cxnl = xn.ChildNodes;
            foreach (XmlNode cxn in cxnl)
            {
                if (cxn.Name == "username")
                {
                    if (cxn.InnerText == username)
                    {
                        CurrentUser = username;
                    }
                }

                if (cxn.Name == "password")
                {
                    if (cxn.InnerText == pwd)
                    {
                        CurrentPwd = pwd;
                    }
                }
            }

            if ((CurrentUser != "") & (CurrentPwd != ""))
            {
                return true;
            }
            else
                return false;
        }

        }

having erros that all part does not return 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

foreach((XmlNode xn in xnl)
{
}
if ((CurrentUser != "") & (CurrentPwd != ""))
    return true;
else
    return false;

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

private bool isLogin(String username, String pwd)
        {

            string CurrentUser = "";
            string CurrentPwd = "";
         XmlDocument xd = new XmlDocument();
        xd.Load(path);

        XmlNodeList xnl = xd.GetElementsByTagName("User");

        foreach (XmlNode xn in xnl)
        {
            XmlNodeList cxnl = xn.ChildNodes;
            foreach (XmlNode cxn in cxnl)
            {
                if (cxn.Name == "username")
                {
                    if (cxn.InnerText == username)
                    {
                        CurrentUser = username;
                    }
                }

                if (cxn.Name == "password")
                {
                    if (cxn.InnerText == pwd)
                    {
                        CurrentPwd = pwd;
                    }
                }
            }

            if ((CurrentUser != "") & (CurrentPwd != ""))
            {
                return true;
            }
            else
                return false;
        }

        }

having erros that all part does not return value

This article has been dead for over six months. Start a new discussion instead.