Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > DropDownList problems inside a datagrid

Reply
Thread Tools

DropDownList problems inside a datagrid

 
 
Michael Kolias
Guest
Posts: n/a
 
      06-10-2005
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><aspropDownList runat="server" ID="ddlStore"
EnableViewState="true"></aspropDownList>
</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.ItemInd ex)))

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.ItemInd ex)))
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()


 
Reply With Quote
 
 
 
 
Michael Kolias
Guest
Posts: n/a
 
      06-10-2005
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" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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><aspropDownList runat="server" ID="ddlStore"
> EnableViewState="true"></aspropDownList>
> </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.ItemInd ex)))
>
> 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.ItemInd ex)))
> 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()
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DropDownList inside GridView: How to set SelectedIndex for the DropDownList? keithb ASP .Net 1 11-01-2006 05:24 AM
DropDownList problems inside a datagrid Michael Kolias ASP .Net 1 06-10-2005 03:21 PM
Losing one row of datagrid when changing dropdownlist inside it. Luis Esteban Valencia ASP .Net 0 03-23-2005 05:04 PM
setting the default item on a dropdownlist inside a datagrid =?Utf-8?B?QXVkcmV5?= ASP .Net 2 02-06-2004 04:41 AM
Catching an SelectedIndexChanged event from a DropDownList inside a DataGrid Dominic ASP .Net 2 07-04-2003 04:43 AM



Advertisments