Error filling DropDownList on ListView

D

DavidC

I have a ListView that is used to edit or add records to an SQL table. In
the ItemDatabound event I am populating the DropDown selections from a class
function. The same class function is used for both the edit and the insert
templates. The edit works great but the insert gives me the error below.

An unhandled exception occurred:
Message: 'ddlServiceID' has a SelectedValue which is invalid because it does
not exist in the list of items.
Parameter name: value

Stack Trace:
at System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable
dataSource)
at System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e)
at System.Web.UI.WebControls.ListControl.PerformSelect()
at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
at payroll_TimesheetEntry.lvReimbursements_ItemDataBound(Object sender,
ListViewItemEventArgs e) in
http://server/MCFICore/payroll/TimesheetEntry.aspx.vb:line 148
at
System.Web.UI.WebControls.ListView.OnItemDataBound(ListViewItemEventArgs e)
at
System.Web.UI.WebControls.ListView.CreateItemsWithoutGroups(ListViewPagedDataSource
dataSource, Boolean dataBinding, InsertItemPosition insertPosition, ArrayList
keyArray)
at System.Web.UI.WebControls.ListView.CreateChildControls(IEnumerable
dataSource, Boolean dataBinding)
at System.Web.UI.WebControls.ListView.PerformDataBinding(IEnumerable data)
at
System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)
at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments
arguments, DataSourceViewSelectCallback callback)
at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
at System.Web.UI.WebControls.ListView.PerformSelect()
at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()
at System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e)
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

The ItemDatabound code is below. I am doing this same thing in another page
and it works fine. I have stared at it for a while and cannot figure out why
the error. Also, if I comment out the ddlServices Databind() then I get no
error. The error is on the ddlServices.DataBind() line. Can anyone see what
is wrong with this? Thanks.

Protected Sub lvReimbursements_ItemDataBound(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles
lvReimbursements.ItemDataBound
Dim ddl As DropDownList
Dim intProgramID As Int32 =
Convert.ToInt32(Request.QueryString("pgm"))
Dim intServiceID As Int32 = 0
Dim intBranch As Int16 = Convert.ToInt16(ddlBranch.SelectedValue)

'Get the item and row objects.
Dim dataItem As ListViewDataItem = CType(e.Item, ListViewDataItem)
Dim rowView As DataRowView = CType(dataItem.DataItem, DataRowView)

'Verify there is an item being edited.
If lvReimbursements.EditIndex >= 0 Then

' Check for an item in edit mode.
If dataItem.DisplayIndex = lvReimbursements.EditIndex Then

intServiceID = Convert.ToInt32(rowView("ServiceID"))
ddl = CType(e.Item.FindControl("ddlServiceID"), DropDownList)
ddl.DataSource =
LookupClass.GetServicesByProgram(intProgramID, True)
ddl.DataValueField = "ServiceID"
ddl.DataTextField = "ServiceName"
ddl.DataBind()
ddl.Items.Insert(0, New ListItem("", "0"))
ddl.SelectedValue = intServiceID.ToString

' Retrieve the PeopleLinkID value for the current item.
'Dim intWorkerLinkID As Int32 =
Convert.ToInt32(rowView("WorkerLinkID"))

End If
Else
If lvReimbursements.InsertItemPosition =
InsertItemPosition.FirstItem Then
If e.Item.ItemType = ListViewItemType.DataItem Then
Dim ddlServices As DropDownList =
lvReimbursements.InsertItem.FindControl("ddlServiceID")
ddlServices.DataSource =
LookupClass.GetServicesByProgram(intProgramID, True)
ddlServices.DataValueField = "ServiceID"
ddlServices.DataTextField = "ServiceName"
ddlServices.DataBind()
ddlServices.Items.Insert(0, New ListItem("", ""))
ddlServices.SelectedValue = ""

Dim tb As TextBox =
lvReimbursements.InsertItem.FindControl("txtWeek2Ending")
tb.Text = txtDefaultPeriodEnding.Text
End If
End If
End If
End Sub
 
G

Guest

I have a ListView that is used to edit or add records to an SQL table.  In
the ItemDatabound event I am populating the DropDown selections from a class
function.  The same class function is used for both the edit and the insert
templates. The edit works great but the insert gives me the error below.

An unhandled exception occurred:
Message: 'ddlServiceID' has a SelectedValue which is invalid because it does
not exist in the list of items.
Parameter name: value

 Stack Trace:
   at System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable
dataSource)
   at System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e)
   at System.Web.UI.WebControls.ListControl.PerformSelect()
   at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
   at payroll_TimesheetEntry.lvReimbursements_ItemDataBound(Object sender,
ListViewItemEventArgs e) inhttp://server/MCFICore/payroll/TimesheetEntry.aspx.vb:line148
   at
