473,761 Members | 7,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DropDownList problems inside a datagrid

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:TemplateCo lumn HeaderText="Sto re"
HeaderStyle-CssClass="regis terDG-store-header">
<itemtemplate >
<asp:Label ID="lblStore" runat="server"
Text='<%#DataBi nder.Eval(Conta iner.DataItem, "store_name")%> '></asp:Label>
</itemtemplate>
<edititemtempla te><asp:DropDow nList runat="server" ID="ddlStore"
EnableViewState ="true"></asp:DropDownLis t>
</edititemtemplat e>
</asp:TemplateCol umn>
..
..
..

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

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

Dim register As CRegister
register = New CRegister(regis tersDG.DataKeys (CInt(e.Item.It emIndex)))

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

Try
oCn.Open()
oCmd = oCn.CreateComma nd
oCmd.CommandTyp e = CommandType.Tex t
oCmd.CommandTex t = "SELECT * FROM STORES;"

oDr = oCmd.ExecuteRea der

If oDr.HasRows Then
While oDr.Read
ddl.Items.Add(N ew ListItem(oDr.It em("store_name" ),
oDr.Item("store _id").ToString) )
End While
End If

ddl.SelectedVal ue = register.Store. StoreID
Catch SQLEx As SqlException
ExMsg(SQLEx)

Catch ex As Exception
ExMsg(ex)

Finally
If Not IsNothing(regis ter) Then register.Dispos e() : 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.Ce lls(0).FindCont rol("txtRegiste rName"), TextBox)
ddl = CType(e.Item.Ce lls(1).FindCont rol("ddlStore") , DropDownList)
cb = CType(e.Item.Ce lls(2).FindCont rol("chkActive" ), CheckBox)

register = New CRegister(regis tersDG.DataKeys (CInt(e.Item.It emIndex)))
register.Regist erName = 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(d dl.SelectedItem .Value)
If register.Update () Then status.Text = "Register Updated."

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

Finally
If Not IsNothing(regis ter) Then register.Dispos e() : register = Nothing

End Try

registersDG.Edi tItemIndex = -1
BindGrid()
Nov 19 '05 #1
1 1373
I found the problem.
I moved the code for populating the dropdown list from the
datagrid_ItemCr eated to the
datagrid_ItemDa taBound and now it works just fine.

"Michael Kolias" <mi***@alspacon sulting.com> wrote in message
news:xO******** ************@sp eakeasy.net...
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:TemplateCo lumn HeaderText="Sto re"
HeaderStyle-CssClass="regis terDG-store-header">
<itemtemplate >
<asp:Label ID="lblStore" runat="server"
Text='<%#DataBi nder.Eval(Conta iner.DataItem, "store_name")%> '></asp:Label>
</itemtemplate>
<edititemtempla te><asp:DropDow nList runat="server" ID="ddlStore"
EnableViewState ="true"></asp:DropDownLis t>
</edititemtemplat e>
</asp:TemplateCol umn>
.
.
.

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

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

Dim register As CRegister
register = New CRegister(regis tersDG.DataKeys (CInt(e.Item.It emIndex)))

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

Try
oCn.Open()
oCmd = oCn.CreateComma nd
oCmd.CommandTyp e = CommandType.Tex t
oCmd.CommandTex t = "SELECT * FROM STORES;"

oDr = oCmd.ExecuteRea der

If oDr.HasRows Then
While oDr.Read
ddl.Items.Add(N ew ListItem(oDr.It em("store_name" ),
oDr.Item("store _id").ToString) )
End While
End If

ddl.SelectedVal ue = register.Store. StoreID
Catch SQLEx As SqlException
ExMsg(SQLEx)

Catch ex As Exception
ExMsg(ex)

Finally
If Not IsNothing(regis ter) Then register.Dispos e() : 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.Ce lls(0).FindCont rol("txtRegiste rName"), TextBox)
ddl = CType(e.Item.Ce lls(1).FindCont rol("ddlStore") , DropDownList)
cb = CType(e.Item.Ce lls(2).FindCont rol("chkActive" ), CheckBox)

register = New CRegister(regis tersDG.DataKeys (CInt(e.Item.It emIndex)))
register.Regist erName = 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(d dl.SelectedItem .Value)
If register.Update () Then status.Text = "Register Updated."

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

Finally
If Not IsNothing(regis ter) Then register.Dispos e() : register = Nothing

End Try

registersDG.Edi tItemIndex = -1
BindGrid()

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
17031
by: Dominic | last post by:
Hi guys, I'm not sure if this question belongs to FAQ, but I couldn't find a concrete answer. I created a Datagrid control using ItemTemplate, but it's NOT a in-place editing datagrid. One of the columns of the data grid contains a DropDownlist. I managed to create this datagrid control as follows.
2
2049
by: Ali | last post by:
I am adding a DropDownList control to my DataGrid footer template. I load my DropDownList using a function that returns an arrayList. I can see all my items in the DropDownList, but when I select an item from the list, I am unable to trap the DropDownList SelectedIndexChanged event. This is a snippet of my DataGrid: <asp:TemplateColumn> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "State") %>
1
2665
by: George Durzi | last post by:
When my datagrid is in edit mode, one of my columns is edited using a drop down list. I'm able to bind the DropDownList to a DataSource when in edit mode. HOWEVER, I can't preset the DropDownList's value when the Edit button is clicked for that row inside the datagrid. Here's a simplified version of my datagrid: <asp:datagrid id="dgHPU" runat="server"
2
2023
by: jason | last post by:
Pardon my ignorance on this. The below code works, except, when I edit a record and update the two drop downs take the first entry in the dropdownlist if not selected. I'd also like the dropdown to show the current value in edit mode. I'm sure this is a common question. I've reviewed several related post and tried them out to no avial. Some of the offerings in listgroup look right on, but are not clear on where to handle the suggested...
0
1535
by: Luis Esteban Valencia | last post by:
Hello. I have a datagrid with one row. I have a button that adds a new row. I am trying to implement that when the user selects one product it must change the price on the quantity column. Anyway when the user selects the first dropdownlist the dropdownselected_indexchanged is firring but when the user chagnes the second dropdownlist its not firing. aahh! if the user selects any of both dropdownlist the datagrid stays with only one row?...
2
16101
by: glenn | last post by:
Hi folks, I am trying to determine which item in a DropDownList Web control has been selected. I have posted an OnSelectedIndexChanged subroutine in my code with a reference to the subroutine in my asp tag as shown here: <asp:DropDownList id="ddlTo" runat="server"
15
3129
by: glenn | last post by:
Hi folks, I have a DropDownList in a DataGrid that is populated from records in a database. I want to add a value that might be a string such as "Select a Company" for the first item since an OnSelectedIndex event is not fired if you select the first item. Does anyone know of an easy way to do this?
2
2020
by: steven | last post by:
i've got a dropDownList that I'm trying to populate from my code behind as follows: uxVehicleColourEdit.DataSource = oDsLookups.Tables; if the dropDown is placed inside a datagrid, i get compile time errors stating the control doesn't exist in the current content: <asp:DataGrid ID="tmp" runat="server"> <Columns>
0
1774
by: stevem2112 | last post by:
I have a datagrid with 2 Template columns. One column has DropDownLists and the other has Textboxes. I bind each DDL in the ItemCreated event. This datagrid is inside a UserControl that is inside a user control (long story). Anyway, the user clicks a save button on the parent control to initiate the postback. The parent then calls a Save method (in the child control) from the button event handler. When I iterate through the datagrid...
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7332
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.