Click here to Skip to main content
15,908,675 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a stored procedure which takes 2 parameters and do some manipulations and returns a dataset, How do I pass the two parameters to the stored procedure and retrieve the dataset , I have done some code but dont know whether it is right or wrong, please help, thanks in advance :)
C#
public static DataSet getDataSet(DateTime todayDate, DateTime previousDate)
        {
            string connString = "my connection string";
            SqlConnection con = new SqlConnection(connString);
            con.Open();

            SqlCommand cmd = new SqlCommand("cst_sp_vls_FetchCVUEData", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@startDate", SqlDbType.DateTime).Value =todayDate;
            cmd.Parameters.Add("@endDate", SqlDbType.DateTime).Value =previousDate;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds);
            
            return ds;
        } 
Posted
Comments
[no name] 16-Oct-14 6:50am    
What do you mean you "dont know whether it is right or wrong"? If it works when you run the code, it's right. If it does not work when you run your code, it's wrong.
George Jonsson 16-Oct-14 7:00am    
Looks like you follow a normal coding pattern.
However, if you don't get an error in your code, this is more of a request for best coding practice rather than a specific question for a problem.
One thing I would consider is to return a DataTable instead of a DataSet, because of a slightly easier coding, but that depends on if the answer you get from your SP contains one or more tables.
Sinisa Hajnal 16-Oct-14 7:11am    
How can you not know? It either returns dataset or it fails with some error...

Also you lack try...finally in which you close / dispose your command and connection objects.
Being0007 16-Oct-14 7:57am    
thanks for your valuable comments, will not repeat those mistakes again :)

1 solution

public DataSet getDataSet(DateTime todayDate, DateTime previousDate) {

string connString = "my connection string";
SqlConnection con = new SqlConnection(connString);
con.Open();


DataSet dataSet = new DataSet();
SqlParameter[] paramArray = new SqlParameter[2];

try
{
paramArray[0] = new SqlParameter("startDate", SqlDbType.DateTime, 5);
paramArray[0].Value = todayDate;

paramArray[1] = new SqlParameter(endDate, SqlDbType.DateTime, 5);
paramArray[1].Value = previousDate ;


dataSet = object.RetriveData("cst_sp_vls_FetchCVUEData", paramArray);
}

catch
{
throw;
}
return dataSet;


}

//this is RetriveData method.. (you can add it in this class or another class and making itz object and call that method)


public DataSet RetriveData(string SpName, SqlParameter[] OraParams)
{
DataSet dataSet = new DataSet();
try
{

using (SqlCommand sqlCommand = new SqlCommand(SpName,connectionString here))
{


sqlCommand.CommandType = CommandType.StoredProcedure;

foreach (SqlParameter sqlParam in SqlParams)
{
sqlCommand .Parameters.Add(sqlParam );
}

SqlDataAdapter SqlDataAdr = new SqlDataAdapter(sqlCommand);
SqlDataAdr.Fill(dataSet);


}

}
catch (Exception ex)
{
throw ex;
}
finally
{

}
return dataSet;
}
 
Share this answer
 
v3
Comments
Prabhani Panamulla 16-Oct-14 7:31am    
let me know if you have any question here
Being0007 16-Oct-14 7:58am    
hey thanks dude.... for the help :)

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