The application that I am working on is a Service Desk App. I have a form there that uses a DropDownList that contains employees names coming from the Active Directory. Any employee can make a request and save it.
Problem arises when an employee leaves the company and consequently his account is deleted from the Active Directory. When some other employee searches the DB to finds a relevant Service Ticket that could be of his use, when tries to open it an error is thrown that indicates that the name does not exist in the DropDownList items.
What I need is a solution so that the functionality will remain the same (be able to delete the Active Directory entry), but no error will be thrown.
I am using as indicated by the tags, ASP.NET with VB. Solutions with C# are also welcome.
Thank you in advance for your suggestions to my problem.
UPDATE:
I am adding some code so that it can be more clear.
ASPX: (This is huge, I am putting only the DropDownList in question)
<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1"
DataKeyNames="ITRequestId">
<EditItemTemplate>
<br />
<asp:LinkButton ID="LinkButton5" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" CssClass="InsertLink" />
<asp:LinkButton ID="LinkButton6" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" CssClass="CancelLink" />
........
<div id="user" style="float: left;">
<label>User:<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ErrorMessage="User" Display="Dynamic" ControlToValidate="DropDownList5" Text="*" ForeColor="#FF0000"></asp:RequiredFieldValidator></label><br />
<asp:DropDownList ID="DropDownList5" runat="server" SelectedValue='<%# Bind("ITRequestUserName") %>'>
<asp:ListItem Value=""></asp:ListItem>
<asp:ListItem Value="All">All</asp:ListItem>
<asp:ListItem Value="NA">N/A</asp:ListItem>
</asp:DropDownList>
</div>
.........
</EditItemTemplate>
CODE BEHIND vb: (This is huge, but I am putting the pageload event where the error is thrown)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Not IsPostBack) Then
If Request.UrlReferrer IsNot Nothing Then
ViewState("RefUrl") = Request.UrlReferrer.ToString()
End If
End If
'Handles the mode of the FormView according to the request
If Request.QueryString.Get("ITRequestId") IsNot Nothing Then
FormView1.DefaultMode = FormViewMode.ReadOnly
Dim tName As String = DirectCast(FormView1.Row.FindControl("DropDownList5"), DropDownList).SelectedValue
Dim temptype As String = DirectCast(FormView1.Row.FindControl("DropDownList1"), DropDownList).SelectedItem.Text
Dim myAD As New tActiveDirectory(LDAPpath)
Dim lName As String = HttpContext.Current.User.Identity.Name.ToString()
Dim sDisplayName As String = myAD.GetUserInfo(lName, "displayName")
Dim cName As String = DirectCast(FormView1.Row.FindControl("Label5"), Label).Text
If Not (User.IsInRole("Developers") Or User.IsInRole("Administrators") Or tName = sDisplayName Or cName = sDisplayName) Then
If (temptype = "Access rights") Then
Response.Redirect("../IT/ITAccessDenied.aspx")
End If
End If
If Not (User.IsInRole("Developers") Or User.IsInRole("Administrators") Or cName = sDisplayName) Then
If (temptype = "Account") Then
Response.Redirect("../IT/ITAccessDenied.aspx")
End If
End If
If Not (User.IsInRole("LocalIT")) Then
If (temptype = "Internal IT Task") Then
Response.Redirect("../IT/ITAccessDenied.aspx")
End If
End If
Else
FormView1.DefaultMode = FormViewMode.Insert
Dim tempstatus As DropDownList = DirectCast(FormView1.Row.FindControl("DropDownList2"), DropDownList)
tempstatus.SelectedIndex = 3
End If
End Sub
The error is thrown at Line 11 of the code when declaring the tName variable. And this is happening because the UserName that is in the database and is to be bounded by the DropDownList has been deleted from the ActiveDirectory and thus it does not exist in the values of the List.
Here is the code that populates the DropDownList:
Protected Sub FormView1_ItemCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.ItemCreated
Dim d1 As DropDownList
Dim d2 As DropDownList
Dim myAD As New tActiveDirectory(LDAPpath)
Dim users As New ArrayList()
users = myAD.GetAllUsersInfo()
d1 = DirectCast(FormView1.Row.FindControl("DropDownList5"), DropDownList)
d2 = DirectCast(FormView1.Row.FindControl("DropDownList7"), DropDownList)
d1.DataSource = users
d2.DataSource = users
End Sub
Public Function GetAllUsersInfo() As ArrayList
Dim Users As New ArrayList()
Dim myDirectory As New DirectoryEntry(sPath)
Dim mySearcher As New DirectorySearcher(myDirectory)
Dim fullName As String
mySearcher.Filter = "(&(objectCategory=person)(objectClass=user))"
mySearcher.PropertiesToLoad.Add("sn")
mySearcher.PropertiesToLoad.Add("displayName")
mySearcher.Sort.PropertyName = "sn"
mySearcher.Sort.Direction = SortDirection.Ascending
Users.Add("")
Users.Add("N/A")
Users.Add("All")
For Each result As DirectoryServices.SearchResult In mySearcher.FindAll
fullName = result.Properties("displayName")(0).ToString
Users.Add(fullName)
Next
Return Users
End Function
Any help with this will be appreciated. Thank you.
Your question does not have much detail regarding code. But it sounds like your app is trying to select a value in the DDL that is not there (anymore). Just check before selecting like this: