Solved

Dynamically binding data to a gridview.

Posted on 2007-04-03
4
240 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
  • 2
4 Comments
 
LVL 14

Expert Comment

by:steveberzins
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks steveberzins and Yurich.

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

Thanks
0
 
LVL 21

Accepted Solution

by:
Yurich earned 500 total points
Comment Utility
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 run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# Update Status Bar.... 1 22
Paging GridView 7 32
Binding a check box in an Asp.net DataGrid 4 25
Different Delete Messages 7 4
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now