views:

18

answers:

0

i get all column values from Users to StandardUsers id,Name,SurName and PostCount data to add. But Return below error:

IN " entityUsers.GetType().GetProperty(columnNames[i]).SetValue(entityUsers, columnValues[i], null);" Return To Me Error : Set method of characteristics not found.


 public static class MyDynamicInsertCustomerExtension
    {

        public static bool DynamicInsertCustomerExtension(this TestEntities db, string[] columnNames, object[] columnValues)
        {
            try
            {
                if (columnNames.Length == 0 || columnValues.Length == 0)
                {
                    throw new Exception("Kolon ismi veya degeri yanlış!");
                }
                if (columnNames.Length != columnValues.Length)
                {
                    throw new Exception("Kolon ismi ve degeri eşit uzunlukta degil!");
                }

                standardUsers entityUsers = new standardUsers();

                for (int i = 0; i < columnNames.Length; i++)
                {
                    entityUsers.GetType().GetProperty(columnNames[i]).SetValue(entityUsers, columnValues[i], null);

                }
                db.AddTostandardUsers(entityUsers);
                db.SaveChanges();
                return true;

            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message); 
            }

        }
    }

Main.cs


 private void Form1_Load(object sender, EventArgs e)
        {
            SaveUsers();
        }

        protected void SaveUsers()
        {
            bool isOk = true;
            using (TestEntities userCtx = new TestEntities())
            {
                List<Users> UserList = new List<Users>();
                UserList = (from user in userCtx.Users select user).ToList();
                ArrayList columnValues = new ArrayList();
                ArrayList columnNames = new ArrayList(); 
                string[] _columnNames;
                object[] _columnValues;
                foreach (var myobject in UserList)
                {
                    foreach (PropertyInfo info in myobject.GetType().GetProperties())
                    {
                        if (info.CanRead)
                        {
                            object ValueUser = info.GetValue(myobject, null);
                            columnValues.Add(ValueUser);
                            string NameUser = info.Name.ToString();
                            columnNames.Add(NameUser);
                        }
                    }
                    _columnNames = columnNames.ToArray(typeof(string)) as string[];
                    _columnValues = columnValues.ToArray(typeof(object)) as object[];

                    isOk = userCtx.DynamicInsertCustomerExtension(_columnNames, _columnValues);
                }
            }
        }

All sql querys for entity:

CREATE TABLE Users
(
   id INT IDENTITY(1,1) PRIMARY KEY,
   Name NVARCHAR(50), 
   SurName NVARCHAR(50),
   PostCount  int
);

go

CREATE TABLE standardUsers
(
   id INT IDENTITY(1,1) PRIMARY KEY,
   Name NVARCHAR(50), 
   SurName NVARCHAR(50),
   PostCount  int
);

go

CREATE TABLE ElementaryUsers
(
   id INT IDENTITY(1,1) PRIMARY KEY,
   Name NVARCHAR(50), 
   SurName NVARCHAR(50),
   PostCount  int
);

go

CREATE TABLE PremiumUsers
(
   id INT IDENTITY(1,1) PRIMARY KEY,
   Name NVARCHAR(50), 
   SurName NVARCHAR(50),
   PostCount  int
);

alt text alt text