We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How to catch Insert Error and Display on screen ASP.Net(VB)/

Medium Priority
619 Views
Last Modified: 2013-11-27
Hello

I've got a formview that inserts data into the database. One of these fields is called ' Booked date' this is set to unique in the SQL  database although not the primary key.

 If i try to insert a duplicate date value into the database i get a .net error message on screen saying it can't insert because its not unique which is what I want.

My question is - how can I catch this error and display a popup box for the user to tell them that the date is already booked?

thanks
Comment
Watch Question

just use the TRY CATCH block...

Try
....
....
SQL QUERY
...
Catch ex as exception
msgbox(ex.message)

Finally
close connection

Else you will have to run a query to check before the insert and hand code a error message:

query = select * from table where Booked date = '"& from text box or something"'
cmd = new sqldataadapter(query,conn)
cmd.fill(DS)
If DS.rows.count - 1 >=1
msgbox("Record exists")



correct code

query = select * from table where Booked date = '"& from text box or something"'
cmd = new sqldataadapter(query,conn)
cmd.fill(DS)
If DS.tables(0).rows.count - 1 >=1
msgbox("Record exists")
CERTIFIED EXPERT

Commented:
Try like this

   TRy

     ' your insert code will be here

Catch (ex as exception)
     ' Here you can put the message in Lable to diplay on screen
   LBLMessage.Text = "that the date is already booked"
End Try


You can use this code to show the error using a javascript alert box. You can use ClientScript.RegisterStartupScript in .NET 2.0 or Page.RegisterStartupScript in .NET 1.1
Try
  ''Your inser code
Catch ex As Exception
  ClientScript.RegisterStartupScript(GetType(String), "ErrorMsg", "<script language='javascript'>alert('The date you selected is already booked');</script>")
  'Page.RegisterStartupScript("ErrorMsg", "<script language='javascript'>alert('The date you selected is already booked');</script>")
End Try

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Ed

Author

Commented:

Thanks.

I'll give you the points for this.

One question though. I'm using the formview insert tempate.... how can i apply the code to this as the insert statement is not in the code?  
You will be having an event that fires when the insert is completed. You can add this code there.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.