none
problem converting to a database RRS feed

  • Question

  • I have lifted some code which used a hard data source I need to convert this to access my DataBase my function returns Null can anyone tell me what I have to do?

    this is the original function which works

     public static Company[] GetCompanies()        {
                return new Company[]
                {
                     new Company("Duets"),
                     new Company("Teaching"),
                     new Company("Osbaldwick")
                };
            }

    This is  the function I have created which does appear to work returning the correct number of elements but they are all null

     public static Company[] GetCompanies()
            {
                BusinessLib.DataAccess.MenusDataDataContext MdC = new BusinessLib.DataAccess.MenusDataDataContext();
                var DishList = from c in MdC.tblCompanies
                                orderby c.Company
                                select new { c.Company };
                int dc = DishList.Count();
                foreach (var d in DishList)
                {
                    {
                        new Company(d.Company.TrimEnd());
                    };
                }
                return new Company[dc];
            }  

     


    jnc

    Thursday, May 24, 2012 10:43 AM

Answers

  • Hi jnc;

    The following should give the wanted results.

    public static Company[] GetCompanies()
    {
    
        BusinessLib.DataAccess.MenusDataDataContext MdC = new BusinessLib.DataAccess.MenusDataDataContext();
        var DishList = (from c in MdC.tblCompanies
                        orderby c.Company
                        select c.Company.TrimEnd()).ToArray();
                        
        int dc = DishList.Count();
        Company[] companies = new Company[dc];
        
        for( int i = 0; i < dc; i++ )
        {
            companies[i] = new Company(DishList[i]);
        }
    
        return companies;
        
    }

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by jnc Thursday, May 24, 2012 3:45 PM
    Thursday, May 24, 2012 3:36 PM

All replies

  • Note:

    I do realise now that return new Company[dc] will initialise the values which is why it returns null for each row but what I don't know is how to initialise Company BEFORE I start adding rows


    jnc

    Thursday, May 24, 2012 11:23 AM
  •  

    In order to give the best solution how is Company defined in your code?

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, May 24, 2012 2:49 PM
  • Many thanks, this is for binding to a WPF treeviewhence there is a reference to the next level in the  Treeview (Concept)

      public class Company
        {
            public Company(string CompanyName)
            {
                this.CompanyName = CompanyName;
            }
            public string CompanyName { get; private set; }
            readonly List<Concept> _companies = new List<Concept>();
            public List<Concept> Companies
            {
                get { return _companies; }
            }
        }


    jnc

    Thursday, May 24, 2012 2:52 PM
  • Hi jnc;

    The following should give the wanted results.

    public static Company[] GetCompanies()
    {
    
        BusinessLib.DataAccess.MenusDataDataContext MdC = new BusinessLib.DataAccess.MenusDataDataContext();
        var DishList = (from c in MdC.tblCompanies
                        orderby c.Company
                        select c.Company.TrimEnd()).ToArray();
                        
        int dc = DishList.Count();
        Company[] companies = new Company[dc];
        
        for( int i = 0; i < dc; i++ )
        {
            companies[i] = new Company(DishList[i]);
        }
    
        return companies;
        
    }

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by jnc Thursday, May 24, 2012 3:45 PM
    Thursday, May 24, 2012 3:36 PM
  • Is that it!!!

    How stupid of me, it works perfectly thanks again


    jnc

    Thursday, May 24, 2012 3:46 PM
  •   

    Sometime we over think a problem and we need that second pair of eyes to show us the way.

    Have a great day.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, May 24, 2012 4:05 PM
  • I was embedded too deep in the problem

    jnc

    Thursday, May 24, 2012 4:09 PM