<asp:datagrid id=DataGrid1 style="Z-INDEX: 104; LEFT: 304px; POSITION: absolute; TOP: 104px" runat="server" CssClass="grid" AutoGenerateColumns="False">
<Columns>

<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" />
<asp:BoundColumn DataField="Message Id" ReadOnly="False" HeaderText="Message Id" />
<asp:TemplateColumn HeaderText="Opcode">
<ItemTemplate>
<%#Container.DataItem("Opcode")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList AutoPostBack = false id="ddl_Opcode" runat="server" DataValueField="Opcode" DataTextField ="Opcode" DataSource ="<%#TempDataView%>" >

</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>

<asp:BoundColumn DataField="Message Status" HeaderText="Message Status" ReadOnly="True" />
<asp:BoundColumn DataField="Error Descp" HeaderText="Error Descp" ReadOnly="True" />

</Columns>
</asp:datagrid>

Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
populateDropDownList()
DataGrid1.EditItemIndex = e.Item.ItemIndex
End Sub


Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand
Dim myDDL As New DropDownList
myDDL = CType(e.Item.FindControl("ddl_Opcode"), DropDownList)
Response.Write(myDDL.SelectedItem.Value)
End Sub


Private Function populateDropDownList()
Dim mydatatable As New DataTable
mydatatable.Columns.Add("Opcode", Type.GetType("System.String"))
' Declare row
Dim myrow As DataRow
' create new row
myrow = mydatatable.NewRow
myrow("Opcode") = "CREATE"
mydatatable.Rows.Add(myrow)
myrow = mydatatable.NewRow
myrow("Opcode") = "UPDATE"
mydatatable.Rows.Add(myrow)
myrow = mydatatable.NewRow
myrow("Opcode") = "DELETE"
mydatatable.Rows.Add(myrow)
TempDataView = mydatatable.DefaultView


End Function


Here is the code...
It shows an error : Object refernce not set
The selected index value remains nothing...
Can u tell me where I have gone wrong?

Dani AI

Generated

Most likely cause of the "Object reference not set" in the UpdateCommand: the DropDownList either isn't present in the EditItem when Update runs, or it exists but has no items. That typically happens when the DataGrid is not rebound into edit mode (so the EditItemTemplate never gets created/bound) or when the data source used by the template (the TempDataView-style value) is not repopulated before the grid is bound. In 's EditCommand the helper is called and EditItemIndex is set, but the grid is never re-bound — that gap prevents the DropDownList from being instantiated and populated.

A robust pattern is to bind the DropDownList when the edit item is created (ItemDataBound) so the control is guaranteed to get its items when the template is instantiated. Example (VB.NET, adapt control names to the real IDs used in the page):

Protected Sub myGrid_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles myGrid.ItemDataBound
  If e.Item.ItemType = ListItemType.EditItem Then
    Dim ddl As DropDownList = CType(e.Item.FindControl("ddlOpcode"), DropDownList)
    ddl.DataSource = GetOpcodeTable()   ' return a DataTable/DataView
    ddl.DataTextField = "Opcode"
    ddl.DataValueField = "Opcode"
    ddl.DataBind()
    Dim drv = TryCast(e.Item.DataItem, DataRowView)
    If drv IsNot Nothing Then ddl.SelectedValue = CStr(drv("Opcode"))
  End If
End Sub

If switching to ItemDataBound is not desired, the minimal fix is to ensure the grid is rebound after setting the edit index and that TempDataView (or whatever datasource the template uses) is available before the DataBind call. Also add defensive checks in UpdateCommand so a missing control or missing selection won’t throw:

Dim ddl = TryCast(e.Item.FindControl("ddl_Opcode"), DropDownList)
If ddl IsNot Nothing AndAlso ddl.SelectedItem IsNot Nothing Then
  Dim val = ddl.SelectedValue
End If

A clarification about 's suggestion: setting AutoPostBack on the DropDownList is unnecessary for this use — UpdateCommand is fired by the grid's Update button, not by the drop-down. Final checklist: (1) repopulate or persist the drop-down datasource across postbacks or bind inside ItemDataBound; (2) call DataBind (or a single BindGrid method) immediately after setting EditItemIndex; (3) avoid re-binding the drop-down on every postback without restoring selection; (4) add null checks before reading SelectedItem.

hi,

you should assign autopostback=true

aspropDownList [B]AutoPostBack = true [/B]id="ddl_Opcode" runat="server" DataValueField="Opcode" DataTextField ="Opcode" DataSource ="<%#TempDataView%>" >

</aspropDownList>

and another thing you can directly assign mydatable.defualtview
instead of tempdatatable
Private Function populateDropDownList()
Dim mydatatable As New DataTable
mydatatable.Columns.Add("Opcode", Type.GetType("System.String"))
' Declare row
Dim myrow As DataRow
' create new row
myrow = mydatatable.NewRow
myrow("Opcode") = "CREATE"
mydatatable.Rows.Add(myrow)
myrow = mydatatable.NewRow
myrow("Opcode") = "UPDATE"
mydatatable.Rows.Add(myrow)
myrow = mydatatable.NewRow
myrow("Opcode") = "DELETE"
mydatatable.Rows.Add(myrow)
ddl_Opcode.Datasource = mydatatable.DefaultView
ddl_Opcode.Databind()


End Function
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.