Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / December 2004

Tip: Looking for answers? Try searching our database.

Howto handle InvalidConstrainException with DataGrid

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Maurício - 28 Oct 2003 20:57 GMT
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.
Ying-Shen Yu[MSFT] - 29 Oct 2003 02:28 GMT
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?= <mauricio.pires@controlbase.com.br>
[quoted text clipped - 13 lines]
| 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
[quoted text clipped - 5 lines]
| How can I handle this exception? I don't know how to
| define the method that will handle it.
Mauricio - 29 Oct 2003 11:06 GMT
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,
[quoted text clipped - 48 lines]
>
>.
Ying-Shen Yu[MSFT] - 30 Oct 2003 06:29 GMT
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" <mauricio.pires@controlbase.com.br>
| Sender: "Mauricio" <mauricio.pires@controlbase.com.br>
| References: <0d1401c39d8d$b92e8890$a001280a@phx.gbl>
<8XdAPwbnDHA.2624@cpmsftngxa06.phx.gbl>
| Subject: RE: Howto handle InvalidConstrainException with DataGrid
| Date: Wed, 29 Oct 2003 02:06:23 -0800
[quoted text clipped - 10 lines]
| 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
[quoted text clipped - 71 lines]
| >
| >.
Mauricio - 03 Nov 2003 10:19 GMT
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,
[quoted text clipped - 121 lines]
>
>.
"Ying-Shen Yu[MSFT]" - 04 Nov 2003 02:23 GMT
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!
better@medianis.net - 27 Dec 2004 22:43 GMT
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
}

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.