?
Solved

Substitute error message when deleting from datagrid

Posted on 2015-01-23
9
Medium Priority
?
74 Views
Last Modified: 2015-02-09
in my datagrid a user has the ability to delete a row.  However, some rows are tied to a relational database and the error pops up "the delete statement conflicted with the reference constraint "xxxxx"  the conflict occurred in database "xxxx"......."

This happens when a record is tied to a foreign key.  I would like a different error message to come up such as "This records contains hour entries.  The record will not be deleted"

This new error message should happen when a person tries to delete a record from the datagrid and the previous error comes up.  

How can I do that?
0
Comment
Question by:al4629740
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 49

Expert Comment

by:Martin Liss
ID: 40567301
At the top of the sub put

On error GoTo Error_Routine

' Your code

Exit Sub

Error_Routine
    If err.Number =  ??? Then ' what ever error number you are getting
        MsgBox "Your message"
        Resume Next
    End if

Open in new window

0
 
LVL 24

Expert Comment

by:mankowitz
ID: 40567310
The error is generated by the datasource, not by the datagrid (i.e. the datagrid doesn't care, it's the underlying database that is upset). The error sounds like sql server -- is that what you are using?

This should get you started:

Private Sub dataGridView1_DataError(ByVal sender As Object, _
    ByVal e As DataGridViewDataErrorEventArgs) _
    Handles dataGridView1.DataError

    ' If the data source raises an exception when a cell value is
    ' commited, display an error message.
    If e.Exception IsNot Nothing  Then
        ' At this point you should check the exception to see what kind of error it is
        MessageBox.Show("record contains hour entries")
    End If

End Sub


EDIT: oh, sorry. Are you using VB.NET or plain VB. My answer is for VB.NET
0
 

Author Comment

by:al4629740
ID: 40567323
I'm using vb6.  Correct.  It is from sql datasource
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 49

Expert Comment

by:Martin Liss
ID: 40567336
Mine is VB6
0
 

Author Comment

by:al4629740
ID: 40567354
Martin
Can I isolate it to only trigger off of the attempted grid deletions instead of all errors
0
 
LVL 49

Accepted Solution

by:
Martin Liss earned 2000 total points
ID: 40567359
mankowitz may be right but if not then something like this

Error_Routine
    If err.Number =  ??? Then ' what ever error number you are getting
        If ActiveControl.Name = ???Then ' the name of your grid
            MsgBox "Your message"
            Resume Next
        End If
    End if

Open in new window

0
 

Author Comment

by:al4629740
ID: 40571776
Martin,

What error number would this message be?

error
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 40571785
I don't know but if you put Msgbox err.number as the first line in the error routine it will tell you. That assumes that you are getting to the error routine.
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 40599587
I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question