Trying to build ddl during Datagrid Edit Command
-
When I run this code I get: Object reference not set to an instance of an object. It's referring to the line of: ddlTitle1.DataSource = DS Here's the code: Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand DataGrid1.EditItemIndex = e.Item.ItemIndex 'Build the DDL For the Title section Con1.Open() Dim strDDL As String strDDL = "SELECT DISTINCT Title FROM Employees ORDER BY Title" Dim da As New OleDb.OleDbDataAdapter() da.SelectCommand = New OleDb.OleDbCommand(strDDL, Con1) Dim DS As New DataSet() da.Fill(DS) Dim ddlTitle1 As DropDownList = e.Item.FindControl("ddlTitle") ddlTitle1.DataSource = DS ddlTitle1.DataMember = "Employees" ddlTitle1.DataTextField = "Title" ddlTitle1.DataValueField = "Title" ddlTitle1.DataBind() Con1.Close()
-
When I run this code I get: Object reference not set to an instance of an object. It's referring to the line of: ddlTitle1.DataSource = DS Here's the code: Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand DataGrid1.EditItemIndex = e.Item.ItemIndex 'Build the DDL For the Title section Con1.Open() Dim strDDL As String strDDL = "SELECT DISTINCT Title FROM Employees ORDER BY Title" Dim da As New OleDb.OleDbDataAdapter() da.SelectCommand = New OleDb.OleDbCommand(strDDL, Con1) Dim DS As New DataSet() da.Fill(DS) Dim ddlTitle1 As DropDownList = e.Item.FindControl("ddlTitle") ddlTitle1.DataSource = DS ddlTitle1.DataMember = "Employees" ddlTitle1.DataTextField = "Title" ddlTitle1.DataValueField = "Title" ddlTitle1.DataBind() Con1.Close()
Hi there, Looks like the container DataGridItem does not contain the dropdownlist
ddlTitle
at the time the FindControl is called, so the method cannot find this control and returns null. As a result, you get the null exception when you access the DataSource property of the ddlTitle1 object. -
Hi there, Looks like the container DataGridItem does not contain the dropdownlist
ddlTitle
at the time the FindControl is called, so the method cannot find this control and returns null. As a result, you get the null exception when you access the DataSource property of the ddlTitle1 object.I'm still gettting: Object reference not set to an instance of an object. ....pointing to ddlName.DataSource = DS ....with this code. I know it's in the second column of the datagrid, so Cells(1) is correct. DataGrid1.EditItemIndex = e.Item.ItemIndex 'Build the DDL For the Name section sCon1.Open() Dim strDDL As String strDDL = "SELECT SA_Names.Authority+' | '+SA_Names.ZNumber AS Expr1, " strDDL &= "SA_Names.Authority FROM SA_Names Order by Expr1" Dim cmd As New SqlCommand(strDDL, sCon1) Dim da As New SqlDataAdapter da.SelectCommand = cmd Dim DS As New DataSet da.Fill(DS) Dim ddlName As DropDownList = e.Item.Cells(1).FindControl("ddlName") ddlName.DataSource = DS ddlName.DataMember = "SA_Names" ddlName.DataTextField = "Expr1" ddlName.DataValueField = "Authority" ddlName.DataBind() sCon1.Close() ...any suggestions? Thanks!
-
I'm still gettting: Object reference not set to an instance of an object. ....pointing to ddlName.DataSource = DS ....with this code. I know it's in the second column of the datagrid, so Cells(1) is correct. DataGrid1.EditItemIndex = e.Item.ItemIndex 'Build the DDL For the Name section sCon1.Open() Dim strDDL As String strDDL = "SELECT SA_Names.Authority+' | '+SA_Names.ZNumber AS Expr1, " strDDL &= "SA_Names.Authority FROM SA_Names Order by Expr1" Dim cmd As New SqlCommand(strDDL, sCon1) Dim da As New SqlDataAdapter da.SelectCommand = cmd Dim DS As New DataSet da.Fill(DS) Dim ddlName As DropDownList = e.Item.Cells(1).FindControl("ddlName") ddlName.DataSource = DS ddlName.DataMember = "SA_Names" ddlName.DataTextField = "Expr1" ddlName.DataValueField = "Authority" ddlName.DataBind() sCon1.Close() ...any suggestions? Thanks!
-
How do you declare the dropdownlist ddlName in the datagrid, declaratively at design time or dynamically at runtime? Can you run your application in debug mode and see if the dropdownlist ddlName is in the Controls collection of the container?
minhpc_bk, If I run this code below, then label3.Text says "The dropdownlist is nothing" Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand DataGrid1.EditItemIndex = e.Item.ItemIndex Dim ddlTitle1 As DropDownList = e.Item.Cells(3).FindControl("ddlTitle") Con1.Open() Dim strDDL As String strDDL = "SELECT DISTINCT Title FROM Employees ORDER BY Title" Dim da As New OleDb.OleDbDataAdapter() da.SelectCommand = New OleDb.OleDbCommand(strDDL, Con1) Dim DS As New DataSet() da.Fill(DS) If ddlTitle1 Is Nothing Then Label3.Text = "The dropdownlist is not found" Else ddlTitle1.DataSource = DS ddlTitle1.DataMember = "Employees" ddlTitle1.DataTextField = "Title" ddlTitle1.DataValueField = "Title" ddlTitle1.DataBind() Con1.Close() End If I've also tried the OnItemCreated routine, but still no luck (ddl not present when I click the Edit Command): Sub OnItemCreated(ByVal e As DataGridItemEventArgs) Me.OnItemCreated(e) Dim i As Integer Select Case e.Item.ItemType Case ListItemType.Item 'Case ListItemType.EditItem Dim ddlName As DropDownList = New DropDownList() Con1.Open() Dim strDDL As String strDDL = "SELECT DISTINCT Title FROM Employees ORDER BY Title" Dim da As New OleDb.OleDbDataAdapter() da.SelectCommand = New OleDb.OleDbCommand(strDDL, Con1) Dim DS As New DataSet() da.Fill(DS) ddlName.DataSource = DS ddlName.DataTextField = "Title" ddlName.DataValueField = "Title" ddlName.DataBind() e.Item.Cells(3).Controls.Add(ddlName) End Select End Sub
-
minhpc_bk, If I run this code below, then label3.Text says "The dropdownlist is nothing" Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand DataGrid1.EditItemIndex = e.Item.ItemIndex Dim ddlTitle1 As DropDownList = e.Item.Cells(3).FindControl("ddlTitle") Con1.Open() Dim strDDL As String strDDL = "SELECT DISTINCT Title FROM Employees ORDER BY Title" Dim da As New OleDb.OleDbDataAdapter() da.SelectCommand = New OleDb.OleDbCommand(strDDL, Con1) Dim DS As New DataSet() da.Fill(DS) If ddlTitle1 Is Nothing Then Label3.Text = "The dropdownlist is not found" Else ddlTitle1.DataSource = DS ddlTitle1.DataMember = "Employees" ddlTitle1.DataTextField = "Title" ddlTitle1.DataValueField = "Title" ddlTitle1.DataBind() Con1.Close() End If I've also tried the OnItemCreated routine, but still no luck (ddl not present when I click the Edit Command): Sub OnItemCreated(ByVal e As DataGridItemEventArgs) Me.OnItemCreated(e) Dim i As Integer Select Case e.Item.ItemType Case ListItemType.Item 'Case ListItemType.EditItem Dim ddlName As DropDownList = New DropDownList() Con1.Open() Dim strDDL As String strDDL = "SELECT DISTINCT Title FROM Employees ORDER BY Title" Dim da As New OleDb.OleDbDataAdapter() da.SelectCommand = New OleDb.OleDbCommand(strDDL, Con1) Dim DS As New DataSet() da.Fill(DS) ddlName.DataSource = DS ddlName.DataTextField = "Title" ddlName.DataValueField = "Title" ddlName.DataBind() e.Item.Cells(3).Controls.Add(ddlName) End Select End Sub
bubberz, + Because you use the
FindControl
method to look for a specific control based on its id, so you have to assign theddlTitle
value to theID
property of the dropdownlist when you create it in theOnItemCreated
handler. Another way is to use the index value for theControls
collection of the container to get the child dropdownlist control. + In the OnItemCreated handler, you might also want to do the same in the case of theListItemType.AlternatingItem
.