Showing posts with label anobject. Show all posts
Showing posts with label anobject. Show all posts

Thursday, March 29, 2012

Object reference error - Microsoft code

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:
>

Thursday, March 22, 2012

Object reference not set to an instance of an object On Code Page Register Tag?

why am i getting the "Object reference not set to an instance of an
object" error on all page registeration tags in my project.
<%@dotnet.itags.org. Page Language="vb" AutoEventWireup="false"
Codebehind="bid_history.aspx.vb"
Inherits="dnfExchange.marketing_bid_history" %looks like you have renamed your file at some point,

change inherits="dnfExchange.marketing_bid_history"

to

inherits="dnfExchange.bid_history"
Okay, let's put on our diagnostic hats here. "Object Reference not set to an
instance of an object" is the message, so let's parse it: "Object Reference"
is a reference to an object. So, how many objects are referenced in your
page registration tag? I will give you a hint: One. "Not set to an instance
of an object" means that the object doesn't exist, or can't be found where
you said it was. So, obviously, the object doesn't exist, or is not where
you say it is. What are the possibilities? You spelled the class name wrong,
the DLL is not in the bin folder, or you don't have a "Src" reference in
your code and you're not using a compiled DLL.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.

<jw56578@.gmail.com> wrote in message
news:1116257283.912003.295390@.g47g2000cwa.googlegr oups.com...
> why am i getting the "Object reference not set to an instance of an
> object" error on all page registeration tags in my project.
> <%@. Page Language="vb" AutoEventWireup="false"
> Codebehind="bid_history.aspx.vb"
> Inherits="dnfExchange.marketing_bid_history" %

Object reference not set to an instance of an object On Code Page Register Tag?

why am i getting the "Object reference not set to an instance of an
object" error on all page registeration tags in my project.
<%@dotnet.itags.org. Page Language="vb" AutoEventWireup="false"
Codebehind="bid_history.aspx.vb"
Inherits="dnfExchange.marketing_bid_history" %>looks like you have renamed your file at some point,
change inherits="dnfExchange.marketing_bid_history"
to
inherits="dnfExchange.bid_history"
Okay, let's put on our diagnostic hats here. "Object Reference not set to an
instance of an object" is the message, so let's parse it: "Object Reference"
is a reference to an object. So, how many objects are referenced in your
page registration tag? I will give you a hint: One. "Not set to an instance
of an object" means that the object doesn't exist, or can't be found where
you said it was. So, obviously, the object doesn't exist, or is not where
you say it is. What are the possibilities? You spelled the class name wrong,
the DLL is not in the bin folder, or you don't have a "Src" reference in
your code and you're not using a compiled DLL.
HTH,
Kevin Spencer
Microsoft MVP
.Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
<jw56578@.gmail.com> wrote in message
news:1116257283.912003.295390@.g47g2000cwa.googlegroups.com...
> why am i getting the "Object reference not set to an instance of an
> object" error on all page registeration tags in my project.
> <%@. Page Language="vb" AutoEventWireup="false"
> Codebehind="bid_history.aspx.vb"
> Inherits="dnfExchange.marketing_bid_history" %>
>

Object reference not set to an instance of an object.

I m getting this error "Object reference not set to an instance of an
object." when i run my application.

i m trying to select a value from dropdownlist

here is the code.

