0

I have the following code that picks up the data from a users form as follows and writes to a database:

public class Member_Extended : Member
{

    public string Source { get; set; }
    public string Other { get; set; }
}
[WebMethod(EnableSession = true)]

public Dictionary<string, object> CreateMemeber(Member_Extended member) {
    var original = member != null ? MemberExtended_Member.Compile().Invoke(member) : null;
    Dictionary<string, object> data = new Dictionary<string, object>();
    context.Connection.Open();
    int success = 0;
    using (DbTransaction transaction = context.Connection.BeginTransaction()) {
        try {

            original.isPending = true;
            original.ExpiresOn = DateTime.Now.AddDays(14);

            context.AddToMember(original);
            success = context.SaveChanges();

            if (success == 1) {
                transaction.Commit();


                CacheManager.MemberData =original;  // how to pass original into member
                 MemeberExtendedRepository.Insert(member)
            }
            Utils.ServiceSuccessResultHandler(ref data, "Login," + original.ID, 1);
        } catch (Exception ex) {
            transaction.Rollback();
            Utils.ServiceErrorResultHandler(ex, ref data);
        }

    }

    return data;
}

public class MemeberExtendedRepository
{

    public static bool Insert (Member_Extended member)
    {
        var command = new StringBuilder();
        command.AppendFormat(string.Format("UPDATE Table SET [Source]='{0}',[Other]='{1}' WHERE  ID={2} ", member.Source, member.Other, member.ID));
        return ExecuteCommand(command.ToString());
    }
}

as you can see on this line var original = member != null ? MemberExtended_Member.Compile().Invoke(member) : null; i assigned the member to original. Because original contains a "ID" parameter value example 1234. when i call this method MemeberExtendedRepository.Insert(member) i want the ID VALUE from the original variable to be passed,currently it returns an ID of 0.

when i tried the following MemeberExtendedRepository.Insert(original); it says can not convert member to Member_Extended.

How can i achieve this?

Stefan Becker
  • 5,695
  • 9
  • 20
  • 30
marry
  • 153
  • 2
  • 3
  • 15
  • Possible duplicate of [Is it possible to assign a base class object to a derived class reference with an explicit typecast?](https://stackoverflow.com/questions/729527/is-it-possible-to-assign-a-base-class-object-to-a-derived-class-reference-with-a) – ProgrammingLlama Feb 15 '19 at 07:39
  • Let me know if this isn't what you're asking and I'll retract my vote. – ProgrammingLlama Feb 15 '19 at 07:39
  • @john i tried those methods but i still dont understand as to how to get it to work – marry Feb 15 '19 at 08:11
  • It's more a case that you can't cast a base class to a more derived type. – ProgrammingLlama Feb 15 '19 at 08:12
  • any idea on a different way of doing so i can achieve the object – marry Feb 15 '19 at 08:28
  • There are many options on the other question I linked. The simplest is to create two constructors for `Member_Extended`: one constructorless, and the other which takes an instance of `Member` and manually copies each property value. – ProgrammingLlama Feb 15 '19 at 08:29
  • @john thanks,its okay i figured it out – marry Feb 15 '19 at 08:53

0 Answers0