Object reference not set to an instance of an object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben

    Object reference not set to an instance of an object

    Hi,

    i try to change the display value of a radiobuttonlist embedded in a
    gridview, but only in normal mode. In edit mode, the radiobuttonlist must
    appears.

    With this code below, i get what i want in normal mode (displayed values
    (yes/no) are ok), but when i try to edit the row, i get:

    "Object reference not set to an instance of an object."
    at line: lbl.Text = "yes"

    So, what's wrong here?
    By the way, i also tried with:
    If (e.Row.RowState And DataControlRowS tate.Normal) =
    DataControlRowS tate.Normal Then
    ....

    and with:
    'If (e.Row.RowState And DataControlRowS tate.Normal) = 0 Then
    .....

    but same error ..
    Thanks
    Ben

    <asp:TemplateFi eld>
    <EditItemTempla te>
    <asp:RadioButto nList ID="r1" SelectedValue=' <%# Bind("myfield") %>'
    runat="server">
    </asp:RadioButton List>
    </EditItemTemplat e>
    <ItemTemplate >
    <asp:Label ID="Labelvp" runat="server" Text='<%# Bind("myfield")
    %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateFie ld>

    Protected Sub GridView1_RowDa taBound(ByVal sender As Object, ByVal e As
    System.Web.UI.W ebControls.Grid ViewRowEventArg s) Handles
    GridView1.RowDa taBound
    If e.Row.RowType = DataControlRowT ype.DataRow Then
    Dim mf As Boolean = DataBinder.Eval (e.Row.DataItem , "myfield")
    Dim lbl As Label = e.Row.FindContr ol("Labelvp")

    If myfield = True Then
    lbl.Text = "yes"
    Else
    lbl.Text = "no"
    End If
    End If
    End Sub


  • Rain

    #2
    Re: Object reference not set to an instance of an object

    You need to detect what the item type is because you have ItemTemplate,
    AlternateItem, Footer, Header etc. You have put this in the ItemTemplate,
    but there are other Template
    types. I cant remember the exact sytax for the event args, but its
    something like.


    if( e.TemplateType == TemplateItem.It emTemplate ) // Do Somtehng




    "Ben" <ben@nol.vbwrot e in message
    news:u3PySb2wIH A.3780@TK2MSFTN GP03.phx.gbl...
    Hi,
    >
    i try to change the display value of a radiobuttonlist embedded in a
    gridview, but only in normal mode. In edit mode, the radiobuttonlist must
    appears.
    >
    With this code below, i get what i want in normal mode (displayed values
    (yes/no) are ok), but when i try to edit the row, i get:
    >
    "Object reference not set to an instance of an object."
    at line: lbl.Text = "yes"
    >
    So, what's wrong here?
    By the way, i also tried with:
    If (e.Row.RowState And DataControlRowS tate.Normal) =
    DataControlRowS tate.Normal Then
    ...
    >
    and with:
    'If (e.Row.RowState And DataControlRowS tate.Normal) = 0 Then
    ....
    >
    but same error ..
    Thanks
    Ben
    >
    <asp:TemplateFi eld>
    <EditItemTempla te>
    <asp:RadioButto nList ID="r1" SelectedValue=' <%# Bind("myfield") %>'
    runat="server">
    </asp:RadioButton List>
    </EditItemTemplat e>
    <ItemTemplate >
    <asp:Label ID="Labelvp" runat="server" Text='<%# Bind("myfield")
    %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateFie ld>
    >
    Protected Sub GridView1_RowDa taBound(ByVal sender As Object, ByVal e As
    System.Web.UI.W ebControls.Grid ViewRowEventArg s) Handles
    GridView1.RowDa taBound
    If e.Row.RowType = DataControlRowT ype.DataRow Then
    Dim mf As Boolean = DataBinder.Eval (e.Row.DataItem , "myfield")
    Dim lbl As Label = e.Row.FindContr ol("Labelvp")
    >
    If myfield = True Then
    lbl.Text = "yes"
    Else
    lbl.Text = "no"
    End If
    End If
    End Sub
    >
    >

    Comment

    • Ben

      #3
      Re: Object reference not set to an instance of an object

      Thanks for replying. but

      In the sub GridView1_RowDa taBound, i only can find: e.Row.TemplateC ontrol =
      ....

      I couldn't find e.TemplateType nor TemplateItem.It emTemplate in the
      intellisense.



      "Rain" <me@myplace.com schreef in bericht
      news:e97lEw2wIH A.3780@TK2MSFTN GP03.phx.gbl...
      You need to detect what the item type is because you have ItemTemplate,
      AlternateItem, Footer, Header etc. You have put this in the ItemTemplate,
      but there are other Template
      types. I cant remember the exact sytax for the event args, but its
      something like.
      >
      >
      if( e.TemplateType == TemplateItem.It emTemplate ) // Do Somtehng
      >
      >
      >
      >
      "Ben" <ben@nol.vbwrot e in message
      news:u3PySb2wIH A.3780@TK2MSFTN GP03.phx.gbl...
      >Hi,
      >>
      >i try to change the display value of a radiobuttonlist embedded in a
      >gridview, but only in normal mode. In edit mode, the radiobuttonlist must
      >appears.
      >>
      >With this code below, i get what i want in normal mode (displayed values
      >(yes/no) are ok), but when i try to edit the row, i get:
      >>
      >"Object reference not set to an instance of an object."
      >at line: lbl.Text = "yes"
      >>
      >So, what's wrong here?
      >By the way, i also tried with:
      >If (e.Row.RowState And DataControlRowS tate.Normal) =
      >DataControlRow State.Normal Then
      >...
      >>
      >and with:
      > 'If (e.Row.RowState And DataControlRowS tate.Normal) = 0 Then
      >....
      >>
      >but same error ..
      >Thanks
      >Ben
      >>
      ><asp:TemplateF ield>
      ><EditItemTempl ate>
      ><asp:RadioButt onList ID="r1" SelectedValue=' <%# Bind("myfield") %>'
      >runat="server" >
      ></asp:RadioButton List>
      ></EditItemTemplat e>
      ><ItemTemplat e>
      ><asp:Label ID="Labelvp" runat="server" Text='<%# Bind("myfield")
      >%>'></asp:Label>
      ></ItemTemplate>
      ></asp:TemplateFie ld>
      >>
      >Protected Sub GridView1_RowDa taBound(ByVal sender As Object, ByVal e As
      >System.Web.UI. WebControls.Gri dViewRowEventAr gs) Handles
      >GridView1.RowD ataBound
      > If e.Row.RowType = DataControlRowT ype.DataRow Then
      > Dim mf As Boolean = DataBinder.Eval (e.Row.DataItem , "myfield")
      > Dim lbl As Label = e.Row.FindContr ol("Labelvp")
      >>
      > If myfield = True Then
      > lbl.Text = "yes"
      > Else
      > lbl.Text = "no"
      > End If
      > End If
      > End Sub
      >>
      >>
      >
      >

      Comment

      • Rain

        #4
        Re: Object reference not set to an instance of an object



        Sorry, wrong operation.

        You may want to try something like.

        e.Row.Cells[0].FindControl('i d');


        "Ben" <ben@nol.vbwrot e in message
        news:e5XUtF3wIH A.4488@TK2MSFTN GP04.phx.gbl...
        Thanks for replying. but
        >
        In the sub GridView1_RowDa taBound, i only can find: e.Row.TemplateC ontrol
        = ...
        >
        I couldn't find e.TemplateType nor TemplateItem.It emTemplate in the
        intellisense.
        >
        >
        >
        "Rain" <me@myplace.com schreef in bericht
        news:e97lEw2wIH A.3780@TK2MSFTN GP03.phx.gbl...
        >You need to detect what the item type is because you have ItemTemplate,
        >AlternateIte m, Footer, Header etc. You have put this in the ItemTemplate,
        >but there are other Template
        >types. I cant remember the exact sytax for the event args, but its
        >something like.
        >>
        >>
        >if( e.TemplateType == TemplateItem.It emTemplate ) // Do Somtehng
        >>
        >>
        >>
        >>
        >"Ben" <ben@nol.vbwrot e in message
        >news:u3PySb2wI HA.3780@TK2MSFT NGP03.phx.gbl.. .
        >>Hi,
        >>>
        >>i try to change the display value of a radiobuttonlist embedded in a
        >>gridview, but only in normal mode. In edit mode, the radiobuttonlist
        >>must appears.
        >>>
        >>With this code below, i get what i want in normal mode (displayed values
        >>(yes/no) are ok), but when i try to edit the row, i get:
        >>>
        >>"Object reference not set to an instance of an object."
        >>at line: lbl.Text = "yes"
        >>>
        >>So, what's wrong here?
        >>By the way, i also tried with:
        >>If (e.Row.RowState And DataControlRowS tate.Normal) =
        >>DataControlRo wState.Normal Then
        >>...
        >>>
        >>and with:
        >> 'If (e.Row.RowState And DataControlRowS tate.Normal) = 0 Then
        >>....
        >>>
        >>but same error ..
        >>Thanks
        >>Ben
        >>>
        >><asp:Template Field>
        >><EditItemTemp late>
        >><asp:RadioBut tonList ID="r1" SelectedValue=' <%# Bind("myfield") %>'
        >>runat="server ">
        >></asp:RadioButton List>
        >></EditItemTemplat e>
        >><ItemTemplate >
        >><asp:Label ID="Labelvp" runat="server" Text='<%# Bind("myfield")
        >>%>'></asp:Label>
        >></ItemTemplate>
        >></asp:TemplateFie ld>
        >>>
        >>Protected Sub GridView1_RowDa taBound(ByVal sender As Object, ByVal e As
        >>System.Web.UI .WebControls.Gr idViewRowEventA rgs) Handles
        >>GridView1.Row DataBound
        >> If e.Row.RowType = DataControlRowT ype.DataRow Then
        >> Dim mf As Boolean = DataBinder.Eval (e.Row.DataItem ,
        >>"myfield")
        >> Dim lbl As Label = e.Row.FindContr ol("Labelvp")
        >>>
        >> If myfield = True Then
        >> lbl.Text = "yes"
        >> Else
        >> lbl.Text = "no"
        >> End If
        >> End If
        >> End Sub
        >>>
        >>>
        >>
        >>
        >
        >

        Comment

        • Ben

          #5
          Re: Object reference not set to an instance of an object

          But i did that: i tried with:

          Dim lbl As Label = e.Row.FindContr ol("Labelvp")

          and with this:

          Dim lbl As Label = e.Row.Cells(5). FindControl("La belvp")

          but both gives the same error ...



          "Rain" <me@myplace.com schreef in bericht
          news:uizkDT3wIH A.1236@TK2MSFTN GP02.phx.gbl...
          >
          >
          Sorry, wrong operation.
          >
          You may want to try something like.
          >
          e.Row.Cells[0].FindControl('i d');
          >
          >
          "Ben" <ben@nol.vbwrot e in message
          news:e5XUtF3wIH A.4488@TK2MSFTN GP04.phx.gbl...
          >Thanks for replying. but
          >>
          >In the sub GridView1_RowDa taBound, i only can find:
          >e.Row.Template Control = ...
          >>
          >I couldn't find e.TemplateType nor TemplateItem.It emTemplate in the
          >intellisense .
          >>
          >>
          >>
          >"Rain" <me@myplace.com schreef in bericht
          >news:e97lEw2wI HA.3780@TK2MSFT NGP03.phx.gbl.. .
          >>You need to detect what the item type is because you have ItemTemplate,
          >>AlternateItem , Footer, Header etc. You have put this in the
          >>ItemTemplat e, but there are other Template
          >>types. I cant remember the exact sytax for the event args, but its
          >>something like.
          >>>
          >>>
          >>if( e.TemplateType == TemplateItem.It emTemplate ) // Do Somtehng
          >>>
          >>>
          >>>
          >>>
          >>"Ben" <ben@nol.vbwrot e in message
          >>news:u3PySb2w IHA.3780@TK2MSF TNGP03.phx.gbl. ..
          >>>Hi,
          >>>>
          >>>i try to change the display value of a radiobuttonlist embedded in a
          >>>gridview, but only in normal mode. In edit mode, the radiobuttonlist
          >>>must appears.
          >>>>
          >>>With this code below, i get what i want in normal mode (displayed
          >>>values (yes/no) are ok), but when i try to edit the row, i get:
          >>>>
          >>>"Object reference not set to an instance of an object."
          >>>at line: lbl.Text = "yes"
          >>>>
          >>>So, what's wrong here?
          >>>By the way, i also tried with:
          >>>If (e.Row.RowState And DataControlRowS tate.Normal) =
          >>>DataControlR owState.Normal Then
          >>>...
          >>>>
          >>>and with:
          >>> 'If (e.Row.RowState And DataControlRowS tate.Normal) = 0 Then
          >>>....
          >>>>
          >>>but same error ..
          >>>Thanks
          >>>Ben
          >>>>
          >>><asp:Templat eField>
          >>><EditItemTem plate>
          >>><asp:RadioBu ttonList ID="r1" SelectedValue=' <%# Bind("myfield") %>'
          >>>runat="serve r">
          >>></asp:RadioButton List>
          >>></EditItemTemplat e>
          >>><ItemTemplat e>
          >>><asp:Label ID="Labelvp" runat="server" Text='<%# Bind("myfield")
          >>>%>'></asp:Label>
          >>></ItemTemplate>
          >>></asp:TemplateFie ld>
          >>>>
          >>>Protected Sub GridView1_RowDa taBound(ByVal sender As Object, ByVal e As
          >>>System.Web.U I.WebControls.G ridViewRowEvent Args) Handles
          >>>GridView1.Ro wDataBound
          >>> If e.Row.RowType = DataControlRowT ype.DataRow Then
          >>> Dim mf As Boolean = DataBinder.Eval (e.Row.DataItem ,
          >>>"myfield")
          >>> Dim lbl As Label = e.Row.FindContr ol("Labelvp")
          >>>>
          >>> If myfield = True Then
          >>> lbl.Text = "yes"
          >>> Else
          >>> lbl.Text = "no"
          >>> End If
          >>> End If
          >>> End Sub
          >>>>
          >>>>
          >>>
          >>>
          >>
          >>
          >
          >

          Comment

          Working...