• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 824
  • Last Modified:

Object reference not set to an instance of an object.

Hi,

Kindly assist how can we resolve this error message below.

Datagrid bound to a table, when we click update link the error popsup.

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 194:    Dim lblx as Label = e.Item.FindControl("lbldeliveryid")
Line 195:
Line 196:    label1.text=lblx.text
Line 197:    End Sub
Line 198:
Source File: c:\inetpub\wwwroot\cardviewscore.aspx    Line: 196


    Sub manage_update( s As Object, e As DataGridCommandEventArgs)
    Dim lblx as Label = e.Item.FindControl("lbldeliveryid")
    label1.text=lblx.text
    End Sub


dategrid--HTML
                    <asp:TemplateColumn Visible="False" HeaderText="deliveryid">
                        <ItemTemplate>
                            <asp:Label runat="server" name="lbldeliveryid" text='<%# DataBinder.Eval(Container, "DataItem.deliveryid") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.deliveryid") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateColumn>

0
EdwardPeter
Asked:
EdwardPeter
3 Solutions
 
raterusCommented:
I see you have an <EditItemTemplate> as well, if that row was currently in "edit" mode, it would not find that control, since it doesn't exist in the EditItemTemplate.

--Michael
0
 
EdwardPeterAuthor Commented:
Michael,

This is one of the ID to identify the unique record, we set it to Visible="False".
How can we get the ID when user click update link?

Thanks.
0
 
HavaganCommented:
And what is "label1" -- a defined Label control elsewhere in your page?

Paul
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
EdwardPeterAuthor Commented:
Havagan,

Label1 is a control place on the page using click and drag.

For displaying purposes.

Thanks.
0
 
praneethaCommented:
can u post complete update code...

i think as raterus said..in edit mode you dont have the label u r looking for...
just erase the edititemtemplate tags...

  <asp:TemplateColumn Visible="False" HeaderText="deliveryid">
                        <ItemTemplate>
                            <asp:Label runat="server" name="lbldeliveryid" text='<%# DataBinder.Eval(Container, "DataItem.deliveryid") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.deliveryid") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateColumn>
0
 
EdwardPeterAuthor Commented:
praneetha,

Change done, sad to say same error message.

We wanted to query the ID only (there are 10 columns) if we could display one everything would follow.

Thanks.

Sub manage_update( s As Object, e As DataGridCommandEventArgs)
    Dim lblx as Label = e.Item.FindControl("lbldeliveryid")
    label1.text=lblx.text
    End Sub



                    <asp:TemplateColumn Visible="False" HeaderText="deliveryid">
                        <ItemTemplate>
                            <asp:Label runat="server" name="lbldeliveryid" text='<%# DataBinder.Eval(Container, "DataItem.deliveryid") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateColumn>

0
 
praneethaCommented:
well instead of label1.Text=lblx.Text.....just do Response.write(lblx.Text)..propably u have problem with label1...not lblx...

Sub manage_update( s As Object, e As DataGridCommandEventArgs)
    Dim lblx as Label = e.Item.FindControl("lbldeliveryid")
 Response.Write(lblx.Text)
    End Sub
0
 
EdwardPeterAuthor Commented:
praneetha,

Sad to say when we click update link on the datagrid the error popup, kindly see below.

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 193:    Dim lblx as Label = e.Item.FindControl("lbldeliveryid")
Line 194:
Line 195:    Response.Write(lblx.Text)
Line 196:    End Sub
Line 197:
 
0
 
praneethaCommented:
well i dont see why it is throwing the error...

would you please complete <asp:Datagrid> code in html and also update commans.

do u have any code in itemdatabound...
0
 
RejojohnyCommented:
The manage_update function seems to be updating the values in the grid when the edit has gone into the edit mode . and the label u r accessing is not accessible during the edit mode, what u might require is have 2 labels one for the edit and another for the itemtemplate ... u can hide the label in the edittemplate if that is what u require ....

                   <asp:TemplateColumn Visible="False" HeaderText="deliveryid">
                        <ItemTemplate>
                            <asp:Label runat="server" name="lbldeliveryid" text='<%# DataBinder.Eval(Container, "DataItem.deliveryid") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label runat="server" name="lbldeliveryidEdit" text='<%# DataBinder.Eval(Container, "DataItem.deliveryid") %>'></asp:Label>
                            <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.deliveryid") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateColumn>

