Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

how to fill Sql Table with Generic Reflection method?

Posted on 2011-04-19
2
Medium Priority
?
234 Views
Last Modified: 2012-06-21
Hi; I have 4 tables, one of them is main table also there is one to many relation between tables. TID is Foreign key and ID is PK. As a result. i don't want to fill table with classic method. I should access table property and generic <T> I want to set all TID to T_Table ,C_Table, Q_Table

MY CODES(this is test project not real project but logis is the same as real project) Below codes return to me ERROR( in first foreach loop): Null reference exception; Object reference not set to an instance of an object.
using System.Reflection;

namespace App.ReflectionToGeneric
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] PropertyNames = new string[] { "TID", "AnyID" };
            int[] Vals = new int[] { 1, 2 };
            new DataManager().Save<QTable>(PropertyNames, Vals);
        }
    }


    public class DataManager
    {
        IEnumerable<Table> list = new GetData().GetVals();
        public void Save<TModel>( string[] PropertyNames, int[] Vals ) where TModel : class, new()
        {

            var instance = new TModel();
            Type calcType = instance.GetType();
           // object calcInstance = Activator.CreateInstance(calcType);
            foreach (string PropertyName in PropertyNames)
            {
                // ERROR RETURN TO ME BELOW   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                calcType.GetProperty(PropertyName).SetValue(instance, Vals[0], null);
            }
            foreach (string PropertyName in PropertyNames)
            {
                Console.WriteLine(calcType.GetProperty(PropertyName).GetValue(instance, null).ToString());
            }

        }
   }

    public class GetData
    {
        public IEnumerable<Table> GetVals()
        {
            List<Table> list = new List<Table>();
            list.Add(new Table() { ID = 1, Name = "yusuf" });
            list.Add(new Table() { ID = 2, Name = "berkay" });
            return list;
        }
    }

    public class Table
    {
        internal int ID { get; set; }
        internal string Name { get; set; }
    }

    public class CTable
    {
        internal int ID { get; set; }
        internal int TID { get; set; }
        internal int AnyID { get; set; }
    }

    public class QTable
    {
        internal int ID { get; set; }
        internal int TID { get; set; }
        internal int AnyID { get; set; }
    }

    public class TTable
    {
        internal int ID { get; set; }
        internal int TID { get; set; }
        internal int AnyID { get; set; }
    }
}

Open in new window

generic reflection
0
Comment
Question by:programmerist
2 Comments
 

Author Comment

by:programmerist
ID: 35430628
ARE YOU THERE?
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 2000 total points
ID: 35432361
Can you debug the application and stop at the line that fails and check what is null? I suppose it will be the result of GetProperty(), because your properties are internal. You should make them public or call an overload of the GetProperty method which accepts some BindingFlags to get nonpublic properties. For more information you should check http://msdn.microsoft.com/en-us/library/zy0d4103.aspx.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Loops Section Overview
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

578 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