Solved

Dynamic list creation

Posted on 2009-04-14
14
233 Views
Last Modified: 2013-12-17
Greetings all

I have a method that returns the results of stored procedure, selected by the user. I would like to return that data to the UI layer via a generic list. The problem is that I have no way of knowing how many fields or what type comes back.

Is this possible using a List/Class in a similiar way to the attached snippet (this is just an example of a simple case) ??


In advance, thanks!!!

    allanamrk
SAMPLE CLASS
 
List<EMailAddress> emailContacts = new List<EMailAddress>();
 
using System;
using System.Collections.Generic;
using System.Text;
 
namespace DataTransferObjects
{
    public class EMailAddress
    {
 
        private string name;
        private string email;
        
        public EMailAddress()
        {
 
        }
 
        public EMailAddress(string pName, string pEmail)
        {
            name = pName;
            email = pEmail;
        }
 
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
 
        public string Email
        {
            get { return email; }
            set { email = value; }
        }
 
    }
}

Open in new window

0
Comment
Question by:allanmark
  • 6
  • 6
14 Comments
 
LVL 27

Expert Comment

by:Dabas
ID: 24145856
What about returning a list of DataRows?

List<DataRow> MyDataRowList = new List<DataRow>();

Dabas
0
 

Author Comment

by:allanmark
ID: 24145960
I tried that, but I need to work with the data in a GridView. When I linked the List (of DataRow) to the grid, it complained that there was no DATAFIELD - what I had was a list of array.
0
 
LVL 27

Expert Comment

by:Dabas
ID: 24146059
Show me the code where you bind to the grid
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:allanmark
ID: 24146098
queryResults =
    BusinessLogic.Business.GetQueryResults(.......);

// Binding
gvResults.DataSource = queryResults;
 gvResults.DataBind();
0
 
LVL 27

Expert Comment

by:Dabas
ID: 24146689
And the code for GetQueryResults?
0
 

Author Comment

by:allanmark
ID: 24146843
Ooops!!!

Sorry!  I'm having a really blonde day!! (no offense intended, if you happen to be blonde!!!).


 public static List<DataRow> GetQueryResults(string procName, List<DbParam> dbParms, string [] fieldNames)
        {
            List<DataRow> queryResults = new List<DataRow>();
 
            queryResults = DataAccess.ReturnQueryResults(procName, dbParms);
 
            return queryResults;
        }
 
 
public static List<DataRow> ReturnQueryResults(string procName, List<DbParam> dbParms)
{
            List<DataRow> returnedRecords = new List<DataRow>();
                        
            SqlParameter workParam;
 
            SqlConnection sqlConn = new SqlConnection(connectStr);
 
            DataSet dsQuery = new DataSet();
                   
            SqlDataAdapter daQuery = new SqlDataAdapter(procName, sqlConn);
            daQuery.SelectCommand.CommandType = CommandType.StoredProcedure;
 
            if (dbParms.Count > 0)
            {
                foreach (DbParam parm in dbParms)
                {
                    workParam = GetParameter(parm.ParmName, parm.ParmType, parm.ParmVal);
                    daQuery.SelectCommand.Parameters.Add(workParam);
                }
            }
         
            daQuery.Fill(dsQuery, "QueryResults");
 
            List <DataRow> returnedRecords = new List<DataRow>(dsQuery.Tables["QueryResults"].Select());
 
            return returnedRecords;
}

Open in new window

0
 
LVL 27

Accepted Solution

by:
Dabas earned 300 total points
ID: 24146906
Thanks

I suggest that you try filling a DataTable instead of a DataSet
Then instead of returning a List of rows, return the DataTable.

DataGridViews work very well with DataTables

Dabas
0
 

Author Comment

by:allanmark
ID: 24147112
COuld do that.
It's just that we have been encouraged to work with Lists, so as to achieve independance/separattion between the DataLayer and the UI and also to n=minimise reliance on System.Data.

0
 
LVL 27

Expert Comment

by:Dabas
ID: 24147235
It could be the right approach, except for your statement 'The problem is that I have no way of knowing how many fields or what type comes back'

I do not see anything wrong with reliance on System.Data. The same can be argued against reliance on System.Collections.

Dabas
0
 

Author Comment

by:allanmark
ID: 24147348
Ok.

So maybe what I need to do is work with the lists, where I have a definite structure that is returned and where it is something flexible / unknown (shoudl only be this one case) return a datatable.

Does that sound like an acceptable "work-around" compromsie, to you?
0
 
LVL 27

Expert Comment

by:Dabas
ID: 24147363
Yes
0
 

Author Closing Comment

by:allanmark
ID: 31569876
Many thanks!!!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Disable Security Alert  popup in Winforms  embedded webbrowser. 1 25
VB: Convert 2 dates to specific format 24 48
Problem!!! 8 33
write xml in vb.net 2 24
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

813 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

12 Experts available now in Live!

Get 1:1 Help Now