Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4170682
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T00:21:59+00:00 2026-05-21T00:21:59+00:00

I have a simple application that reads a list of Albums from a DataBase

  • 0

I have a simple application that reads a list of Albums from a DataBase and fills a ListBox (AlbumShowCase).
Whenever a ListBoxItem is selected I update a DataGrid (trackDataGrid) with the list of tracks in that Album (also from the DataBase).

The problem is, I can edit the items in the DataGrid, and for all the existing tracks, changes are persistent. But if I try to add a new track, once I finish editing the row I get the System.NullReferenceException.

private TunesDBDataContext db;

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    db = new TunesDBDataContext("TunesDB.sdf");
    var query = from a in db.Albums select new AlbumCase(a);
    AlbumShowCase.ItemsSource = query;
}

private void trackDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    db.SubmitChanges();
}

private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var query = from a in db.Albums
                where a.AlbumID == ((AlbumCase)e.AddedItems[0]).Album.AlbumID
                select a.Tracks;

    trackDataGrid.ItemsSource = query;
}

The Exception occurs right after my ValueConverter:

[ValueConversion(typeof(String), typeof(int))]
public class TimeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int time = (int)value;
        TimeSpan ts = TimeSpan.FromSeconds(time);
        return ts.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        // The validation runs before this, so we know that if we got here
        // the data must be valid and won't throw an exception.
        return (int)TimeSpan.Parse((string)value).TotalSeconds;
        // THE EXCEPTION OCCURS AFTER THIS LINE FOR NEW ROWS
    }

The TimeConverter is paired with a TimeConverterRule that makes sure the track length entered is valid, and for all I know it’s working fine.
It’s just when the user edits the LAST row (the empty one) of the DataGrid that the crash occurs. And here’s the stack trace:

System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=PresentationFramework
StackTrace:
    at System.Windows.Data.BindingExpression.IsValidValueForUpdate(Object value, Type sourceType)
    at System.Windows.Data.BindingExpression.ConvertProposedValue(Object value)
    at System.Windows.Data.BindingExpression.ValidateAndConvertProposedValue(Collection1& values)
    at System.Windows.Controls.DataGridHelper.ValidateWithoutUpdate(FrameworkElement element)
    at System.Windows.Controls.DataGridColumn.CommitCellEdit(FrameworkElement editingElement)
    at System.Windows.Controls.DataGridColumn.CommitEdit(FrameworkElement editingElement)
    at System.Windows.Controls.DataGridCell.CommitEdit()
    at System.Windows.Controls.DataGrid.OnExecutedCommitEdit(ExecutedRoutedEventArgs e)
    at System.Windows.Controls.DataGrid.OnExecutedCommitEdit(Object sender, ExecutedRoutedEventArgs e)
    at System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
    at System.Windows.Input.CommandManager.ExecuteCommandBinding(Object sender, ExecutedRoutedEventArgs e, CommandBinding commandBinding)
    at System.Windows.Input.CommandManager.FindCommandBinding(CommandBindingCollection commandBindings, Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
    at System.Windows.Input.CommandManager.FindCommandBinding(Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
    at System.Windows.Input.CommandManager.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
    at System.Windows.UIElement.OnExecutedThunk(Object sender, ExecutedRoutedEventArgs e)
    at System.Windows.Input.ExecutedRoutedEventArgs.InvokeEventHandler(Delegate genericHandler, Object target)
    at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
    at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
    at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
    at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
    at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
    at System.Windows.Input.RoutedCommand.ExecuteImpl(Object parameter, IInputElement target, Boolean userInitiated)
    at System.Windows.Input.RoutedCommand.Execute(Object parameter, IInputElement target)
    at System.Windows.Controls.DataGrid.EndEdit(RoutedCommand command, DataGridCell cellContainer, DataGridEditingUnit editingUnit, Boolean exitEditMode)
    at System.Windows.Controls.DataGrid.CommitAnyEdit()
    at System.Windows.Controls.DataGrid.OnEnterKeyDown(KeyEventArgs e)
    at System.Windows.Controls.DataGrid.OnKeyDown(KeyEventArgs e)
    etc...etc...
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T00:21:59+00:00Added an answer on May 21, 2026 at 12:21 am

    My suspicion is that this is because you are binding to the results of a LINQ to SQL query. When you edit the row, WPF tries to “Add” the new item to your query – but it doesn’t support adding.

    Try something like this:

    var query = from a in db.Albums
                where a.AlbumID == ((AlbumCase)e.AddedItems[0]).Album.AlbumID
                select a.Tracks;
    
    var dataSource = new ObservableCollection<Track>();
    foreach (var item in query)
        dataSource.Add(item);
    
    trackDataGrid.ItemsSource = dataSource;
    

    You might then need to subscribe to events on the grid so that when an item is added, you add it to the DbContext.

    Also, make sure AlbumCase is a public class with a public, parameterless constructor. That’s because WPF will try to “new” one up to set properties on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently i have an application that reads and writes several properties from one or
I have a silverlight 3 application, that fetches some simple data from a ms-sql-server
I have a simple application that fetches some data from ONE table on db
I have a simple application that loads an unmanaged dll and passes a few
I have a simple c++ application that generates reports on the back end of
I have a simple WPF application that uses ClickOnce to handle installing. Within this
I have a simple iPhone application that is very similar to the Page Control
I have a simple Android application that uses a JAR I have built. When
I have a simple test application (C# console application) that does an HTTP GET
I have a simple Desktop Facebook application that allows the user to retrieve some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.