Solved

Where to put Try CAtch code for inserting a new record

Posted on 2008-10-21
5
220 Views
Last Modified: 2010-04-21
I have an unbound Formview that is used for adding a new Work Order against an equipment item. In the ItemInserting event I have code that generates the pk (WorkOrderID) for the table that the record is being inserted to. I want to put a Try_Catch there which would allow me to redirect the user if for some reason the sql server is down and a new WorkOrderID did not get generated. If succesful then I want to provide a message box that tells the user the new WorkOrderID and allows them to either go to another web page to further document the Work Order or add another Work Order.

 Where would be the best place to put the Try Catch function to make sure a new WorkOrderID was generated? I put it where I thought it might go but not too sure.

Which event would I use to  redirect the user once a new record was inserted and a WorkOrderID generated? ItemInserted perhaps?  
To redirect the user once the record is inserted I would need the WorkOrderID generated, how do I store that in a global variable after it is generated so I can then grab it later?

Thanks for any help





I have inserted what I have so far.
Protected Sub fvAddWorkOrder_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles fvAddWorkOrder.ItemInserting
        Dim newWorkOrderID As New NewWorkOrderID 'Class created to gerenrate next incramental WorkOrderID
        Dim txtDate As TextBox = DirectCast(fvAddWorkOrder.FindControl("txtDateOpened"), TextBox)
        Dim dateOpened As Date = txtDate.Text ' date needed to pass into the class to detrmine next woID
        Dim nextWorkOrderID As String = newWorkOrderID.NewWorkOrderID(dateOpened).ToString()
        Try
            e.Values("WorkOrderID") = nextWorkOrderID
        Catch ex As Exception
            MsgBox("There was a problem generating the Work Order. Contact System Administrator at 552-7604")
            Return
        End Try
 
    End Sub

Open in new window

0
Comment
Question by:AkAlan
[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
  • 3
  • 2
5 Comments
 
LVL 11

Expert Comment

by:ladarling
ID: 22768348
Your newWorkOrderID() method is apparently generating the new number from the dateOpened object, so you should include that line in the Try block as well....
Try
Dim nextWorkOrderID As String = newWorkOrderID.NewWorkOrderID(dateOpened).ToString()
 e.Values("WorkOrderID") = nextWorkOrderID
Catch...
If that object has customer errors, you could identify it exactly by declaring ex to be a specific error type (Just as an example):
Catch ex As NewWorkOrderID_InvalidCastException
To catch my made-up exception type.
-------
On another note, VB.NET is *not* case sensitive. So, declaring a variable name identical to a class name is strongly discouraged, since you could run into all kinds of namespace qualification issues. So this line:
Dim newWorkOrderID As New NewWorkOrderID
Has the potential to give you problems down the road. Give it a number or someother unique value to keep your sanity trying to debug. For example:
 Dim newWorkOrderID1 As New NewWorkOrderID
0
 
LVL 11

Accepted Solution

by:
ladarling earned 500 total points
ID: 22768437
Which event would I use to  redirect the user once a new record was inserted and a WorkOrderID generated? ItemInserted perhaps?  
You could do the redirect right after your Try statements:
e.Values("WorkOrderID") = nextWorkOrderID
...Since if that errors out, the statements following it will be ignored and Catch will take over execution.

To redirect the user once the record is inserted I would need the WorkOrderID generated, how do I store that in a global variable after it is generated so I can then grab it later?
Declare a global variable in VB just after the Partial Class.. statement of your master page, something like:
Dim currentWOID as String
And then assign your resulting new work order ID before the redirect in your Try..Catch, somtheing like:
currentWOID = nextWorkOrderID

0
 
LVL 6

Author Comment

by:AkAlan
ID: 22768444
Thanks, both suggestions make tons of sence. Any thoughts on where to put the code for redirecting the user once the database has been updated with the new record. Item_Inserted? I don't know the ramifications of interupting the process of inserting.
0
 
LVL 6

Author Comment

by:AkAlan
ID: 22768469
Thanks, I got your second responce just as I submitted my last post. I'll give all those a try. Thanks a bunch for your help.
0
 
LVL 6

Author Closing Comment

by:AkAlan
ID: 31508335
Thanks again, I'm a newby coming from Access and this is different enough to give me a great challenge. People like you make it much easier.
0

Featured Post

Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

Question has a verified solution.

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

If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

627 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