troubleshooting Question

How can i convert  the contents of a list into a dataset and make it available anywhere in my application

Avatar of Olukayode Oluwole
Olukayode OluwoleFlag for Canada asked on
C#Crystal Reports
11 Comments1 Solution109 ViewsLast Modified:
In my c# application I need to read a set of data  from a table "stateorigin"
and get it ready as a dataset  for preparing a crystal report

I have 2 scripts

My first script defines the  fields in the dataset ( see script below)

  //Creates a new dataset, 2 datatables and adds data to them
   _dsData = new DataSet("TestDataSet");

  private void CreateDataSet2()
        {
            //Creates a new dataset, 2 datatables and adds data to them
            _dsData = new DataSet("TestDataSet");

            //byte[] arrBytes;
            //System.IO.MemoryStream ms;

            using (DataTable dt = new DataTable("stateorigin"))
            {
                dt.Columns.Add("stateid", typeof(int));
                dt.Columns.Add("statedescription", typeof(string));
              
                GlobalConfig.Connection.GetStateCode_All();

                _dsData.Tables.Add(dt);
            }
         
        }

So basically I want to read 2 fields  stateid and statedescription  from stateorigin table

Already during the creation of my application i have  a script that get a list of these same fields BUT

into a list.  ( See  script below )

[public List<StateOriginModel> GetStateCode_All()
        {
             List<StateOriginModel> results = new List<StateOriginModel>();

            using (var conn = new NpgsqlConnection(pgrstring))
            {
                using (NpgsqlCommand command = new NpgsqlCommand("public.spstateorigincode_getall", conn))
                {
                    conn.Open();
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.Add(new NpgsqlParameter("countrycodex", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Input, Value = LoginDetails.staticcountry.ToUpper() });

                    var reader = command.ExecuteReader();
                   
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            StateOriginModel model = new StateOriginModel();

                           
                            if ((string)reader["StateID"] != null)
                            {
                                model.Id = (int)reader["id"];
                                model.StateID = (string)reader["StateID"];  
                                model.StateDescription = (string)reader["StateDescription"];  
                                results.Add(model);
                            }
                        }
                    }
                }
            }
            return results;
        }[/code]

Using the debugger i know that the List is populated  into a list called results

My questions are

1. Is there a way for me to  assign the contents of the list results to the dataset    _dsData

2. I need to make the contents of _dsData   available to the calling script 1
How do i define _dsData as a static class so that  i can get its data  anywhere in my application


Thanks

Olukay
ASKER CERTIFIED SOLUTION
Éric Moreau
Senior .Net Consultant
Join our community to see this answer!
Unlock 1 Answer and 11 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros