Object reference error - Microsoft code


D

David C

I am getting an error "Object reference not set to an instance of an
object." using code taken directly from MSDN for
GridViewUpdatedEventArgs.OldValues property. I am trying to view the old and
new values from before and after a row is updated. Below is the code I am
using in 2 subs. The error occurs at both the lines that call the
DisplayValues sub and on the setting of txtMsg.Text value in the For...Next
loop of the DisplayValues sub. Can someone help me with getting this to
work? Thanks.
p.s. Below the code for the 2 subs is the Stack Trace.

Protected Sub SubfilesGridView_RowUpdated(ByVal sender As Object, ByVal
e As GridViewUpdatedEventArgs)
' Use the Exception property to determine whether an exception
' occurred during the update operation.
If e.Exception Is Nothing Then
If e.AffectedRows = 1 Then
' Use the Keys property to get the value of the key field.
Dim keyFieldValue As String = e.Keys("ID").ToString()

' Display a confirmation message.
txtMsg.Text = "Record " & keyFieldValue & _
" updated successfully. "

' Display the new and original values.
DisplayValues(CType(e.NewValues, OrderedDictionary),
CType(e.OldValues, OrderedDictionary))
Else
' Display an error message.
txtMsg.Text = "An error occurred during the update
operation."

' When an error occurs, keep the GridView control in edit
mode.
e.KeepInEditMode = True
End If
Else
' Insert the code to handle the exception.
txtMsg.Text = e.Exception.Message

' Use the ExceptionHandled property to indicate that the
' exception is already handled.
e.ExceptionHandled = True

e.KeepInEditMode = True
End If
End Sub

Protected Sub DisplayValues(ByVal newValues As OrderedDictionary, ByVal
oldValues As OrderedDictionary)
txtMsg.Text &= "<br/></br>"

' Iterate through the new and old values. Display the values on the
page.
Dim i As Integer
For i = 0 To oldValues.Count - 1

txtMsg.Text &= "Old Value=" & oldValues(i).ToString() & _
", New Value=" & newValues(i).ToString() & "<br/>"
Next

txtMsg.Text &= "</br>"
End Sub


Stack Trace:
at ASP.frmfileedit_aspx.DisplayValues(OrderedDictionary newValues,
OrderedDictionary oldValues) in
http://server//Fileroom/frmFileEdit.aspx:line 1615
at ASP.frmfileedit_aspx.SubfilesGridView_RowUpdated(Object sender,
GridViewUpdatedEventArgs e) in http://server//Fileroom/frmFileEdit.aspx:line
1588
at
System.Web.UI.WebControls.GridView.OnRowUpdated(GridViewUpdatedEventArgs e)
at System.Web.UI.WebControls.GridView.HandleUpdateCallback(Int32
affectedRows, Exception ex)
at System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary
values, IDictionary oldValues, DataSourceViewOperationCallback callback)
at System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32
rowIndex, Boolean causesValidation)
at System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean
causesValidation, String validationGroup)
at System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source,
EventArgs e)
at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
at System.Web.UI.WebControls.GridViewRow.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)
 
Ad

Advertisements

S

Sachin Saki

In Display function instead of oldValues(i).ToString() try to
Convert.ToString(oldValues(i))

ToString() does not checks for NULL and thorws an exception while
Conver.ToString() handles
NULL.

Thanks
Sachin Saki
 
Ad

Advertisements

D

David C

That did it! Thank you Sachin.
David
Sachin Saki said:
In Display function instead of oldValues(i).ToString() try to
Convert.ToString(oldValues(i))

ToString() does not checks for NULL and thorws an exception while
Conver.ToString() handles
NULL.

Thanks
Sachin Saki
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top