Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to build dropdownlist during datagrid Edit Command

Status
Not open for further replies.

lanm

Programmer
Jul 7, 2005
244
0
0
US
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()
 
Don't know if you can use this but here is what I use in my datagrid:

Code:
<asp:DropDownList id="dropTypeAdd" runat="server">
        <asp:ListItem Selected="True">Band Night</asp:ListItem>
        <asp:ListItem>Fundraiser</asp:ListItem>
    </asp:DropDownList>

If I am thinking too "simple" my apologies. I'm new to ASP.Net and just learning my way around it.



I hope you find this post helpful.

Regards,

Mark
 
markdmac,

thanks, but what you've listed is for static data. Mine will need to be dynamic.

I just want to be able to build the dataadapter and Dataset by hand in the Edit_Command handler
 
This is how I do it.

Dim rs As New SqlCommand("SELECT Records FROM Table ", cn)

cn.Open()

cboBox.DataSource = rs.ExecuteReader
cboBox.DataTextField = "FieldName"
cboBox.DataValueField = "FieldName"
cboBox.DataBind()

cn.Close()

 
It could be that your FindControl is not finding the control. Step through the code and see if ddlTitle1 is NOTHING after the FindControl line.
 
Actually I belive you need to change that line and include a referenc to the cell you want to find the control in:
Code:
Dim ddlTitle1 As DropDownList = e.Item.[b]Cells(cell index)[/b].FindControl("ddlTitle")
 
Also, you might have to do it in another event handler:
Code:
OnItemCreated(ByVal e As DataGridItemEventArgs)
        MyBase.OnItemCreated(e)
        Dim i As Integer
        Select Case e.Item.ItemType
            Case ListItemType.EditItem

The datagrid might not have created the edititem control at the time it is handling onEdit. Not sure since I am still getting a hold of when all these events fire, and the order myself.

But I have sucess with this code, although I am adding the DL myself:

Code:
Sub OnItemCreated(ByVal e As DataGridItemEventArgs)
        MyBase.OnItemCreated(e)
        Dim i As Integer
        Select Case e.Item.ItemType
            Case ListItemType.EditItem
                Dim eventDL As DropDownList = New DropDownList()
                eventDL.DataSource = sqlSelect("Select * from [event]")
                eventDL.DataTextField = "eventName"
                eventDL.DataValueField = "pk_Event"
                eventDL.DataBind()
                e.Item.Cells(3).Controls.Add(eventDL)
 
If I use the following code, it looks as if the dropdownlist is nothing. My label3.Text is getting populated with "The dropdownlist is not found". I know it should be cells(3), and the id is in fact ddlTitle.
Here's the code:

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
 
cjburkha,

What Imports are you using?
I've got:
Imports System.Web.UI.Page

If I use the code:
Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated
MyBase.OnItemCreated(e)
MyBase.Oni()
Dim i As Integer
Select Case e.Item.ItemType
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

...the first two lines of code:
MyBase.OnItemCreated(e)
MyBase.Oni()

...say they're not members of "System.Web.UI.Page"

Any ideas?

Thanks!
 
sorry....MyBase.Oni() is a typo..I deleted this line.
 
Just tried this too, and ....

MyBase.OnItemCreated(e)

.....isn't a member of System.Web.UI.Page


Sub OnItemCreated(ByVal e As DataGridItemEventArgs)
MyBase.OnItemCreated(e)
Dim i As Integer
Select Case e.Item.ItemType
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
 
Okay...duh...it should be Me.OnItemCreated(e), but there's still no ddl created when the Edit link is clicked. Do I need to call something in the Datagrid's EditCommand handler?

Thanks for the help!
 
I think your onItemCreated is not being called. Did you wire it in your asp code like

<asp:datagrid onitemcreated = "myitemcreated">

I know thats not what I gave you at first, sorry, I'm doing it a bit different and forgot to take that into account.

 
cjburkha,

Here's the code-behind:
Sub MyItemCreated(ByVal e As DataGridItemEventArgs)
Me.MyItemCreated(e)
Dim i As Integer
Select Case e.Item.ItemType
Case ListItemType.EditItem
sCon1.Open()
Dim strDDL As String
strDDL = "SELECT Signature_Authority_Names.Authority+' | '+Signature_Authority_Names.ZNumber AS Expr1, "
strDDL &= "Signature_Authority_Names.Authority FROM Signature_Authority_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 = "Signature_Authority_Names"
ddlName.DataTextField = "Expr1"
ddlName.DataValueField = "Authority"
ddlName.DataBind()
sCon1.Close()
End Select

Here's the HTML:
<asp:datagrid id="DataGrid1" OnItemCreated="MyItemCreated"

When I run the code, I get:
Compiler Error Message: BC30408: Method 'Public Sub MyItemCreated(e As System.Web.UI.WebControls.DataGridItemEventArgs)' does not have the same signature as delegate 'Delegate Sub DataGridItemEventHandler(sender As Object, e As System.Web.UI.WebControls.DataGridItemEventArgs)'.

It looks like MyItemCreated needs a sender parameter too.
If I do this:
Sub MyItemCreated(ByVal Sender As Object, ByVal e As DataGridItemEventArgs)
Me.MyItemCreated(Sender, e)

..it hangs for minutes, then I just kill the app.
 
As luck would have it, I too need to now create a dynamic drop down list but my needs are a little different here. I just need to create a list of dates starting from this year and going out an additional 10 years. Here is what I have but I just get a blank drop down.

Code:
Function BuildYears()
		Dim ThisYear As Integer
		Dim X As Integer
		Dim opString As String
		ThisYear = CINT(Year(Date))
		For X = 0 to 10
			opString = opString & "<option label='" & (ThisYear + X) & "' value='" & (ThisYear + X) &"' \>"
		Next
		BuildYears = opString
	End Function




<select id=\"expYear\" name=\"expYear\" style=\"width:60px;\" size=\"1\">"
<script>
'GENERATE A YEAR DROP-DOWN 10 YEARS OUT FROM THIS YEAR:
BuildYears()
</script>
</select>

If I pop my function into a vbscript file it gives me the results I am looking for, so what am I doing wrong here?

I hope you find this post helpful.

Regards,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top