Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2008

Tip: Looking for answers? Try searching our database.

Object reference error - Microsoft code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David C - 15 Feb 2008 15:26 GMT
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)
Sachin Saki - 15 Feb 2008 19:44 GMT
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

> I am getting an error "Object reference not set to an instance of an
> object." using code taken directly from MSDN for
[quoted text clipped - 93 lines]
>    at System.Web.UI.Page.ProcessRequestMain(Boolean
> includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
David C - 18 Feb 2008 14:25 GMT
That did it!  Thank you Sachin.
David
> In Display function instead of oldValues(i).ToString() try to
> Convert.ToString(oldValues(i))
[quoted text clipped - 115 lines]
>>    at System.Web.UI.Page.ProcessRequestMain(Boolean
>> includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.