Howto handle InvalidConstrainException with DataGrid

  • Thread starter Thread starter =?iso-8859-1?Q?Maur=EDcio?=
  • Start date Start date
?

=?iso-8859-1?Q?Maur=EDcio?=

On my form there are 2 DataGrids bind with a DataSet. One
is the parent and the other is the child.
When I delete a parent row it's thrown a
InvalidCostrainException exception.
How can I handle this exception? I don't know how to
define the method that will handle it.
 
Hi Mauricio,
If the parent row is related to one or more child rows, deleting this
parent row will
violates the ForeignKeyConstraint, you need delete all the child rows
before
deleting the parent row.
Does this solve your problem?
If you still have problem on this issue, please be free to post on this
group,
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!

--------------------
| Content-Class: urn:content-classes:message
| From: =?iso-8859-1?Q?Maur=EDcio?= <[email protected]>
| Sender: =?iso-8859-1?Q?Maur=EDcio?= <[email protected]>
| Subject: Howto handle InvalidConstrainException with DataGrid
| Date: Tue, 28 Oct 2003 11:57:31 -0800
| Lines: 10
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcOdjbku+AUz5DnhSCuqmKT3EkYQPg==
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:55388
| NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| On my form there are 2 DataGrids bind with a DataSet. One
| is the parent and the other is the child.
| When I delete a parent row it's thrown a
| InvalidCostrainException exception.
| How can I handle this exception? I don't know how to
| define the method that will handle it.
|
|
|
|
|
 
Hi Ying-Shen Yu,
I know that I need delete all child rows, but how can I
delete them before datagrid delete the parent row?
Is There an event thrown by the datagrid before delete the
parent row?
Thanks,
Best regards,
Mauricio
-----Mensagem original-----
Hi Mauricio,
If the parent row is related to one or more child rows, deleting this
parent row will
violates the ForeignKeyConstraint, you need delete all the child rows
before
deleting the parent row.
Does this solve your problem?
If you still have problem on this issue, please be free to post on this
group,
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!
 
Hi Mauricio,
Thanks for your reply,
You may try this way:
1.In your dataset schema, set the delete rule of your parent-child relation
to cascade.
then you can delete the parent without problem in the dataset, the
corresponding child rows will be removed automatically.
2.To persist changes to your database, you may use the Update method on
each dataAdapters.
You should first update your child table, then update your parent table. Or
you will break the foreignkey constraint in DB.

Does this solve your problem?
If you still have problem on it, please be free to post it this group.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!

--------------------
| Content-Class: urn:content-classes:message
| From: "Mauricio" <[email protected]>
| Sender: "Mauricio" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: RE: Howto handle InvalidConstrainException with DataGrid
| Date: Wed, 29 Oct 2003 02:06:23 -0800
| Lines: 78
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcOeBE79TAZijM3yRWqbye+JxF+P7Q==
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:55422
| NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| Hi Ying-Shen Yu,
| I know that I need delete all child rows, but how can I
| delete them before datagrid delete the parent row?
| Is There an event thrown by the datagrid before delete the
| parent row?
| Thanks,
| Best regards,
| Mauricio
|
| >-----Mensagem original-----
| >Hi Mauricio,
| >If the parent row is related to one or more child rows,
| deleting this
| >parent row will
| >violates the ForeignKeyConstraint, you need delete all
| the child rows
| >before
| >deleting the parent row.
| >Does this solve your problem?
| >If you still have problem on this issue, please be free
| to post on this
| >group,
| >Thanks!
| >
| >Best regards,
| >
| >Ying-Shen Yu [MSFT]
| >Microsoft Online Partner Support
| >Get Secure! - www.microsoft.com/security
| >
| >This posting is provided "AS IS" with no warranties and
| confers no rights.
| >You should not reply this mail directly, "Online" should
| be removed before
| >sending, Thanks!
| >
| >--------------------
| >| Content-Class: urn:content-classes:message
| >| From: =?iso-8859-1?Q?Maur=EDcio?=
| <[email protected]>
| >| Sender: =?iso-8859-1?Q?Maur=EDcio?=
| <[email protected]>
| >| Subject: Howto handle InvalidConstrainException with
| DataGrid
| >| Date: Tue, 28 Oct 2003 11:57:31 -0800
| >| Lines: 10
| >| Message-ID: <[email protected]>
| >| MIME-Version: 1.0
| >| Content-Type: text/plain;
| >| charset="iso-8859-1"
| >| Content-Transfer-Encoding: 7bit
| >| X-Newsreader: Microsoft CDO for Windows 2000
| >| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| >| Thread-Index: AcOdjbku+AUz5DnhSCuqmKT3EkYQPg==
| >| Newsgroups:
| microsoft.public.dotnet.framework.windowsforms
| >| Path: cpmsftngxa06.phx.gbl
| >| Xref: cpmsftngxa06.phx.gbl
| >microsoft.public.dotnet.framework.windowsforms:55388
| >| NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
| >| X-Tomcat-NG:
| microsoft.public.dotnet.framework.windowsforms
| >|
| >| On my form there are 2 DataGrids bind with a DataSet.
| One
| >| is the parent and the other is the child.
| >| When I delete a parent row it's thrown a
| >| InvalidCostrainException exception.
| >| How can I handle this exception? I don't know how to
| >| define the method that will handle it.
| >|
| >|
| >|
| >|
| >|
| >
| >.
| >
|
 
