?
Solved

Object reference not set to an instance of an object.

Posted on 2005-04-08
19
Medium Priority
?
332 Views
Last Modified: 2011-10-03
Hi,

Please kindly guide me how can we resolve this error: Object reference not set to an instance of an object.

Sub Manage_ItemDataBound  is on a datagrid that when you click the datagrid will fire this sub.

Thanks.

Server Error in '/' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 199:                                Dim i1 As ListItem = DDL1.Items.FindByText(strSelected1)
Line 200:                                If strSelected1 <> "" Then
Line 201:                                DDL1.Items.FindByText(strSelected1).Selected=true
Line 202:                                else
Line 203:                                DDL1.Items.Insert(0, "Select")
 

Source File: c:\inetpub\wwwroot\shiftview.aspx    Line: 201


Sub Manage_ItemDataBound(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
                                SetDeletionAlert(e.Item)
                                If e.Item.ItemType = ListItemType.EditItem Then
                                Dim DDL1 as DropDownList = e.Item.FindControl("DropDownList1")
                                DDL1.DataSource=GenerateSourcestarttime().defaultview
                                DDL1.DataTextField="starttime"
                                DDL1.DataValueField="shiftid"
                                DDL1.Items.Insert(0, "Select")
                                DDL1.DataBind()

                                Dim lbl1 as Label = e.Item.FindControl("lblSelected1")
                                Dim strSelected1 = CStr(lbl1.Text)
                                Dim i1 As ListItem = DDL1.Items.FindByText(strSelected1)
                                If strSelected1 <> "" Then
                                DDL1.Items.FindByText(strSelected1).Selected=true
                                else
                                DDL1.Items.Insert(0, "Select")
                                end if
                                End If
                        End Sub


            &nbsp;<asp:DataGrid id="DataGrid1" runat="server" Font-Names="Verdana" Font-Size="XX-Small" DataKeyField="operatorshiftid" AutoGenerateColumns="False" CellPadding="4" onPageIndexChanged="pageGrid" AllowPaging="True" PageSize="50" OnDeleteCommand="manage_delete" OnUpdateCommand="manage_update" OnCancelCommand="manage_cancel_edit" OnEditCommand="manage_edit" OnItemDataBound="manage_ItemDataBound" GridLines="Horizontal">
                <HeaderStyle font-bold="True"></HeaderStyle>
                <Columns>
                    <asp:BoundColumn DataField="operatorid" ReadOnly="True" HeaderText="Operator ID"></asp:BoundColumn>
                    <asp:BoundColumn DataField="operatorname" ReadOnly="True" HeaderText="Operator Name"></asp:BoundColumn>
                    <asp:BoundColumn DataField="shiftdate" ReadOnly="True" HeaderText="Shift Date"></asp:BoundColumn>
                    <asp:TemplateColumn HeaderText="Start Time">
                        <ItemTemplate>
                            <%# Container.DataItem("starttime")%>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label id="lblSelected1" visible="false" text=<%# DataBinder.Eval(Container.DataItem, "starttime") %> runat="server"/>
                            <asp:DropDownList ID="DropDownList1" runat="server" />
                        </EditItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="First Break">
0
Comment
Question by:lynnton
  • 9
  • 7
  • 3
19 Comments
 
LVL 10

Expert Comment

by:djhex
ID: 13735960
Line 199:                                Dim i1 As ListItem = DDL1.Items.FindByText(strSelected1)

change it for

Dim i1 As new ListItem = DDL1.Items.FindByText(strSelected1)
0
 
LVL 8

Expert Comment

by:thrill_house
ID: 13735991
It appears the problem is that DDL1 does not contain strSelected1.  Is this possible?  If this is possible you need to check to make sure that DDL1 contains strSelected1 before you set the selected property equal to true.
0
 
LVL 1

Author Comment

by:lynnton
ID: 13736012
djhex,

sad to say this error popup.

Thanks.


Server Error in '/' Application.
--------------------------------------------------------------------------------

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30205: End of statement expected.
Source Error:
Line 212:                                Dim lbl1 as Label = e.Item.FindControl("lblSelected1")
Line 213:                                Dim strSelected1 = CStr(lbl1.Text)
Line 214:                                Dim i1 As new ListItem = DDL1.Items.FindByText(strSelected1)
Line 215:
Line 216:                                If strSelected1 <> "" Then
Source File: c:\inetpub\wwwroot\shiftview.aspx    Line: 214
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:lynnton
ID: 13736037
thrill_house,

Can you kindly provide a algorithm we can run to check if the finding is true?

Thanks.
0
 
LVL 10

Expert Comment

by:djhex
ID: 13736053
are you missing and End SUB???

paste the whole code of the functionr or subroutine
0
 
LVL 8

Expert Comment

by:thrill_house
ID: 13736064
Change this:

If strSelected1 <> "" Then
                                DDL1.Items.FindByText(strSelected1).Selected=true
                                else
                                DDL1.Items.Insert(0, "Select")
                                end if

TO


  If strSelected1 <> "" and DDL1.Items.Contains(strSelected1) Then
           DDL1.Items.FindByText(strSelected1).Selected=true
  else
           DDL1.Items.Insert(0, "Select")
  end if
0
 
LVL 1

Author Comment

by:lynnton
ID: 13736086
djhex,

Please see below.

Thanks.

                     'on click update bound datagrid
                        Sub Manage_ItemDataBound(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
                                SetDeletionAlert(e.Item)
                                If e.Item.ItemType = ListItemType.EditItem Then
                                'BindToList(e.Item)
                                Dim DDL1 as DropDownList = e.Item.FindControl("DropDownList1")
                                DDL1.DataSource=GenerateSourcestarttime().defaultview
                                DDL1.DataTextField="starttime"
                                DDL1.DataValueField="shiftid"
                                'DDL1.Items.Insert(0, "Select")
                                DDL1.DataBind()
                                Dim lbl1 as Label = e.Item.FindControl("lblSelected1")
                                Dim strSelected1 = CStr(lbl1.Text)
                                Dim i1 As new ListItem = DDL1.Items.FindByText(strSelected1)
                                If strSelected1 <> "" Then
                                    DDL1.Items.FindByText("1").Selected=true
                                else
                                    DDL1.Items.Insert(0, "Select")
                                end if
                                end if
                                End Sub

0
 
LVL 1

Author Comment

by:lynnton
ID: 13736092
DDL1.Items.FindByText("1").Selected=true <-----disregard this one. I was tryinng something out :-)
0
 
LVL 8

Expert Comment

by:thrill_house
ID: 13736106
lynnton,
I really do not think it matters if you have that new in there or not.  I would actually take it out, since you are setting the listitem to another listitem. Also, the error did not come from this since the line of code that you said the error came from did not contain i1, so I think you should not worry about that line.
0
 
LVL 1

Author Comment

by:lynnton
ID: 13736129
thrill_house,

I think you maybe right, if we could only verify that the data isn't in the list..maybe we could post it some where?

Thanks.


Server Error in '/' Application.
--------------------------------------------------------------------------------

Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Specified cast is not valid.
Source Error:
Line 214:                                Dim i1 As ListItem = DDL1.Items.FindByText(strSelected1)
Line 215:
Line 216:                                If strSelected1 <> "" and DDL1.Items.Contains(strSelected1) Then
Line 217:                                DDL1.Items.FindByText(strSelected1).Selected=true
Line 218:                                else
Source File: c:\inetpub\wwwroot\shiftview.aspx    Line: 216

0
 
LVL 10

Assisted Solution

by:djhex
djhex earned 400 total points
ID: 13736143
Try to find the controls by celll


    Dim DDL1 as DropDownList = e.Item.cells(0).FindControl("DropDownList1")

'Supposing its on the first column

If it doesnt work in this way
use NEW
Dim DDL1 as new DropDownList = e.Item.cells(0).FindControl("DropDownList1")


make a breakpoint on the SUB and make an step by step and tell me which line gereates the End of statement error.

I see the ENDS statements correct in this sub. The error must be in anothe part.


                         
0
 
LVL 8

Expert Comment

by:thrill_house
ID: 13736184
Oh my fault, I didn't realize that strSelected1 was a string and not a list item.  

 If strSelected1 <> "" and DDL1.Items.Contains(ddl1.Items.FindByText(strSelected1)) Then
                           DDL1.Items.FindByText(strSelected1).Selected=true
 else


Try that...
0
 
LVL 1

Author Comment

by:lynnton
ID: 13736254
thrill_house,

Done, seems like it always goes to "Select"

can we list down on a label what's the value or the data in the datagrid and then compare?

This procedure work for text values, sadly this error occured when using time value "00:00:00" up to "23:30:00"

Thanks.
0
 
LVL 8

Expert Comment

by:thrill_house
ID: 13736257
That might throw an error, I can't test this right now so I'm just throwing out possible solutions, if that doesn't work try this...

 If strSelected1 <> "" and not ddl1.Items.FindByText(strSelected1) is nothing Then
                           DDL1.Items.FindByText(strSelected1).Selected=true
 else
0
 
LVL 8

Expert Comment

by:thrill_house
ID: 13736318
Well if you just want to display the data, just add the column you want to see.  Or am I misunderstanding you?  
0
 
LVL 1

Author Comment

by:lynnton
ID: 13736327
thrill_house,

There are spaces in the data !!!

strSelected1 has spaces, can we trim or something?

Thanks.
0
 
LVL 8

Accepted Solution

by:
thrill_house earned 1600 total points
ID: 13736349
Yep, just call Trim(strSelected1) to trim spaces to the left and right of the string.  If the spaces are in the middle, then just do
strSelected = strSelected.Replace(" ", "")
0
 
LVL 1

Author Comment

by:lynnton
ID: 13736567
thrill_house,

Can you kindly look at this and give you thoughts.

Thanks.


http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21381390.html
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question