DropDownList problems inside a datagrid

Discussion in 'ASP .Net Datagrid Control' started by Michael Kolias, Jun 10, 2005.

  1. Hi everybody,

    I am having a problem getting the selected value of a drop down list that is
    populated dynamically inside a datagrid control. When I try to access the
    selected item on the datagrid_Update function I get an Object reference not
    set to an instance of an object.

    I am reakky stuck here and I would appreciate any help from you guys.


    Michael


    Here is the html markup for the dropdown list inside the datagrid
    ..
    ..
    ..
    <asp:TemplateColumn HeaderText="Store"
    HeaderStyle-CssClass="registerDG-store-header">
    <itemtemplate>
    <asp:Label ID="lblStore" runat="server"
    Text='<%#DataBinder.Eval(Container.DataItem, "store_name")%>'></asp:Label>
    </itemtemplate>
    <edititemtemplate><asp:DropDownList runat="server" ID="ddlStore"
    EnableViewState="true"></asp:DropDownList>
    </edititemtemplate>
    </asp:TemplateColumn>
    ..
    ..
    ..

    Here is the code snippet inside the datagrid_ItemCreated Sub that populates
    the DropDownList
    This works fine and selects the item I want selected.

    Dim ddl As DropDownList
    ddl = CType(e.Item.Cells(1).Controls(1), DropDownList)

    Dim register As CRegister
    register = New CRegister(registersDG.DataKeys(CInt(e.Item.ItemIndex)))

    Dim oCmd As SqlCommand = Nothing
    Dim oDr As SqlDataReader = Nothing

    Try
    oCn.Open()
    oCmd = oCn.CreateCommand
    oCmd.CommandType = CommandType.Text
    oCmd.CommandText = "SELECT * FROM STORES;"

    oDr = oCmd.ExecuteReader

    If oDr.HasRows Then
    While oDr.Read
    ddl.Items.Add(New ListItem(oDr.Item("store_name"),
    oDr.Item("store_id").ToString))
    End While
    End If

    ddl.SelectedValue = register.Store.StoreID


    Catch SQLEx As SqlException
    ExMsg(SQLEx)

    Catch ex As Exception
    ExMsg(ex)

    Finally
    If Not IsNothing(register) Then register.Dispose() : register = Nothing
    If Not IsNothing(oDr) Then oDr.Close() : oDr = Nothing
    If Not IsNothing(oCmd) Then oCmd.Dispose() : oCmd = Nothing
    If Not IsNothing(oCn) Then oCn.Close()

    End Try


    Here is the code for the datagrid_Update function which trows the exception

    Dim register As CRegister
    Dim tb As TextBox
    Dim cb As CheckBox
    Dim ddl As DropDownList

    Try
    tb = CType(e.Item.Cells(0).FindControl("txtRegisterName"), TextBox)
    ddl = CType(e.Item.Cells(1).FindControl("ddlStore"), DropDownList)
    cb = CType(e.Item.Cells(2).FindControl("chkActive"), CheckBox)

    register = New CRegister(registersDG.DataKeys(CInt(e.Item.ItemIndex)))
    register.RegisterName = tb.Text.Trim
    register.Active = cb.Checked

    '*****************************************************************
    'The next line throws Object Reference not set to an instance of an object
    exception
    'The ddl.Items.Count property returns 0
    'The ddl.ID property returns the correct ID of the dropdownlist
    '*****************************************************************
    register.Store.StoreID = Integer.Parse(ddl.SelectedItem.Value)


    If register.Update() Then status.Text = "Register Updated."

    Catch ex As Exception
    status.Text = ExMsg(ex)

    Finally
    If Not IsNothing(register) Then register.Dispose() : register = Nothing

    End Try

    registersDG.EditItemIndex = -1
    BindGrid()
     
    Michael Kolias, Jun 10, 2005
    #1
    1. Advertising

  2. I found the problem.
    I moved the code for populating the dropdown list from the
    datagrid_ItemCreated to the
    datagrid_ItemDataBound and now it works just fine.

    "Michael Kolias" <> wrote in message
    news:...
    > Hi everybody,
    >
    > I am having a problem getting the selected value of a drop down list that

    is
    > populated dynamically inside a datagrid control. When I try to access the
    > selected item on the datagrid_Update function I get an Object reference

    not
    > set to an instance of an object.
    >
    > I am reakky stuck here and I would appreciate any help from you guys.
    >
    >
    > Michael
    >
    >
    > Here is the html markup for the dropdown list inside the datagrid
    > .
    > .
    > .
    > <asp:TemplateColumn HeaderText="Store"
    > HeaderStyle-CssClass="registerDG-store-header">
    > <itemtemplate>
    > <asp:Label ID="lblStore" runat="server"
    > Text='<%#DataBinder.Eval(Container.DataItem, "store_name")%>'></asp:Label>
    > </itemtemplate>
    > <edititemtemplate><asp:DropDownList runat="server" ID="ddlStore"
    > EnableViewState="true"></asp:DropDownList>
    > </edititemtemplate>
    > </asp:TemplateColumn>
    > .
    > .
    > .
    >
    > Here is the code snippet inside the datagrid_ItemCreated Sub that

    populates
    > the DropDownList
    > This works fine and selects the item I want selected.
    >
    > Dim ddl As DropDownList
    > ddl = CType(e.Item.Cells(1).Controls(1), DropDownList)
    >
    > Dim register As CRegister
    > register = New CRegister(registersDG.DataKeys(CInt(e.Item.ItemIndex)))
    >
    > Dim oCmd As SqlCommand = Nothing
    > Dim oDr As SqlDataReader = Nothing
    >
    > Try
    > oCn.Open()
    > oCmd = oCn.CreateCommand
    > oCmd.CommandType = CommandType.Text
    > oCmd.CommandText = "SELECT * FROM STORES;"
    >
    > oDr = oCmd.ExecuteReader
    >
    > If oDr.HasRows Then
    > While oDr.Read
    > ddl.Items.Add(New ListItem(oDr.Item("store_name"),
    > oDr.Item("store_id").ToString))
    > End While
    > End If
    >
    > ddl.SelectedValue = register.Store.StoreID
    >
    >
    > Catch SQLEx As SqlException
    > ExMsg(SQLEx)
    >
    > Catch ex As Exception
    > ExMsg(ex)
    >
    > Finally
    > If Not IsNothing(register) Then register.Dispose() : register = Nothing
    > If Not IsNothing(oDr) Then oDr.Close() : oDr = Nothing
    > If Not IsNothing(oCmd) Then oCmd.Dispose() : oCmd = Nothing
    > If Not IsNothing(oCn) Then oCn.Close()
    >
    > End Try
    >
    >
    > Here is the code for the datagrid_Update function which trows the

    exception
    >
    > Dim register As CRegister
    > Dim tb As TextBox
    > Dim cb As CheckBox
    > Dim ddl As DropDownList
    >
    > Try
    > tb = CType(e.Item.Cells(0).FindControl("txtRegisterName"), TextBox)
    > ddl = CType(e.Item.Cells(1).FindControl("ddlStore"), DropDownList)
    > cb = CType(e.Item.Cells(2).FindControl("chkActive"), CheckBox)
    >
    > register = New CRegister(registersDG.DataKeys(CInt(e.Item.ItemIndex)))
    > register.RegisterName = tb.Text.Trim
    > register.Active = cb.Checked
    >
    > '*****************************************************************
    > 'The next line throws Object Reference not set to an instance of an object
    > exception
    > 'The ddl.Items.Count property returns 0
    > 'The ddl.ID property returns the correct ID of the dropdownlist
    > '*****************************************************************
    > register.Store.StoreID = Integer.Parse(ddl.SelectedItem.Value)
    >
    >
    > If register.Update() Then status.Text = "Register Updated."
    >
    > Catch ex As Exception
    > status.Text = ExMsg(ex)
    >
    > Finally
    > If Not IsNothing(register) Then register.Dispose() : register = Nothing
    >
    > End Try
    >
    > registersDG.EditItemIndex = -1
    > BindGrid()
    >
    >
     
    Michael Kolias, Jun 10, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dominic
    Replies:
    2
    Views:
    736
    Saravana
    Jul 4, 2003
  2. =?Utf-8?B?QXVkcmV5?=
    Replies:
    2
    Views:
    416
    =?Utf-8?B?QXVkcmV5?=
    Feb 6, 2004
  3. Luis Esteban Valencia
    Replies:
    0
    Views:
    551
    Luis Esteban Valencia
    Mar 23, 2005
  4. Michael Kolias

    DropDownList problems inside a datagrid

    Michael Kolias, Jun 10, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    491
    Michael Kolias
    Jun 10, 2005
  5. keithb
    Replies:
    1
    Views:
    12,137
    shauns1
    Nov 1, 2006
Loading...

Share This Page