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.Rai
sePostBackEvent(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)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
"David C" wrote:
> 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 a
nd
> 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...Nex
t
> 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, ByVa
l
> 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, ByVa
l
> oldValues As OrderedDictionary)
> txtMsg.Text &= "<br/></br>"
> ' Iterate through the new and old values. Display the values on th
e
> 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 [url]http://server//Fileroom/frmFileEdit.aspx:line[/ur
l]
> 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, Int
32
> 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.R
aisePostBackEvent(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)
>
>
That did it! Thank you Sachin.
David
"Sachin Saki" <SachinSaki@.discussions.microsoft.com> wrote in message
news:90AC2594-FC34-4A16-B6D2-56AEC238DF97@.microsoft.com...
> 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
> "David C" wrote:
>
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment