Tag Archives: VB.NET

VB.NET Code Snippets: Updating a data source using an event

Filed under ASP.NET, Microsoft

You may wish to update a data source using an event.

'You need to use FindControl to access control on a  FormView:
Dim FirstName As TextBox = FormView1.FindControl("FirstNameTextBox")
Dim LastName As TextBox = FormView1.FindControl("LastNameTextBox")
Dim Phone As TextBox = FormView1.FindControl("PhoneTextBox")
Dim Email As TextBox = FormView1.FindControl("EmailTextBox")
SqlDataSource1.InsertParameters("FirstName").DefaultValue = FirstName
SqlDataSource1.InsertParameters("LastName").DefaultValue = LastName
SqlDataSource1.InsertParameters("Phone").DefaultValue = Phone
SqlDataSource1.InsertParameters("Email").DefaultValue = Email

And, this is the code for Update:

SqlDataSource1.UpdateParameters("FirstName").DefaultValue = FirstName
SqlDataSource1.UpdateParameters("LastName").DefaultValue = LastName
SqlDataSource1.UpdateParameters("Phone").DefaultValue = Phone
SqlDataSource1.UpdateParameters("Email").DefaultValue = Email
SqlDataSource1. Update()

VB.NET: Retrieving Active Directory account properties

Filed under ASP.NET, Microsoft

The .NET Framework includes an API called System.DirectoryServices. The sample VB.NET code below shows how to retrieve user account properties from the Active Directory.

In order to use System.DirectoryServices classes, you will need to first add a reference to System.DirectoryServices.

Imports System.DirectoryServices is required at the very top of your VB.NET code.

Function GetUserProperties(ByVal GetUser, ByVal Result)
        Dim objSearch As New DirectorySearcher
        objSearch.SearchRoot = New DirectoryEntry("LDAP://dc=domain,dc=co,dc=uk")
        objSearch.Filter = GetUser
        'cn = Full Name, givenName = First name, initials = Middle names, sn = Surname, mail = email address
        objSearch.PropertiesToLoad.AddRange(New String() {"cn", "giveName", "initials", "sn", "department", "mail", "Manager"})
        Dim objResult As SearchResult
        objResult = objSearch.FindOne()
        GetUserProperties = (objResult.Properties(Result)(0))
End Function

Replace dc=domain, dc=co, dc=uk with your AD settings.