Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public string ExecuteScalar(String storedprocedure, params SqlParameter[] arrParam)
         {
            SqlConnection cn = new SqlConnection(connection());
            try
            {
                //initialisation of datatable, Sql connection and sql command
                DataSet dt = new DataSet();

                SqlCommand cmd = new SqlCommand(storedprocedure, cn);
                cmd.CommandType = CommandType.StoredProcedure;

                //opens the db connection
                if (cn.State == ConnectionState.Closed || cn.State == ConnectionState.Broken)
                    cn.Open();

                //if sql param is not null then gets the parameters from that
                if (arrParam != null)
                {
                    foreach (SqlParameter param in arrParam)
                        cmd.Parameters.Add(param);
                }
                cmd.CommandTimeout = 5000;
                //executes the command and fills it into the datatable
              
                return string;
            }

            catch (Exception ee)
            {
                
                return null;
            }
            finally
            {
                cn.Close();
            }
        }

its showing error on return
it should return 2 thins if success true of false type or any string for success or failure
Posted
Updated 17-Nov-15 19:29pm
v2
Comments
Krunal Rohit 18-Nov-15 1:30am    
It shoudl return what ?

-KR

See below changes and confirm me:-
public string ExecuteScalar(String storedprocedure, params SqlParameter[] arrParam)
{
SqlConnection cn = new SqlConnection(connection());
try
{
//initialisation of datatable, Sql connection and sql command
DataSet dt = new DataSet();

SqlCommand cmd = new SqlCommand(storedprocedure, cn);
cmd.CommandType = CommandType.StoredProcedure;

//opens the db connection
if (cn.State == ConnectionState.Closed || cn.State == ConnectionState.Broken)
cn.Open();

//if sql param is not null then gets the parameters from that
if (arrParam != null)
{
foreach (SqlParameter param in arrParam)
cmd.Parameters.Add(param);
}
cmd.CommandTimeout = 5000;
//executes the command and fills it into the datatable

return "success";
}

catch (Exception ee)
{

return "error";
}
finally
{
cn.Close();
}
}
 
Share this answer
 
C#
public string ExecuteScalar(String storedprocedure, params SqlParameter[] arrParam)
         {
string _result = "failure";
            SqlConnection cn = new SqlConnection(connection());
            try
            {
                //initialisation of datatable, Sql connection and sql command
                DataSet dt = new DataSet();
 
                SqlCommand cmd = new SqlCommand(storedprocedure, cn);
                cmd.CommandType = CommandType.StoredProcedure;
 
                //opens the db connection
                if (cn.State == ConnectionState.Closed || cn.State == ConnectionState.Broken)
                    cn.Open();
 
                //if sql param is not null then gets the parameters from that
                if (arrParam != null)
                {
                    foreach (SqlParameter param in arrParam)
                        cmd.Parameters.Add(param);
                }
                cmd.CommandTimeout = 5000;
                //executes the command and fills it into the datatable
              
               _result = "success";
            } 
            catch (Exception e)
            {                
               _result = "exception";
            }
            finally
            {
                cn.Close();
            }
return _result;
        }
 
Share this answer
 
You have to return a string and not "string".
public string ExecuteScalar(String storedprocedure, params SqlParameter[] arrParam)
         {
            SqlConnection cn = new SqlConnection(connection());
            try
            {
                //initialisation of datatable, Sql connection and sql command
                DataSet dt = new DataSet();
 
                SqlCommand cmd = new SqlCommand(storedprocedure, cn);
                cmd.CommandType = CommandType.StoredProcedure;
 
                //opens the db connection
                if (cn.State == ConnectionState.Closed || cn.State == ConnectionState.Broken)
                    cn.Open();
 
                //if sql param is not null then gets the parameters from that
                if (arrParam != null)
                {
                    foreach (SqlParameter param in arrParam)
                        cmd.Parameters.Add(param);
                }
                cmd.CommandTimeout = 5000;
                //executes the command and fills it into the datatable
              
                return "success";
            }
 
            catch (Exception ee)
            {
                
                return "failer";
            }
            finally
            {
                cn.Close();
            }
        }
 
Share this answer
 
Comments
Member 11970398 18-Nov-15 4:25am    
when i am running my business logic i should get the value but i am getting success
Manoj Sawant 19-Nov-15 5:02am    
Which value you want to return

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900