System.Web.UI.WebControls.ListView.OnItemDataBound(ListViewItemEventArgs e)
   at
System.Web.UI.WebControls.ListView.CreateItemsWithoutGroups(ListViewPagedDa­taSource
dataSource, Boolean dataBinding, InsertItemPosition insertPosition, ArrayList
keyArray)
   at System.Web.UI.WebControls.ListView.CreateChildControls(IEnumerable
dataSource, Boolean dataBinding)
   at System.Web.UI.WebControls.ListView.PerformDataBinding(IEnumerable data)
   at
System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(I­Enumerable data)
   at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments
arguments, DataSourceViewSelectCallback callback)
   at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
   at System.Web.UI.WebControls.ListView.PerformSelect()
   at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
   at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()
   at System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e)
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

The ItemDatabound code is below.  I am doing this same thing in another page
and it works fine. I have stared at it for a while and cannot figure out why
the error.  Also, if I comment out the ddlServices Databind() then I get no
error.  The error is on the ddlServices.DataBind() line.  Can anyone see what
is wrong with this?  Thanks.

    Protected Sub lvReimbursements_ItemDataBound(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles
lvReimbursements.ItemDataBound
        Dim ddl As DropDownList
        Dim intProgramID As Int32 =
Convert.ToInt32(Request.QueryString("pgm"))
        Dim intServiceID As Int32 = 0
        Dim intBranch As Int16 = Convert.ToInt16(ddlBranch.SelectedValue)

        'Get the item and row objects.
        Dim dataItem As ListViewDataItem = CType(e.Item, ListViewDataItem)
        Dim rowView As DataRowView = CType(dataItem.DataItem, DataRowView)

        'Verify there is an item being edited.
        If lvReimbursements.EditIndex >= 0 Then

            ' Check for an item in edit mode.
            If dataItem.DisplayIndex = lvReimbursements.EditIndex Then

                intServiceID = Convert.ToInt32(rowView("ServiceID"))
                ddl = CType(e.Item.FindControl("ddlServiceID"), DropDownList)
                ddl.DataSource =
LookupClass.GetServicesByProgram(intProgramID, True)
                ddl.DataValueField = "ServiceID"
                ddl.DataTextField = "ServiceName"
                ddl.DataBind()
                ddl.Items.Insert(0, New ListItem("", "0"))
                ddl.SelectedValue = intServiceID.ToString

                ' Retrieve the PeopleLinkID value for the current item.
                'Dim intWorkerLinkID As Int32 =
Convert.ToInt32(rowView("WorkerLinkID"))

            End If
        Else
            If lvReimbursements.InsertItemPosition =
InsertItemPosition.FirstItem Then
                If e.Item.ItemType = ListViewItemType.DataItem Then
                    Dim ddlServices As DropDownList =
lvReimbursements.InsertItem.FindControl("ddlServiceID")
                    ddlServices.DataSource =
LookupClass.GetServicesByProgram(intProgramID, True)
                    ddlServices.DataValueField = "ServiceID"
                    ddlServices.DataTextField = "ServiceName"
                    ddlServices.DataBind()
                    ddlServices.Items.Insert(0, New ListItem("", ""))
                    ddlServices.SelectedValue = ""

                    Dim tb As TextBox =
lvReimbursements.InsertItem.FindControl("txtWeek2Ending")
                    tb.Text = txtDefaultPeriodEnding.Text
                End If
            End If
        End If
    End Sub

Which line is #148?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top