Saturday, March 24, 2012

Object reference not set to an instance of an object

Embarrassed [:$]Hi,

The next error appear in my page:

Object reference not set to an instance of an object.

Line 37: labelTipo.Text = "Producto Terminado"

My partial aspx code page is:

<asp:FormView id="FormviewProductos"
DataSourceID="ProductosSqlDataSource" runat="server"
DataKeyNames="IDProducto"
OnDataBound="FormViewProductos_DataBound"
AllowPaging="True"
BorderWidth="1px"
BorderColor="#CC9966"
BorderStyle="None"
backcolor="#8fa4cc"
CellPadding="4">
<RowStyle ForeColor="#330099" backcolor="#c2d7ff" ></RowStyle>
<PagerStyle ForeColor="#330099" HorizontalAlign="Center" BackColor="#FFFFCC"></PagerStyle>
<HeaderStyle ForeColor="#FFFFCC" Font-Bold="True" BackColor="#990000"></HeaderStyle>
<EditRowStyle ForeColor="#663399" BackColor="#e5efff" Font-Bold="True"></EditRowStyle>
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>

<ItemTemplate>
<div class="section table">
<table border="0" cellspacing="4" cellpadding="0">
<tr>
<td><b>Código</b></td>
<td><%#Eval("IDProducto")%></td>
</tr>
<tr>
<td><b>Descripción</b></td>
<td><%#Eval("Pro_Descripcion")%></td>
</tr>
<tr>
<td><b>Grupo</b></td>
<td><%#Eval("Gru_Descripcion")%></td>
</tr>
<tr>
<td><b>Subgrupo</b></td>
<td><%#Eval("Sgr_Descripcion")%></td>
</tr>
<tr>
<td><b>Tipo</b></td>
<asp:Label id="lblItemT_Tipo" runat="server"
Text='<%#Eval("Pro_Tipo")%>' />
</tr>
<tr>
<td><b>Impuesto</b></td>
<td><%#Eval("Pro_FKP_IDImpuesto")%></td>
</tr>
</table>
</div>
<asp:LinkButton id="btnEdit" runat="server"
CommandName="Edit" Text="Edit Details" /><br />
<asp:LinkButton id="btnInsert" runat="server"
CommandName="New" Text="Add New Product" />
</ItemTemplate>
</asp:FormView>

My VB.NET code is:

Imports System
Imports System.Data
Imports System.Data.SqlClient

Partial Class DsxProductosEdit_aspx

Sub Page_Load(obj As Object, e As EventArgs)
If Session.Item("IDUsuario") is Nothing Then
Response.Redirect("../DsxStartPage.aspx")
End If


End Sub

Sub FormViewProductos_DataBound(sender As object, e As EventArgs)

Dim labelTipo As Label = FormViewProductos.FindControl(" lblItemT_Tipo")

labelTipo.Text = "Producto Terminado"
End Sub


End Class

HOW I CAN SOLVE THIS PROBLEM?

THANK′S.

The first suggestion is for you to just do the direct control access. Your code behind should be:

Sub FormViewProductos_DataBound(sender As object, e As EventArgs)

labelTipo.Text = "Producto Terminado"
End Sub

The reason why you weren't finding that control is because you were looking for and ID of "lblItemT_Tipo" (notice the space in the front) instead of a straight "lblItemT_Tipo"

Hi,

Thank you for your assistance. Iremoves the space and again the same mistake appears.


I see that label is in the item template. This means there will be as many labels on the page as there are records in the data set. Therefore DataGrid has more than one instance of the control. The key is to search in the right container. Have a look at this article

http://odetocode.com/Articles/116.aspx
Thank you, your help was very usefulBig Smile [:D]

0 comments:

Post a Comment