Click here to Skip to main content
15,884,057 members
Please Sign up or sign in to vote.
4.71/5 (4 votes)
Hello all

I have been working on a WPF app, and it's 99% finished, but I have a problem.

When I attempt to update a DataGrid after a database delete operation, I get the following, exception:

Non-static method requires a target

The Exception is happening inside the ViewModelBase when the View Model attempts to update the view, code:

C#
public class ViewModelBase : INotifyPropertyChanged
{
    public ViewModelBase()
    {
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    #region Private Methods

    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName)); // Exception here
        }
    }

    #endregion


The rest of the time the app runs as expected, with the code section causing no problems. The data comes back from the database without any problems.

Has anyone got any ideas ??

Thanks in advance.

Baxter-P


OK,

Thanks for the replies.

The comments about details are very fair, so, here is the property I am binding to:

XML
private ObservableCollection<UserData> userData = new ObservableCollection<UserData>();
public ObservableCollection<UserData> UserData
{
    get
    {
        return userData;
    }
    set
    {
        userData = value;
        RaisePropertyChanged("UserData");
    }
}



Also here is the class for the data:

C#
public class UserData
{
    public UserData(int id, string userName)
    {
        this.ID = id;
        this.UserName = userName;
    }

    public int ID { get; set; }
    public string UserName { get; set; }
}


Standard stuff. Also here is a screen shot of my DataGrid XAML code:

http://s30.postimg.org/rftcsp01d/xaml.png[^]

Also here is the code where the excepetion is caused:

C#
private void DeleteUserCommandExecute(Object parameter)
        {
            WPFMessageBox messageBox = new WPFMessageBox("Delete ?", "Are you sure you want to DELETE user '" + userName + "' ?", MessageBoxType.YesNo);

            if (messageBox.Answer != MessageBoxDecision.Yes)
                return;

            try
            {
                lookUpContext.DeleteUser(userID);
                lookUpContext.SaveChanges();

                RemoveAllUserDivisions();
                PopulateUserDivisions();

                GetUserData(); // Exception here

                UserName = String.Empty;
                AllowUIToUpdate();
            }
            catch (Exception ex)
            {
                WPFMessageBox errorMessageBox = new WPFMessageBox("Error", "Error DELETING data - " + ex.Message, MessageBoxType.OK);
            }
        }


Also, the call to the database:

C#
private void GetUserData()
{
    lookUpContext = new JMLookupEntities();

    var UserDataLocal = (from data in lookUpContext.JMUsers.ToList()
                         select new UserData(data.ID, data.UserName)).ToList();

    UserData = UserDataLocal.ToObservableCollection(); // This updates the view, or should do..
}


The event causing the exception is the DeleteUserCommand execute event. The exception occurs when I try to update the 'dgUsers' datasource property 'UserData' shown above. The code block to update the view is called multiple times before the problem occurs, with no problems.

I really do not understand this problem, and I have tried everything.

I am using WPF 4.5, and EF 6.

Thanks Again,

Baxter-P
Posted
Updated 19-Sep-14 0:03am
v4
Comments
Matt T Heffron 18-Sep-14 12:37pm    
This is almost verbatim the same as code I'm using which has been working for several years.
I don't see anything here that could be causing the issue...
There might be more info in the stack trace at the point of the exception.
Sorry I can't be more help.
Sergey Alexandrovich Kryukov 18-Sep-14 12:37pm    
Where is the implementation of your property You could have missed something there.
—SA
Matt T Heffron 18-Sep-14 12:57pm    
INotifyPropertyChanged does not require a dependency property.
From the name: ViewModelBase it's strongly implied that this is a common base class that other ViewModel classes will be derived from.
The relevant property could be in the derived class...
Sergey Alexandrovich Kryukov 18-Sep-14 13:01pm    
Yes, I know, agree... I tried to fix my question. I want to try any implementation of any relevant property. In other words, I would need to see a complete sample which I could use to reproduce the problem. It should be something simple, but not so easy to guess what.
—SA
Paulo Zemek 18-Sep-14 12:59pm    
I really believe the bug is in your property getter.
Also, are you sure you are notifying the right property name? Maybe a single letter can be the cause of your bug.

I have a solution of sorts.

I changed the control to a ListView from a DataGrid, and although I still got the exception the control now updates.

So I hid the exception inside a try/catch block, with some logic to only hide the specfific exception, not all exceptions.

So my app is fully finished, thanks for the support.
 
Share this answer
 
I found the following on StackOverflow (I Googled "Non-static method requires a target") and this was the first "hit":

http://stackoverflow.com/questions/13717355/non-static-method-requires-a-target[^]

It appears to be due to a null reference exception in the linq execution.
 
Share this answer
 
Comments
Baxter P 20-Sep-14 3:42am    
I saw that too, but I suspected the problem was unrelated to mine.

OK, so I have to test for NULLs like so:

var UserDataLocal = (from data in lookUpContext.JMUsers.ToList()
where data != null
select new UserData(data.ID, data.UserName)).ToList();

to be honest I thought the problem was a WPF one.

I try it on Monday. Cheers.
Baxter P 22-Sep-14 4:39am    
Tried the null entry check in the linq query, exception is still thrown. I have also tried updating the DBContext, but again exception persists.

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