• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 443
  • Last Modified:

Trap update error asp.net

Hi

I'm updating a row in a  sql database where the date field has a unique constraint set to not allow duplicates.

To do this I'm using a formview insert control.

My question is, how do I trap the error and show a popup message  when a duplicate entry is trying to be inserted into the database?

 i tried the page load on error
0
Ed
Asked:
Ed
  • 4
  • 3
  • 3
  • +1
1 Solution
 
-Thespian-Commented:
You simply need to rewrite method "on insert" to block the default insert command, and than put your code of insertion into block "try ... catch". so, you will get an error locally, without transfering it to the global application error level.
0
 
nmarunCommented:
If you're using C#:

try
{
    //some code here
}
catch (Exception ex)
{
    string exMessage = string.Format("Message: {0} Source: {1} StackTrace: {2}", ex.Message, ex.Source, ex.StackTrace);
    MessageBox.Show(exMessage);
}

If you're using VB.NET:

              Try
    'some code here
Catch ex As Exception
    Dim exMessage As String = String.Format("Message: {0} Source: {1} StackTrace: {2}", ex.Message, ex.Source, ex.StackTrace)
    MessageBox.Show(exMessage)
End Try
     

0
 
baghlafCommented:
You just for traping errors use Try/Catch/finally block and for showing a popupWindow to show the message to the user you can use ModalPopupExtender of microsoft AjaxToolkit, you can find it here on http://www.asp.net

hope help.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
baghlafCommented:
This url may help you for traping error for asp.net pages
http://aspnetresources.com/articles/CustomErrorPages.aspx

Regards
0
 
EdAuthor Commented:
nmarun

How do I implement this using an update template in a formview?

Try
   'some code here
Catch ex As Exception
   Dim exMessage As String = String.Format("Message: {0} Source: {1} StackTrace: {2}", ex.Message, ex.Source, ex.StackTrace)
   MessageBox.Show(exMessage)
End Try
     
0
 
baghlafCommented:
you just pass the values to be updated to a parameter of public or protected method in the code behind for example:
Protected Sub UpdateFunction(value1 as string)
Try
   'some code here
Catch ex As Exception
   Dim exMessage As String = String.Format("Message: {0} Source: {1} StackTrace: {2}", ex.Message, ex.Source, ex.StackTrace)
   MessageBox.Show(exMessage)
End Try
End Sub

in the form template you just put the UpdateFunction(<%#Eval("Field")%>

That it works with exception
Regards
0
 
nmarunCommented:
edjones1: were you able to get this to work? If not, please post the code you have and I'll have a look at it.
0
 
EdAuthor Commented:
No I wasn't able to get it to work.  I'm using a formview update template and a bit of a novice at this.

My update command.  

EventDate2 is the unique field that thows an error if populated

UpdateCommand="UPDATE [ProvBooking] SET [FirstName] = @FirstName, [Surname] = @Surname, [ContactNumber] = @ContactNumber, [EmailAddress] = @EmailAddress, [BrochureRequest] = @BrochureRequest, [PostalAddress] = @PostalAddress, [EventType] = @EventType, [EventDate] = @EventDate, [NumberOfPeople] = @NumberOfPeople, [MenuType] = @MenuType, [FingerBuffetDishes] = @FingerBuffetDishes, [MarqueeRequired] = @MarqueeRequired, [CivilCeremony] = @CivilCeremony, [EveningCatering] = @EveningCatering, [EveningCateringnNumbers] = @EveningCateringnNumbers, [AdditionalInformation] = @AdditionalInformation, [BrochureSent] = @BrochureSent, [BookingStatus] = @BookingStatus,  [DepositPaid] = @DepositPaid, [LodgeNotes] = @LodgeNotes, [ViewingMade] = @ViewingMade, [DateAttended] = @DateAttended, [ViewingNotes] = @ViewingNotes, [EventDate2] = @EventDate2 WHERE [ID] = @ID">


Thanks
0
 
nmarunCommented:
What is the error? Also, try running the command with the same data in query analyzer first to see what happens and then do it in your code.
0
 
EdAuthor Commented:

The problem is that the update code is in the .aspx page not the code view so how do I use try and catch that way when I do an update.


The error is below


Server Error in '/Admin' Application.
--------------------------------------------------------------------------------
 
Cannot insert duplicate key row in object 'dbo.ProvBooking' with unique index 'IX_ProvBooking'.
The statement has been terminated
 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
 
Exception Details: System.Data.SqlClient.SqlException: Cannot insert duplicate key row in object 'dbo.ProvBooking' with unique index 'IX_ProvBooking'.
The statement has been terminated

Open in new window

0
 
baghlafCommented:
You can't use try/catch blcok in UpdateCommand property of sql datasource in aspx view, if you want to do this, you should set updateCommand property in code behind so you set it inside try/catch block, as below for example:
try
{
SQLdataSource.UpdateCommand = ' Update Statement Here'
}
catch(ex){
throw exception
}
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now