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

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

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
0
Ed
Asked:
Ed
1 Solution
 
isaackhaziCommented:
just use the TRY CATCH block...

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

Finally
close connection

0
 
isaackhaziCommented:
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")



0
 
isaackhaziCommented:
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")
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Pratima PharandeCommented:
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


0
 
sunithnairCommented:
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

0
 
EdAuthor 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?  
0
 
sunithnairCommented:
You will be having an event that fires when the insert is completed. You can add this code there.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now