Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7511357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:31:23+00:00 2026-05-29T23:31:23+00:00

I have this weird issue. I am trying to create a service that synchs

  • 0

I have this weird issue. I am trying to create a service that synchs the users in database to users in Email groups in Active Directory(AD). So if A, B, C are in database and the active directory email group has A & E, I will synch the AD. So I will add ‘E’ and remove ‘B’ & ‘C’.

To get users from database I execute SQL calls. And after that if I try to add the user I get UserPrincipal.FindByIdentity as null. Here is a test code I have put up.

        int tries = 0;
        List<string> dbUsers = new List<string>();
        dbUsers.Add("12345");//test user id's
        dbUsers.Add("67891");

        int k = 0;
        for (k = 0; k < dbUsers.Count; k++)
        {
            AddUserToGroup(tries, dbUsers[k]);
        }

     public void AddUserToGroup(int tries, string userid)
     {
        try
        {
            PrincipalContext ctx = new PrincipalContext(ContextType.Domain,      domainName, Container, userName, Password);

            Principal user = Principal.FindByIdentity(ctx, userid); 
            string groupName = "TestGroup";

            using (ctx)
            {
                GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, groupName);
                if (!user.IsMemberOf(group))
                {
                    group.Members.Add(ctx, IdentityType.UserPrincipalName, user.UserPrincipalName);
                    group.Save();
                }
            }
        }
        catch (AppDomainUnloadedException ex)
        {
            if (tries > 5)
            {
                throw;
            }
            tries += 1;
            Thread.Sleep(1000);
            AddUserToGroup(tries, userid);
        }
    }

This piece works perfectly and I get the ‘user’ object correctly and I can successfully add it to AD email group. Problem is when I have SQL call to get userId’s from Database instead of hard coding.

So the code that is problematic is:

      List<string> dbUsers = GetActiveMembersFromDB('id')
      public List<string> GetActiveMembersFromDB(string practiceGroupId)
      {
        List<string> outpt = new List<string>();
        string SQLstring = string.Empty;

        string SQL_CONNECTION_STRING = "connectionString"; 
        SQLstring = "SELECT * from dbo.test";//whatever query

        using (SqlConnection cn = new SqlConnection(SQL_CONNECTION_STRING))
        {
            cn.Open();
            try
            {
                SqlCommand cm = new SqlCommand(SQLstring, cn);

                using (SqlDataReader dr = cm.ExecuteReader())
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            outpt.Add(dr["userId"].ToString());
                        }
                        return outpt;
                    }
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (cn != null)
                {
                    SqlConnection.ClearPool(cn);
                    cn.Close();
                }
            }
        }
        return outpt;
    }

So after fetching users from DB, if I call:

        int k = 0;
        for (k = 0; k < dbUsers.Count; k++)
        {
            AddUserToGroup(tries, dbUsers[k]);
        }

My ‘user’ is coming back as null.

Has anyone else encountered the same problem. What’s wrong with DB calls and AD calls together. I mean, I close all connections in my db calls and then try to access the Active Directory(AD).

Any help is appreciated.

Thanks,
Deeksha

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:31:25+00:00Added an answer on May 29, 2026 at 11:31 pm

    There should be no problem mixing AD and SQL. Your only coupling is List<string>. If the strings work in one case and the same string don’t work in another, it shouldn’t have anything to do with how you got the strings, but something different in the environment or on the thread, like the security context or similar.

    I think you need to distill it down to the smallest test case which shows the problem.

    I notice that your username and password are defined outside of the code given – have you checked and made sure you aren’t overwriting them?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is pretty weird. I have my Profiler open and it obviously shows that
This one is weird, I have a page that consists of a html table
This is a really weird problem that I have been having. When I download
I have this weird crash that only happens when running the app on the
I seem to have some weird issue going on that I am sure will
I have a weird issue with my CSS. Really can't figure this one out.
Having a weird issue. I'm new to Macs and have a windows VM that
Got this weird issue with Using jQuery 1.6.0 issue. I am trying to append
I have a weird issue using polymorphism. I have a base class that implements
I have a weird issue that I am being asked to fix but alas

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.