change the update function to search for the new label
   Sub manage_update( s As Object, e As DataGridCommandEventArgs)
    Dim lblx as Label = Ctype(e.Item.FindControl("lbldeliveryidEdit"), Label)
    Respnse.write lblx.text
    End Sub
0
 
EdwardPeterAuthor Commented:
Rejojohny,

It could be that there's something wrong with the datagrid ?

The weird thing is, now i've troubleshoot it and set Visible ="True" on a column called "ScoreID"

it also returned the same error message. kinldy assist how can we troubleshoot furthur..

Thanks.



    Sub manage_update( s As Object, e As DataGridCommandEventArgs)
    Dim lblx as textbox = Ctype(e.Item.FindControl("txtscoreid"), Textbox)
    response.write (lblx.text)
    End Sub


<asp:DataGrid id="DataGrid1"
runat="server"
DataKeyField="operatoranalysisID"
AutoGenerateColumns="False"
CellPadding="4"
onPageIndexChanged="pageGrid"
AllowPaging="True"
 PageSize="40"
OnDeleteCommand="manage_delete"
OnUpdateCommand="manage_update"
OnCancelCommand="manage_cancel_edit"
OnEditCommand="manage_edit"
OnItemDataBound="manage_ItemDataBound"
GridLines="Horizontal"
AllowSorting="True"
OnSortCommand="Sort_Grid"
Font-Names="Verdana"
Font-Size="XX-Small"
HorizontalAlign="Left">
                <HeaderStyle wrap="False" backcolor="White"></HeaderStyle>
                <Columns>
                    <asp:TemplateColumn Visible="True" HeaderText="scoreid">
                        <ItemTemplate>
                            <asp:Label runat="server" name="lblscoreid" text='<%# DataBinder.Eval(Container, "DataItem.scoreid") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox runat="server" name="txtscoreid" Text='<%# DataBinder.Eval(Container, "DataItem.scoreid") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateColumn>

0
 
EdwardPeterAuthor Commented:
I've place a 1 on the textbox in HTML Template.

Then run
    Sub manage_update( s As Object, e As DataGridCommandEventArgs)
    Dim lblx as textbox = e.Item.FindControl("txtscoreid") <---------------------------no object in the Template
    response.write (lblx.text)
    End Sub

The error showed the same..hope this helps us, I'm able to query other columns

Thanks.



                    <asp:TemplateColumn HeaderText="scoreid">
                        <ItemTemplate>
                            <asp:Label runat="server" text='<%# DataBinder.Eval(Container, "DataItem.scoreid") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox runat="server" name="txtscoreid1" Text='<%# DataBinder.Eval(Container, "DataItem.scoreid") %>'></asp:TextBox>
                        </EditItemTemplate>

0
 
EdwardPeterAuthor Commented:
This one works,

    Sub manage_update( s As Object, e As DataGridCommandEventArgs)
    Dim lblx as label = e.Item.FindControl("lbllisteningskills")
    response.write (lblx.text)
    End Sub



                    <asp:TemplateColumn SortExpression="listeningskills" HeaderText="lstngSkil">
                        <HeaderStyle forecolor="Blue"></HeaderStyle>
                        <ItemTemplate>
                            <%# Container.DataItem("listeningskills")%>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label id="lbllisteningskills" visible="false" text=<%# DataBinder.Eval(Container.DataItem, "listeningskills") %> runat="server"/>
                            <asp:DropDownList ID="ddllisteningskills" runat="server" />
                        </EditItemTemplate>
                    </asp:TemplateColumn>

0
 
EdwardPeterAuthor Commented:
Got it !!!

needed to add <%# Container.DataItem("scoreID")%>

Thanks so much for you time and patience. Please let me know if this is a good idea to proceed as a solution.

    Sub manage_update( s As Object, e As DataGridCommandEventArgs)
    Dim lblx as label = e.Item.FindControl("lblscoreid")
    response.write (lblx.text)
    End Sub



                    <asp:TemplateColumn HeaderText="ScoreID" visible="false">
                        <HeaderStyle forecolor="Blue"></HeaderStyle>
                        <ItemTemplate>
                            <%# Container.DataItem("scoreID")%>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label id="lblscoreid" visible="false" text=<%# DataBinder.Eval(Container.DataItem, "scoreid") %> runat="server"/>
                        </EditItemTemplate>
                    </asp:TemplateColumn>

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now