Click here to Skip to main content
15,888,302 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All

I am creating a clone of an object using reflection. But at the end of the method, when I call dataContext.GetChangeSet(), it gives inserts:1.

Here is the code:


MyDataContext dataContext = new MyDataContext();


dataContext.Connection.Open();
dataContext.Transaction = dataContext.Connection.BeginTransaction();
DeliveryRegistration dr = null;
try
{
    DeliveryRegistration oldRegistration = null;
    object obj = null;
    string error = "";
    if (Utility.EmailNotificationsEnabled)
    {
        oldRegistration = RAKBankDataAccess.GetRegistrationById(dataContext, int.Parse(Request.QueryString["Id"]));

        if (Utility.CreateCopy((object)oldRegistration, out obj, out error))
        {

           oldRegistration = (DeliveryRegistration)obj;
        }
    }



And the CreateCopy method at the end of which datacontext has 1 inserts

C#
public static bool CreateCopy(object orignal, out object newCopy, out string error)
    {
        error = "";
        try
        {
            Type t = orignal.GetType();
            ConstructorInfo[] constructors = t.GetConstructors();
            newCopy = constructors[0].Invoke(null);
            PropertyInfo[] props = t.GetProperties();
            foreach (PropertyInfo prop in props)
            {
               if(prop.CanRead && prop.CanWrite && !prop.PropertyType.IsArray)
               {
                   prop.SetValue(newCopy, prop.GetValue(orignal, null), null);
               }
            }
        catch (Exception ex)
        {
            newCopy = null;
            return false;
        }
        return true;
    }



On submitting changes, a new entry is also created. Any Idea what might be causing this ?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900