Protected Sub GetMsg(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles dgJobQueue.PreRender
ASPNET_MsgBox("welcome to msgbox")
dgJobQueue.FindControl("cboUserID")

Dim cboUserIdTemp As DropDownList =
CType(dgJobQueue.FindControl("cboUserID"), DropDownList)
Dim s As String = GetUserName()
Dim listItem As ListItem
cboUserIdTemp.SelectedValue = s
End SubAnd the line that throws the exception is?....

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.

<hina.pandya@.gmail.com> wrote in message
news:1117043143.865298.52560@.g49g2000cwa.googlegro ups.com...
>I m getting this error "Object reference not set to an instance of an
> object." when i run my application.
> i m trying to select a value from dropdownlist
> here is the code.
> Protected Sub GetMsg(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles dgJobQueue.PreRender
> ASPNET_MsgBox("welcome to msgbox")
> dgJobQueue.FindControl("cboUserID")
> Dim cboUserIdTemp As DropDownList =
> CType(dgJobQueue.FindControl("cboUserID"), DropDownList)
> Dim s As String = GetUserName()
> Dim listItem As ListItem
> cboUserIdTemp.SelectedValue = s
> End Sub
cboUserIdTemp.SelectedValue = s
cboUserIdTemp.SelectedValue = s
cboUserIdTemp.SelectedValue = s
cboUserIdTemp.SelectedValue = s
Hi Hina

can you try this

if (cboUserIdTemp.Items.FindByText(s) != null) {
cboUserIdTemp.SelectedValue = s
}

This is C# code you can easily convert it to VB.NET

thanks

hina.pandya@.gmail.com wrote:
> cboUserIdTemp.SelectedValue = s
Nope, It not working .

I m trying to set selected value to dropdownlist which is inside
datagrid. and dropdownlist gets populated by other method.

Thanks
It looks like you didn't find the Control you were looking for. Are you sure
it's a child Control of the Control you're looking for it in? Are you sure
you spelled the ID correctly?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.

<hina.pandya@.gmail.com> wrote in message
news:1117044323.952273.117990@.g44g2000cwa.googlegr oups.com...
> cboUserIdTemp.SelectedValue = s
This is the asp code that i have . ..now i m trying to set the selected
value as logged in user name .. i have user logged in id which returns
string. s now .. when ever i try to set cboUserIdTemp.SelectedValue =
s it gives me an error. ..

and if i try to do this

cboUserIdTemp.SelectedIndex =
cboUserIdTemp.Items.IndexOf(cboUserIdTemp.Items.Fi ndByValue(s)) it
gives me following error
Object reference not set to an instance of an object.
===============================================
Protected Sub GetMsg(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles dgJobQueue.PreRender
ASPNET_MsgBox("welcome to msgbox")
dgJobQueue.FindControl("cboUserID")

Dim cboUserIdTemp As DropDownList =
CType(dgJobQueue.FindControl("cboUserID"), DropDownList)
Dim s As String = GetUserName()
'Dim listItem As ListItem

'cboUserIdTemp.SelectedValue = s --> Error :Method not
found: Void
System.Web.UI.WebControls.ListControl.set_Selected Value(System.String).

cboUserIdTemp.SelectedIndex =
cboUserIdTemp.Items.IndexOf(cboUserIdTemp.Items.Fi ndByValue(s)) -->
Error: Object reference not set to an instance of an object.
'cboUserIdTemp.SelectedIndex = 1
End Sub

====================================

Protected Function PopulateProjectDropDownList()

Dim myCommand As SqlCommand = New SqlCommand("GetProjectList",
ripConn)
myCommand.CommandType = CommandType.StoredProcedure

ripConn.Open()
Return myCommand.ExecuteReader(CommandBehavior.CloseConne ction)

End Function

====================

<asp:TemplateColumn HeaderText="User Name">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" Width="15%"></ItemStyle>
<ItemTemplate>
<asp:Label id="Label2" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=cboUserID OnPreRender = '<%# GetMsg() %>'
tabIndex=1 runat="server" AutoPostBack="True" DataValueField="UserName"
DataTextField="UserName" DataSource="<%# PopulateUserNameDropDownList()
%>">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn

Friday, March 16, 2012

Object reference not set to an instance of an object.

I m getting this error "Object reference not set to an instance of an
object." when i run my application.
i m trying to select a value from dropdownlist
here is the code.
Protected Sub GetMsg(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles dgJobQueue.PreRender
ASPNET_MsgBox("welcome to msgbox")
dgJobQueue.FindControl("cboUserID")
Dim cboUserIdTemp As DropDownList =
CType(dgJobQueue.FindControl("cboUserID"), DropDownList)
Dim s As String = GetUserName()
Dim listItem As ListItem
cboUserIdTemp.SelectedValue = s
End SubAnd the line that throws the exception is?....
HTH,
Kevin Spencer
Microsoft MVP
.Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
<hina.pandya@.gmail.com> wrote in message
news:1117043143.865298.52560@.g49g2000cwa.googlegroups.com...
>I m getting this error "Object reference not set to an instance of an
> object." when i run my application.
> i m trying to select a value from dropdownlist
> here is the code.
> Protected Sub GetMsg(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles dgJobQueue.PreRender
> ASPNET_MsgBox("welcome to msgbox")
> dgJobQueue.FindControl("cboUserID")
> Dim cboUserIdTemp As DropDownList =
> CType(dgJobQueue.FindControl("cboUserID"), DropDownList)
> Dim s As String = GetUserName()
> Dim listItem As ListItem
> cboUserIdTemp.SelectedValue = s
> End Sub
>
cboUserIdTemp.SelectedValue = s
cboUserIdTemp.SelectedValue = s
cboUserIdTemp.SelectedValue = s
cboUserIdTemp.SelectedValue = s
Hi Hina
can you try this
if (cboUserIdTemp.Items.FindByText(s) != null) {
cboUserIdTemp.SelectedValue = s
}
This is C# code you can easily convert it to VB.NET
thanks
hina.pandya@.gmail.com wrote:
> cboUserIdTemp.SelectedValue = s
Nope, It not working .
I m trying to set selected value to dropdownlist which is inside
datagrid. and dropdownlist gets populated by other method.
Thanks
It looks like you didn't find the Control you were looking for. Are you sure
it's a child Control of the Control you're looking for it in? Are you sure
you spelled the ID correctly?
HTH,
Kevin Spencer
Microsoft MVP
.Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
<hina.pandya@.gmail.com> wrote in message
news:1117044323.952273.117990@.g44g2000cwa.googlegroups.com...
> cboUserIdTemp.SelectedValue = s
>
This is the asp code that i have . ..now i m trying to set the selected
value as logged in user name .. i have user logged in id which returns
string. s now .. when ever i try to set cboUserIdTemp.SelectedValue =
s it gives me an error. ..
and if i try to do this
cboUserIdTemp.SelectedIndex =
cboUserIdTemp.Items.IndexOf(cboUserIdTemp.Items.FindByValue(s)) it
gives me following error
Object reference not set to an instance of an object.
========================================
=======
Protected Sub GetMsg(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles dgJobQueue.PreRender
ASPNET_MsgBox("welcome to msgbox")
dgJobQueue.FindControl("cboUserID")
Dim cboUserIdTemp As DropDownList =
CType(dgJobQueue.FindControl("cboUserID"), DropDownList)
Dim s As String = GetUserName()
'Dim listItem As ListItem
'cboUserIdTemp.SelectedValue = s --> Error :Method not
found: Void
System.Web.UI.WebControls.ListControl.set_SelectedValue(System.String).
cboUserIdTemp.SelectedIndex =
cboUserIdTemp.Items.IndexOf(cboUserIdTemp.Items.FindByValue(s)) -->
Error: Object reference not set to an instance of an object.
'cboUserIdTemp.SelectedIndex = 1
End Sub
====================================
Protected Function PopulateProjectDropDownList()
Dim myCommand As SqlCommand = New SqlCommand("GetProjectList",
ripConn)
myCommand.CommandType = CommandType.StoredProcedure
ripConn.Open()
Return myCommand.ExecuteReader(CommandBehavior.CloseConnection)
End Function
====================
<asp:TemplateColumn HeaderText="User Name">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" Width="15%"></ItemStyle>
<ItemTemplate>
<asp:Label id="Label2" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=cboUserID OnPreRender = '<%# GetMsg() %>'
tabIndex=1 runat="server" AutoPostBack="True" DataValueField="UserName"
DataTextField="UserName" DataSource="<%# PopulateUserNameDropDownList()
%>">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>