Hi friends,
I want to write a method to return a DataTable Object.. Input parameters can be overloaded depending on parameters required by Stored Procedure..
How can i write this please guide as i am new to c#
How to write a method to return a DataTable Object?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jul 18, 2012, 9:11 PM EDT
params SqlParameter[] arrParam)
{
DataTable dt = new DataTable();
using (SqlConnection cnn = new SqlConnection(
"Data Source=.\sqlexpress;Initial Catalog=NorthWind;
Integrated Security=True"))
{
cnn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = cnn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = storedProcedureName;
if (arrParam != null)
{
foreach (SqlParameter param in arrParam)
cmd.Parameters.Add(param);
}
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
}
}
}
return dt;
}
rani m mk47 reputationPosted Jul 18, 2012, 11:16 PM EDT