3

I want to run an if before updating a form view; if yes then..."message" & cancel update query if no continue update query.

i've tried this but i'm getting a "obeject instance not set to null instance......" on the first line of the if? and the item updates regardless

Private Sub FormView2_ItemUpdating(sender As Object, e As System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles FormView2.ItemUpdating
    Dim status As TextBox = FormView1.FindControl("ApprovalStatusTextBox")


    If status.Text = "approved" Or "denied" Then
        e.Cancel = True
        lblupdaterequest.Text = "you cannot update this request as it has already been responded to"
    Else
        HolidayDetailsdatasource.Update()
    End If

Anyone aware of a better was of achieving something like this?

exact error:

   System.NullReferenceException was unhandled by user code
   Message=Object reference not set to an instance of an object.
   Source=WebApplication1
   StackTrace:
   at WebApplication1.HolidayApprovalDetails.DetailsView1_ItemUpdating(Object sender, DetailsViewUpdateEventArgs e) in line 32
   at System.Web.UI.WebControls.DetailsView.OnItemUpdating(DetailsViewUpdateEventArgs e)
   at System.Web.UI.WebControls.DetailsView.HandleUpdate(String commandArg, Boolean causesValidation)
   at System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)
   at System.Web.UI.WebControls.DetailsView.OnBubbleEvent(Object source, EventArgs e)
   at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
   at System.Web.UI.WebControls.DetailsViewRow.OnBubbleEvent(Object source, EventArgs e)
   at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
   at System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
   at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
4

2 回答 2

2

I can at least see that you are retrieving the value of the status TextBox from 'FormView1', while your sub ItemUpdating is referring to 'FormView2'.

After changing this, try adding a message box before the if, to make sure you got the right value you wanted:

MsgBox(status.Text)

Hopefully, it helps you solve your problem.

于 2012-04-04T07:14:33.413 回答
1
If status IsNot Nothing AndAlso (status.Text = "approved" OrElse status.Text = "denied") Then
于 2012-04-03T21:24:01.863 回答