1

I have a datagridview which is databound to an Entity Framework "table":

    public ObjectSet<TEntity> tableData { get; private set; }
    private BindingSource tableBinding;

    public AuxiliaryTableEditor(ObjectSet<TEntity> something)
    {
        InitializeComponent();

        this.tableData = something;
        this.tableBinding = new BindingSource();
        this.tableBinding.DataSource = tableData;

        this.auxiliaryTableEditorGridView.DataSource = tableBinding;
    }

This works perfectly fine.

The problem is this: If a user starts editing / adding a row, if they enter into edit mode in a cell, erase the contents, then tab or click out of it, an unhandled exception will be thrown (as most of the db columns do not allow nulls). This is prefectly normal and acceptable, but I would like to be able to catch and handle these exceptions, and I don't know where / how to catch them.

I have tried using several different event handlers on the datagridview, such as .DataError, .RowValidating, and a few others that I don't remember now... But I can't seem to catch the pesky exceptions.

Any suggestions are greatly appreciated!

EDIT: I should add that the exception is typically something like this: "ConstraintException was unhandled by user code. This property cannot be set to a null value".

sǝɯɐſ
  • 2,470
  • 4
  • 33
  • 47
  • Look at the stack trace? – Erix Dec 11 '12 at 18:25
  • @Erix is the stack trace the same as the call stack? Because the call stack doesn't really tell me much... just the line that the unhandled error shows up on (which is in the designer.cs file) – sǝɯɐſ Dec 11 '12 at 18:48
  • 1
    Yes, Sometimes it can help you find a place to catch it. Not always though. – Erix Dec 11 '12 at 19:02

1 Answers1

1

Try the CellEditEnding or CurrentCellChanged events.

Edit: Or you can also have the setter of your property handle the null value..

public int Number
{
  get { return _number; }
  set
  {  
      if (null == value)
      {
          // handle here
      }
  }
}

Edit: See Entity Framework error when submitting empty fields for more info.

Community
  • 1
  • 1
d.moncada
  • 16,900
  • 5
  • 53
  • 82
  • How do I check whether a constraint exception is thrown on cellEdit / Changed tho? I tried adding this, but still not sure how to catch the exception... – sǝɯɐſ Dec 11 '12 at 18:36
  • And to your edit: I'm not using my own properties, I'm letting the Entity Framework handle everything... I guess half my problem is I don't fully understand the magic going on behind the scenes with EF... – sǝɯɐſ Dec 11 '12 at 18:39
  • 1
    Didn't realize you were binding directly to EF.. check out this answer: http://stackoverflow.com/questions/1777815/entity-framework-error-when-submitting-empty-fields – d.moncada Dec 11 '12 at 18:47
  • Aha, just what I was looking for! Already marked your answer as correct, but can you add that link as part of your answer? Thanks! =) – sǝɯɐſ Dec 11 '12 at 18:53
  • Obviously, I should have done a bit more searching, but unfortunately I was focusing more on catching the exception than figuring out **why** the exception was happening... =P – sǝɯɐſ Dec 11 '12 at 19:02
  • 1
    @JamesEkema added the link. Looks like you will probably need to add a Model layer within your application. – d.moncada Dec 11 '12 at 19:04