B
Brett
I have an application that contains two databound DropDownLists. Selecting
a department from the first ddl causes a DropDownList of machines to be
populated.
One of the users enters several new records back-to-back, and she reported
that she often gets the error, "'ddlMachine' has a SelectedValue which is
invalid because it does not exist in the list of items", after selecting a
value from ddlDepartment in insert mode. At that point, she must close
the browser and re-open it to prevent the error from occurring again.
Then, everything is fine, until she has saved a few more entries.
At first, I was unable to duplicate this. The next day, I was able to
duplicate it, and it happened every time I selected a value from
ddlDepartment. At that point, I got diverted by a phone call and did
not get back to it for about 45 minutes. When I returned, the application
worked fine again!
It seems as though something is not getting cleared properly from memory on
the web server. Restarting Internet Explorer or just being inactive for a
while seems to correct the problem. If this is the problem, is there some
way that I can force this to get cleared from memory?
I have included code below.
Thanks in advance!
Brett
ASPX:
<asp:FormView ID="FormView1" runat="server"
DataMember="DefaultView"
DataSourceID="WorkOrderSqlDataSource"
DataKeyNames="SSRID" CssClass="Panels"
OnItemUpdating="FormView1_ItemUpdating">
<InsertItemTemplate>
<asp:SqlDataSource ID="DeptSqlDataSource"
runat="server"
ConnectionString="<%$ConnectionStrings:SSR_ConnString %>"
SelectCommand="SELECT [DepartmentName] FROM
[Department] ORDER BY PositionInDropDownList"
DataSourceMode="DataReader">
</asp:SqlDataSource>
<asp:SqlDataSource ID="MachineSqlDataSource"
runat="server"
ConnectionString="<%$ConnectionStrings:SSR_ConnString %>"
SelectCommand="SELECT [MachineName], [MachineID]
FROM [Machine] WHERE ([Line] = @Line)"
DataSourceMode="DataReader">
<SelectParameters>
<asp:ControlParameter ControlID="ddlDepartment"
Name="Line"
PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<aspropDownList ID="ddlDepartment"
runat="server"
CssClass="DetailsViewItemStyle"
AutoPostBack="True" Width="13em"
AppendDataBoundItems="true"
DataSourceID="DeptSQLDataSource"
DataTextField="DepartmentName"
DataValueField="DepartmentName"
OnPreRender="ShowAdminFields"
SelectedValue='<%# Bind("DepartmentName") %>' >
</aspropDownList>
<aspropDownList ID="ddlMachine"
runat="server"
CssClass="DetailsViewItemStyle"
Width="17em"
DataSourceID="MachineSQLDataSource"
DataTextField="MachineName"
DataValueField="MachineName" >
<asp:ListItem></asp:ListItem>
</aspropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server"
ControlToValidate="ddlMachine"
cssclass="Validator"
Display="Dynamic"
ErrorMessage="<== Required field" />
ASPX.VB:
Protected Sub ddlDepartment_DataBound(ByVal sender As Object, ByVal e As
EventArgs)
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim frmV As FormView = CType(ddl.NamingContainer, FormView)
If Not frmV.DataItem Is Nothing Then
'Pull the department value from the databound item
Dim sDepartment As String = CType(frmV.DataItem,
DataRowView)("DepartmentName")
ddl.ClearSelection()
Dim li As ListItem = ddl.Items.FindByValue(sDepartment)
If Not li Is Nothing Then li.Selected = True
End If
'Since the machine selection is dependent on the department, we have
'to databind the machine list after changing the department
'selection.
ddl = CType(frmV.FindControl("ddlMachine"), DropDownList)
If Not ddl Is Nothing Then ddl.DataBind()
End Sub
Protected Sub ddlMachine_DataBound(ByVal sender As Object, ByVal e As
EventArgs)
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim frmV As FormView = CType(ddl.NamingContainer, FormView)
If Not frmV.DataItem Is Nothing Then
Dim sMachine As String = CType(frmV.DataItem,
DataRowView)("MachineName")
ddl.ClearSelection()
Dim li As ListItem = ddl.Items.FindByValue(sMachine)
If Not li Is Nothing Then li.Selected = True
End If
End Sub
Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.FormViewInsertEventArgs) Handles
FormView1.ItemInserting
Dim sDepartment As String = CType(CType(sender,
FormView).FindControl("ddlDepartment"), DropDownList).SelectedValue
e.Values("DepartmentName") = sDepartment
Dim sMachine As String = CType(CType(sender,
FormView).FindControl("ddlMachine"), DropDownList).SelectedValue
e.Values("MachineName") = sMachine
e.Cancel = False
End Sub
Protected Sub FormView1_ItemUpdating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles
FormView1.ItemUpdating
Dim sDepartment As String = CType(CType(sender,
FormView).FindControl("ddlDepartment"), DropDownList).SelectedValue
e.NewValues("DepartmentName") = sDepartment
Dim sMachine As String = CType(CType(sender,
FormView).FindControl("ddlMachine"), DropDownList).SelectedValue
e.NewValues("MachineName") = sMachine
e.Cancel = False
End Sub
a department from the first ddl causes a DropDownList of machines to be
populated.
One of the users enters several new records back-to-back, and she reported
that she often gets the error, "'ddlMachine' has a SelectedValue which is
invalid because it does not exist in the list of items", after selecting a
value from ddlDepartment in insert mode. At that point, she must close
the browser and re-open it to prevent the error from occurring again.
Then, everything is fine, until she has saved a few more entries.
At first, I was unable to duplicate this. The next day, I was able to
duplicate it, and it happened every time I selected a value from
ddlDepartment. At that point, I got diverted by a phone call and did
not get back to it for about 45 minutes. When I returned, the application
worked fine again!
It seems as though something is not getting cleared properly from memory on
the web server. Restarting Internet Explorer or just being inactive for a
while seems to correct the problem. If this is the problem, is there some
way that I can force this to get cleared from memory?
I have included code below.
Thanks in advance!
Brett
ASPX:
<asp:FormView ID="FormView1" runat="server"
DataMember="DefaultView"
DataSourceID="WorkOrderSqlDataSource"
DataKeyNames="SSRID" CssClass="Panels"
OnItemUpdating="FormView1_ItemUpdating">
<InsertItemTemplate>
<asp:SqlDataSource ID="DeptSqlDataSource"
runat="server"
ConnectionString="<%$ConnectionStrings:SSR_ConnString %>"
SelectCommand="SELECT [DepartmentName] FROM
[Department] ORDER BY PositionInDropDownList"
DataSourceMode="DataReader">
</asp:SqlDataSource>
<asp:SqlDataSource ID="MachineSqlDataSource"
runat="server"
ConnectionString="<%$ConnectionStrings:SSR_ConnString %>"
SelectCommand="SELECT [MachineName], [MachineID]
FROM [Machine] WHERE ([Line] = @Line)"
DataSourceMode="DataReader">
<SelectParameters>
<asp:ControlParameter ControlID="ddlDepartment"
Name="Line"
PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<aspropDownList ID="ddlDepartment"
runat="server"
CssClass="DetailsViewItemStyle"
AutoPostBack="True" Width="13em"
AppendDataBoundItems="true"
DataSourceID="DeptSQLDataSource"
DataTextField="DepartmentName"
DataValueField="DepartmentName"
OnPreRender="ShowAdminFields"
SelectedValue='<%# Bind("DepartmentName") %>' >
</aspropDownList>
<aspropDownList ID="ddlMachine"
runat="server"
CssClass="DetailsViewItemStyle"
Width="17em"
DataSourceID="MachineSQLDataSource"
DataTextField="MachineName"
DataValueField="MachineName" >
<asp:ListItem></asp:ListItem>
</aspropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server"
ControlToValidate="ddlMachine"
cssclass="Validator"
Display="Dynamic"
ErrorMessage="<== Required field" />
ASPX.VB:
Protected Sub ddlDepartment_DataBound(ByVal sender As Object, ByVal e As
EventArgs)
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim frmV As FormView = CType(ddl.NamingContainer, FormView)
If Not frmV.DataItem Is Nothing Then
'Pull the department value from the databound item
Dim sDepartment As String = CType(frmV.DataItem,
DataRowView)("DepartmentName")
ddl.ClearSelection()
Dim li As ListItem = ddl.Items.FindByValue(sDepartment)
If Not li Is Nothing Then li.Selected = True
End If
'Since the machine selection is dependent on the department, we have
'to databind the machine list after changing the department
'selection.
ddl = CType(frmV.FindControl("ddlMachine"), DropDownList)
If Not ddl Is Nothing Then ddl.DataBind()
End Sub
Protected Sub ddlMachine_DataBound(ByVal sender As Object, ByVal e As
EventArgs)
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim frmV As FormView = CType(ddl.NamingContainer, FormView)
If Not frmV.DataItem Is Nothing Then
Dim sMachine As String = CType(frmV.DataItem,
DataRowView)("MachineName")
ddl.ClearSelection()
Dim li As ListItem = ddl.Items.FindByValue(sMachine)
If Not li Is Nothing Then li.Selected = True
End If
End Sub
Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.FormViewInsertEventArgs) Handles
FormView1.ItemInserting
Dim sDepartment As String = CType(CType(sender,
FormView).FindControl("ddlDepartment"), DropDownList).SelectedValue
e.Values("DepartmentName") = sDepartment
Dim sMachine As String = CType(CType(sender,
FormView).FindControl("ddlMachine"), DropDownList).SelectedValue
e.Values("MachineName") = sMachine
e.Cancel = False
End Sub
Protected Sub FormView1_ItemUpdating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles
FormView1.ItemUpdating
Dim sDepartment As String = CType(CType(sender,
FormView).FindControl("ddlDepartment"), DropDownList).SelectedValue
e.NewValues("DepartmentName") = sDepartment
Dim sMachine As String = CType(CType(sender,
FormView).FindControl("ddlMachine"), DropDownList).SelectedValue
e.NewValues("MachineName") = sMachine
e.Cancel = False
End Sub