[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Dynamically binding data to a gridview.

Posted on 2007-04-03
4
Medium Priority
?
250 Views
Last Modified: 2013-11-07
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);
            else
                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)
                    tasks.Add(currenttask);
            }
            dbReader.Close();

            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.
0
Comment
Question by:rowmark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 14

Expert Comment

by:steveberzins
ID: 18847099
you can bind to a DataSet, why not just do that?
should give you the flexibility you're looking for...
0
 
LVL 21

Expert Comment

by:Yurich
ID: 18847270
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 + ", ...";

cheers,
yurich
0
 

Author Comment

by:rowmark
ID: 18847968
Thanks steveberzins and Yurich.

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

Thanks
0
 
LVL 21

Accepted Solution

by:
Yurich earned 1500 total points
ID: 18854526
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?
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question