Dynamically binding data to a gridview.

Hello Experts,

We are using Model View Controller Framework in our ASP.net Application developed in C#,MS SQL Server.
For Eg. in one of my ASPX page I am binding data to the grid as shown below:

protected void LoadTasks()
            lstTaskLst.ListDataSource = TaskController.GetAllTasks(true,CurrentUser);

Here is the code in my Controller:
public static List<Task> GetAllTasks(bool active,UserInfo assignedTo)
            return TaskDAL.SelectAllToUser(active, assignedTo.UserID);

Here is code from my DAL:

public static List<Task> SelectAllToUser(bool? active, int assigned_to)
            List<Task> tasks = new List<Task>();
            Task currenttask = null;
GenericDatabase db = new GenericDatabase(z360.Configuration.CurrentConceptConnectionString(), );
            DbCommand dbCommand = db.GetStoredProcCommand("Calendar_Task_sel_only_By_User");

            IDataReader dbReader;

            if (active == null)
                db.AddInParameter(dbCommand, "active", DbType.String, DBNull.Value);
                db.AddInParameter(dbCommand, "active", DbType.String, active);

            db.AddInParameter(dbCommand, "assigned_to", DbType.Int32, assigned_to);

            dbReader = db.ExecuteReader(dbCommand);
            while (dbReader.Read())
                currenttask = LoadFromDataReader(dbReader);
                if (currenttask != null)

            if (tasks.Count == 0)
                tasks = null;

       return tasks;

And in LoadFromDataReader I am reading all the data from the datareader

It is working absolutely fine. But I have some other issue now.  Now instead of binding data from the Stored Procedure which is currently returning a list, is there a way I can bind it to a SQL Statement.  The reason is I have a wizard control where users will pick whatever columns they want.

Appreciate if you could throw some light on this.
Who is Participating?
YurichConnect With a Mentor Commented:
I don't have any ready to go samples. Where do you need more assistance: with a data set, parameterizing your stored procedure, or using an ad-hoc query in your application?
you can bind to a DataSet, why not just do that?
should give you the flexibility you're looking for...
you can parameterize your stored procedure and return the number of columns depending on your wizard's outcome.

Also, if you use standard DbCommand object, there is a property CommandText, like

DbCommand db = conn.CreateCommand();
db.CommandText = "SELECT CustomerName, " + SomeColumnName + ", ...";

rowmarkAuthor Commented:
Thanks steveberzins and Yurich.

Is there any sample code I can look at and try.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.