Object reference not set to an instance of an object.

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">
LVL 1
lynntonAsked:
Who is Participating?
 
thrill_houseConnect With a Mentor Commented:
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
 
djhexCommented:
Line 199:                                Dim i1 As ListItem = DDL1.Items.FindByText(strSelected1)

change it for

Dim i1 As new ListItem = DDL1.Items.FindByText(strSelected1)
0
 
thrill_houseCommented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
lynntonAuthor Commented:
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
 
lynntonAuthor Commented:
thrill_house,

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

Thanks.
0
 
djhexCommented:
are you missing and End SUB???

paste the whole code of the functionr or subroutine
0
 
thrill_houseCommented:
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
 
lynntonAuthor Commented:
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
 
lynntonAuthor Commented:
DDL1.Items.FindByText("1").Selected=true <-----disregard this one. I was tryinng something out :-)
0
 
thrill_houseCommented:
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
 
lynntonAuthor Commented:
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
 
djhexConnect With a Mentor Commented:
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
 
thrill_houseCommented:
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
 
lynntonAuthor Commented:
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
 
thrill_houseCommented:
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
 
thrill_houseCommented:
Well if you just want to display the data, just add the column you want to see.  Or am I misunderstanding you?  
0
 
lynntonAuthor Commented:
thrill_house,

There are spaces in the data !!!

strSelected1 has spaces, can we trim or something?

Thanks.
0
 
lynntonAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.