0

Hi I am creating an MVVM application and I have my database linked up to the project, I have a WCF service added to my project and I have a service reference created so I can add new data from my application to my database.

In my application viewmodel I have a command that saves a new user to the database and I create a new instance of my service client within that C# file, and then the command is stored within the service file. However, when I go to call this command using my service I get the error: "the best overloaded method match for XXX has some invalid arguments" and I can't figure out why because everything seems to be added correctly.

Here is some of my code to show you what I have so far in the viewmodel, and it is under this line I get the error (_serviceClient.SaveNewUser(newUser);) :

 public RelayCommand AddUserCommand { get; private set; }
 private ServiceClient _serviceClient;

    public AddUserViewModel()
    {            
        _serviceClient = new ServiceClient();
        AddUserCommand = new RelayCommand(() => ExecuteAddUserCommand());          
    }

 private void ExecuteAddUserCommand()
    {
        User newUser = new User();
        newUser.FirstName = this.FirstName;
        newUser.LastName = this.LastName;
        newUser.ContactNumber = this.ContactNumber;
        newUser.Email = this.Email;


        //service call
        int userID = _serviceClient.SaveNewUser(newUser);


        newUser.UserID = userID;
     }

Then in the service.svc.cs I have:

    [OperationContract]
    public int SaveNewUser(User user)
    {
        using (var context = new SolutionEntities())
        {
            context.Users.AddObject(user);
            context.SaveChanges();
            return user.UserID;               

        }
    }

The User class looks like:

       public partial class Client : EntityObject
       {

    public static Client CreateClient(global::System.Int32 userID, global::System.String firstName, global::System.String lastName, global::System.String contactNumber)
    {
        User user = new User();
        user .UserID = userID;
        user .FirstName = firstName;
        user .LastName = lastName;
        user .ContactNumber = contactNumber;
        return user;
    }

    public global::System.Int32 UserID
    {
        get
        {
            return _UserID;
        }
        set
        {
            if (_UserID!= value)
            {
                OnUserIDChanging(value);
                ReportPropertyChanging("UserID");
                _UserID= StructuralObject.SetValidValue(value, "UserID");
                ReportPropertyChanged("UserID");
                OnUserIDChanged();
            }
        }
    }
    private global::System.Int32 _UserID;
    partial void OnUserIDChanging(global::System.Int32 value);
    partial void OnUserIDChanged();

    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.String FirstName
    {
        get
        {
            return _FirstName;
        }
        set
        {
            OnFirstNameChanging(value);
            ReportPropertyChanging("FirstName");
            _FirstName = StructuralObject.SetValidValue(value, false, "FirstName");
            ReportPropertyChanged("FirstName");
            OnFirstNameChanged();
        }
    }
    private global::System.String _FirstName;
    partial void OnFirstNameChanging(global::System.String value);
    partial void OnFirstNameChanged();

    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.String LastName
    {
        get
        {
            return _LastName;
        }
        set
        {
            OnLastNameChanging(value);
            ReportPropertyChanging("LastName");
            _LastName = StructuralObject.SetValidValue(value, false, "LastName");
            ReportPropertyChanged("LastName");
            OnLastNameChanged();
        }
    }
    private global::System.String _LastName;
    partial void OnLastNameChanging(global::System.String value);
    partial void OnLastNameChanged();


    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.String ContactNumber
    {
        get
        {
            return _ContactNumber;
        }
        set
        {
            OnContactNumberChanging(value);
            ReportPropertyChanging("ContactNumber");
            _ContactNumber = StructuralObject.SetValidValue(value, false, "ContactNumber");
            ReportPropertyChanged("ContactNumber");
            OnContactNumberChanged();
        }
    }
    private global::System.String _ContactNumber;
    partial void OnContactNumberChanging(global::System.String value);
    partial void OnContactNumberChanged();

    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
    [DataMemberAttribute()]
    public global::System.String Email
    {
        get
        {
            return _Email;
        }
        set
        {
            OnEmailChanging(value);
            ReportPropertyChanging("Email");
            _Email = StructuralObject.SetValidValue(value, true, "Email");
            ReportPropertyChanged("Email");
            OnEmailChanged();
        }
    }
    private global::System.String _Email;
    partial void OnEmailChanging(global::System.String value);
    partial void OnEmailChanged();

Hopefully someone can see where my problem lies, thanks.

RebeccaD
  • 143
  • 5
  • 12
  • And your User class looks like? – PM. Nov 21 '13 at 13:26
  • I don't have a class, User is an item from my database. The service first pulls the User back and then you can use it in the solution. It stores first name, last name, contact number and email address. – RebeccaD Nov 21 '13 at 13:28
  • @RebeccaD You **do** have a class: generated by Entity Framework for you on server side, and generated by svcutil (client proxy) for you on client side – ken2k Nov 21 '13 at 13:30
  • @ken2k is right, if you don't have this class then **public int SaveNewUser(User user) will not work** – PM. Nov 21 '13 at 13:32
  • Oh ok, I found the created class for me. I will add it to the question to see if it is causing the problem. Thanks. – RebeccaD Nov 21 '13 at 13:37
  • If XXX is `ServiceClient.SaveNewUser(User)`, then you have two different User classes, it seems. Check their types :) If XXX is not that, then what is it? – oerkelens Nov 21 '13 at 14:25
  • When you encounter an exception that confuses you, surround the bad code with a `try/catch` block and call `ToString()` on the exception. The result is the full details of the exception, any inner exceptions, and the call stack. This information will either be your answer, or if you [edit] and add this to your question, will help others decode your problem. –  Nov 21 '13 at 18:30

0 Answers0