Hi, Ying-Shen Yu [MSFT]
I can't use delete rule of my parent-child relation,
because I need test other things before to decide delete
child and parent rows.
Is threre an event before datagrid's delete? Can I cancel
delete?

Best regards,

Mauricio
-----Mensagem original-----
Hi Mauricio,
Thanks for your reply,
You may try this way:
1.In your dataset schema, set the delete rule of your parent-child relation
to cascade.
then you can delete the parent without problem in the dataset, the
corresponding child rows will be removed automatically.
2.To persist changes to your database, you may use the Update method on
each dataAdapters.
You should first update your child table, then update your parent table. Or
you will break the foreignkey constraint in DB.

Does this solve your problem?
If you still have problem on it, please be free to post it this group.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!

--------------------
| Content-Class: urn:content-classes:message
| From: "Mauricio" <[email protected]>
| Sender: "Mauricio" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: RE: Howto handle InvalidConstrainException with DataGrid
| Date: Wed, 29 Oct 2003 02:06:23 -0800
| Lines: 78
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcOeBE79TAZijM3yRWqbye+JxF+P7Q==
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:55422
| NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| Hi Ying-Shen Yu,
| I know that I need delete all child rows, but how can I
| delete them before datagrid delete the parent row?
| Is There an event thrown by the datagrid before delete the
| parent row?
| Thanks,
| Best regards,
| Mauricio
|
| >-----Mensagem original-----
| >Hi Mauricio,
| >If the parent row is related to one or more child rows,
| deleting this
| >parent row will
| >violates the ForeignKeyConstraint, you need delete all
| the child rows
| >before
| >deleting the parent row.
| >Does this solve your problem?
| >If you still have problem on this issue, please be free
| to post on this
| >group,
| >Thanks!
| >
| >Best regards,
| >
| >Ying-Shen Yu [MSFT]
| >Microsoft Online Partner Support
| >Get Secure! - www.microsoft.com/security
| >
| >This posting is provided "AS IS" with no warranties and
| confers no rights.
| >You should not reply this mail directly, "Online" should
| be removed before
| >sending, Thanks!
| >
| >--------------------
| >| Content-Class: urn:content-classes:message
| >| From: =?iso-8859-1?Q?Maur=EDcio?=
| <[email protected]>
| >| Sender: =?iso-8859-1?Q?Maur=EDcio?=
| <[email protected]>
| >| Subject: Howto handle InvalidConstrainException with
| DataGrid
| >| Date: Tue, 28 Oct 2003 11:57:31 -0800
| >| Lines: 10
| >| Message-ID: <[email protected]>
| >| MIME-Version: 1.0
| >| Content-Type: text/plain;
| >| charset="iso-8859-1"
| >| Content-Transfer-Encoding: 7bit
| >| X-Newsreader: Microsoft CDO for Windows 2000
| >| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| >| Thread-Index: AcOdjbku+AUz5DnhSCuqmKT3EkYQPg==
| >| Newsgroups:
| microsoft.public.dotnet.framework.windowsforms
| >| Path: cpmsftngxa06.phx.gbl
| >| Xref: cpmsftngxa06.phx.gbl
| >microsoft.public.dotnet.framework.windowsforms:55388
| >| NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
| >| X-Tomcat-NG:
| microsoft.public.dotnet.framework.windowsforms
| >|
| >| On my form there are 2 DataGrids bind with a DataSet.
| One
| >| is the parent and the other is the child.
| >| When I delete a parent row it's thrown a
| >| InvalidCostrainException exception.
| >| How can I handle this exception? I don't know how to
| >| define the method that will handle it.
| >|
| >|
| >|
| >|
| >|
| >
| >.
| >
|

.
 
Hi Mauricio,
After some research, I found that ADO.NET didn't provide the exact event to
do the job, DataTable does have the OnRowDeleting event but I didn't found
a way to cancel the delete.
So I went back to find a work around from the datagrid class.
You may try deriving the DataGrid class, and overriding the method
ProcessDialogKey, you may catch the "Del" key in this event, and do some
check and delete the child rows before the datagrid handling it.
Does it solve your problem?
If you still have problem on it, please be free to reply to the group!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!
 
Full Explanation - How trap exception coused by deleting row in
Constraint
This problem is often see with deleting by datagrid, and imposible trap
System.Data.InvalidConstraintException.

Here is problem solution satisfies in complete.
Code is in C#, but if you need just translate to vb.net, and it will
work too.

We catch this exception at application level, and if we detect that is
"System.Data.InvalidConstraintException" exception, just do nothing, or
just inform user that he can not delete this row.

Once you add this method, you can use it for different exception traps!

Best Regards
Milan Better

1.Step is to add event handler Application.ThreadException, in main
function of our project


[STAThread]
static void Main()
{
Application.ThreadException +=new
System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
Application.Run(new MainForm());
}



private static void Application_ThreadException(object sender,
System.Threading.ThreadExceptionEventArgs e)
{
//Fill here
}

2.Step is fill event handler we just created!

private static void Application_ThreadException(object sender,
System.Threading.ThreadExceptionEventArgs e)
{
if(e.Exception is System.Data.InvalidConstraintException)
{
MessageBox.Show(e.Exception.Message,"Error - can not break business
rules",MessageBoxButtons.OK,MessageBoxIcon.Error);
return; //This code silent pass exception
}
else throw e.Exception; //all other exceptions which we do not handle
}
 
Back
Top