Unable to add item to dataset in Linq to SQL

Posted by Mike B on Stack Overflow See other posts from Stack Overflow or by Mike B
Published on 2010-03-15T18:22:32Z Indexed on 2010/03/15 19:59 UTC
Read the original article Hit count: 285

Filed under:
|
|
|

I am having an issue adding an item to my dataset in Linq to SQL. I am using the exact same method in other tables with no problem. I suspect I know the problem but cannot find an answer (I also suspect all i really need is the right search term for Google). Please keep in mind this is a learning project (Although it is in use in a business)

I have posted my code and datacontext below. What I am doing is:

Create a view model (Relevant bits are shown) and a simple wpf window that allows editing of 3 properties that are bound to the category object. Category is from the datacontext. Edit works fine but add does not. If I check GetChangeSet() just before the db.submitChanges() call there are no adds, edits or deletes.

I suspect an issue with the fact that a Category added without a Subcategory would be an orphan but I cannot seem to find the solution.

Command code to open window:

CategoryViewModel vm = new CategoryViewModel();
AddEditCategoryWindow window = new AddEditCategoryWindow(vm);
window.ShowDialog();

ViewModel relevant stuff:

public class CategoryViewModel : ViewModelBase
{
    public Category category { get; set; }

    // Constructor used to Edit a Category
    public CategoryViewModel(Int16 categoryID)
    {
        db = new OITaskManagerDataContext();
        category = QueryCategory(categoryID);
    }

    // Constructor used to Add a Category
    public CategoryViewModel()
    {
        db = new OITaskManagerDataContext();
        category = new Category();
    }
}

The code for saving changes:

// Don't close window unless all controls are validated
if (!vm.IsValid(this)) return;
var changes = vm.db.GetChangeSet(); // DEBUG
try
{
    vm.db.SubmitChanges(ConflictMode.ContinueOnConflict);
}
catch (ChangeConflictException)
{
    vm.db.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
    vm.db.SubmitChanges();
}

The Xaml (Edited fror brevity):

<TextBox Text="{Binding category.CatName, 
                Mode=TwoWay, 
                ValidatesOnDataErrors=True, 
                UpdateSourceTrigger=PropertyChanged}" />
 <TextBox Text="{Binding category.CatDescription, 
                ValidatesOnDataErrors=True, 
                UpdateSourceTrigger=PropertyChanged}" />
 <CheckBox IsChecked="{Binding category.CatIsInactive, Mode=TwoWay}" />

Data Context

  • IssCategory in the Issues table is the old, text based category. This field is no longer used and will be removed from the database as soon as this is working and pushed live.

© Stack Overflow or respective owner

Related posts about linq-to-sql

Related posts about LINQ