ASP.NET Form View IF on Updating

Go To StackoverFlow.com

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)
2012-04-03 21:04
by user1055487
Can you post the real and clean error message? Maybe copy & paste? And it sounds like your status textbox does not exist or is null? Can you debug that - Remy 2012-04-03 21:13
Where is this TextBox, i mean in which Template(f.e. <code>EditItemTemplate</code>? FormView has three different <code>FormViewModes</code>. Apart from that i would suggest to use <code>FormViewUpdateEventArgs.NewValues</code> instea - Rango 2012-04-03 22:01
Yes. its edititemtemplat - user1055487 2012-04-03 22:12
Is this TextBox readonly? Then i would suggest to use FormViewUpdateEventArgs.OldValues. Are you sure that you've spelled ApprovalStatusTextBox correctly? Can you show the relevant part of your FormView - Rango 2012-04-03 22:16


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-04 07:14
by utsikko
good spot! thank - user1055487 2012-04-04 18:29


1

If status IsNot Nothing AndAlso (status.Text = "approved" OrElse status.Text = "denied") Then
2012-04-03 21:24
by Joel Coehoorn
it doesn't crash anymore. but it still updates regardless of the "status" am I missing something - user1055487 2012-04-03 21:33