Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the below code behind. I am trying to set the selectedvalue of a dropdownlist inside a edit template of a listview. But I get a null reference error at
ddlWorkArea.DataSource = ds


VB
Private Sub ListView1_ItemEditing(sender As Object, e As System.Web.UI.WebControls.ListViewEditEventArgs) Handles ListView1.ItemEditing

    Dim i As Integer = e.NewEditIndex
    Dim strWorkArea As String
    Dim lstWorkArea As ListItem
    Dim txtWorkArea As Label = ListView1.Items(i).FindControl("WorkAreaLabel")
    Dim ddlWorkArea As DropDownList = ListView1.Items(i).FindControl("WorkAreaDDL")
    Dim Site As String
    Site = Me.TextBoxAuditSite.Text

    Dim strConn As String = ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString
    Dim objConn As New SqlConnection(strConn)
    Dim ds As New DataSet()
    Dim myda As New SqlDataAdapter("SELECT [WorkArea] FROM [zEdits]", objConn)
    myda.Fill(ds, "workArea")
    ddlWorkArea.DataSource = ds
    ddlWorkArea.DataValueField = "WorkArea"
    ddlWorkArea.DataBind()
    If txtWorkArea IsNot Nothing Then
        strWorkArea = txtWorkArea.Text
        lstWorkArea = ddlWorkArea.Items.FindByText(strWorkArea)
        If IsNothing(lstWorkArea) Then
            ddlWorkArea.Items.Insert(0, "")
        Else
            ddlWorkArea.Items.Insert(0, "")
            ddlWorkArea.SelectedValue = strWorkArea
        End If
    End If


End Sub
Posted

1 solution

myda.Fill(ds, "workArea") should probably be myda.Fill(ds, "zEdits").
 
Share this answer
 
Comments
Andy Morris 1-Jan-12 12:54pm    
I changed it to zEdits, but still doesn't work. I built a DropDownList outside of the ListView and it worked fine. It seems the issue is with the:
Dim ddlWorkArea As DropDownList = ListView1.Items(i).FindControl("WorkAreaDDL"

it like it doesn't see it, here is the asp.net part


<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID, WorkArea, SubWorkArea"
DataSourceID="AuditChecklistAnswerData" InsertItemPosition="LastItem" >
<edititemtemplate>
<tr style="background-color:#008A8C;color: #FFFFFF;">
<td style="width: 55px" rowspan="2">
<asp:Button ID="UpdateButton" runat="server" CommandName="Update"
Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Cancel" />
</td>
<td>
<asp:TextBox ID="WorkAreaTextBox" runat="server" Text='<%# Bind("WorkArea") %>'
width="100px" Enabled="True" BackColor="White" visible ="false" Font-Bold="true" />
<asp:DropDownList ID="WorkAreaDDL" runat="server" BackColor="#FFFFCC" AppendDataBoundItems="true"
DataSourceID="WorkAreaDDLSelect" width="200px" DataValueField="WorkArea" >



~~ rest of the listview ~~

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900