M
Michael Kolias
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
ropDownList runat="server" ID="ddlStore"
EnableViewState="true"></asp
ropDownList>
</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()
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
EnableViewState="true"></asp
</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()