casting type for objectGuid

Discussion in 'ASP .Net Security' started by Guest, May 27, 2005.

  1. Guest

    Guest Guest

    Hi
    I am trying to put a user's objectGuid as the primary key onto a datatable
    and be used as the unique key on editing / updating. But I seem to get
    casting error with the following statement
    dt.Columns.Add(new DataColumn("object_Guid", typeof(string)));

    Any idea?

    TIA

    --
     
    Guest, May 27, 2005
    #1
    1. Advertising

  2. In AD/S.DS, objectGUID is returned from a DirectoryEntry or SearchResult as
    a byte array. You would want to cast that to a byte array, then pass that
    byte array to the constructor on a Guid structure. That will give you a
    valid .NET Guid.

    I can't remember the exact syntax to pass it into SQL to get it set up as a
    UniqueIdentifier column (which is the correct SQL type for a GUID), but as I
    recall, you pass it in your query as a String. Call the ToString method on
    the Guid to get this.

    HTH,

    Joe K.

    <dl> wrote in message news:...
    > Hi
    > I am trying to put a user's objectGuid as the primary key onto a datatable
    > and be used as the unique key on editing / updating. But I seem to get
    > casting error with the following statement
    > dt.Columns.Add(new DataColumn("object_Guid", typeof(string)));
    >
    > Any idea?
    >
    > TIA
    >
    > --
    >
    >
    >
     
    Joe Kaplan \(MVP - ADSI\), May 27, 2005
    #2
    1. Advertising

  3. Guest

    Guest Guest

    Hi Joe
    The problem with datatable is whenever I try to assign with the following
    statement I get a casting error
    dr = (string)resEnt.Properties["objectGuid"][0];

    I saw an example that can write NativeGuid directly from the returned
    directoryentry, as below, does it mean I instantiate another directoryEntry
    instance and simply assign this NativeGuid to a string variable?

    TIA

    String myADSPath =
    "LDAP://onecity/CN=Users,DC=onecity,DC=corp,DC=fabrikam,DC=com";
    DirectoryEntry myDirectoryEntry=new
    DirectoryEntry(myADSPath,UserName,SecurelyStoredPassword);

    // Display the Guid and NativeGuid.
    Console.WriteLine("The GUID of the ADS object:"+
    myDirectoryEntry.Guid);
    Console.WriteLine("The Native GUID of the ADS"+
    "object:"+myDirectoryEntry.NativeGuid);


    "Joe Kaplan (MVP - ADSI)" <> wrote
    in message news:...
    > In AD/S.DS, objectGUID is returned from a DirectoryEntry or SearchResult

    as
    > a byte array. You would want to cast that to a byte array, then pass that
    > byte array to the constructor on a Guid structure. That will give you a
    > valid .NET Guid.
    >
    > I can't remember the exact syntax to pass it into SQL to get it set up as

    a
    > UniqueIdentifier column (which is the correct SQL type for a GUID), but as

    I
    > recall, you pass it in your query as a String. Call the ToString method

    on
    > the Guid to get this.
    >
    > HTH,
    >
    > Joe K.
    >
    > <dl> wrote in message news:...
    > > Hi
    > > I am trying to put a user's objectGuid as the primary key onto a

    datatable
    > > and be used as the unique key on editing / updating. But I seem to get
    > > casting error with the following statement
    > > dt.Columns.Add(new DataColumn("object_Guid", typeof(string)));
    > >
    > > Any idea?
    > >
    > > TIA
    > >
    > > --
    > >
    > >
    > >

    >
    >
     
    Guest, May 28, 2005
    #3
  4. That's not what I said to do though. It should go like this:

    Guid objectGuid = new Guid((byte[]) resEnt.Properties["objectGUID"][0]);
    dr = objectGuid.ToString("B");

    ToString("B") will give you the GUID in "COM" format with { }. You can also
    do ToString("D") if you don't want the {}.

    I'd suggest not using the NativeGuid property for this. It will give you
    the objectGUID as an octet string in binary order, but that is likely to
    confuse SQL server which is likely to expect it in string format.

    You can use the Guid property if you want though. It works fine. The nice
    thing about the syntax above is that it works equally well with
    DirectoryEntry and SearchResult objects.

    HTH,

    Joe K.

    <dl> wrote in message news:%...
    > Hi Joe
    > The problem with datatable is whenever I try to assign with the following
    > statement I get a casting error
    > dr = (string)resEnt.Properties["objectGuid"][0];
    >
    > I saw an example that can write NativeGuid directly from the returned
    > directoryentry, as below, does it mean I instantiate another
    > directoryEntry
    > instance and simply assign this NativeGuid to a string variable?
    >
    > TIA
    >
    > String myADSPath =
    > "LDAP://onecity/CN=Users,DC=onecity,DC=corp,DC=fabrikam,DC=com";
    > DirectoryEntry myDirectoryEntry=new
    > DirectoryEntry(myADSPath,UserName,SecurelyStoredPassword);
    >
    > // Display the Guid and NativeGuid.
    > Console.WriteLine("The GUID of the ADS object:"+
    > myDirectoryEntry.Guid);
    > Console.WriteLine("The Native GUID of the ADS"+
    > "object:"+myDirectoryEntry.NativeGuid);
    >
    >
    > "Joe Kaplan (MVP - ADSI)" <> wrote
    > in message news:...
    >> In AD/S.DS, objectGUID is returned from a DirectoryEntry or SearchResult

    > as
    >> a byte array. You would want to cast that to a byte array, then pass
    >> that
    >> byte array to the constructor on a Guid structure. That will give you a
    >> valid .NET Guid.
    >>
    >> I can't remember the exact syntax to pass it into SQL to get it set up as

    > a
    >> UniqueIdentifier column (which is the correct SQL type for a GUID), but
    >> as

    > I
    >> recall, you pass it in your query as a String. Call the ToString method

    > on
    >> the Guid to get this.
    >>
    >> HTH,
    >>
    >> Joe K.
    >>
    >> <dl> wrote in message news:...
    >> > Hi
    >> > I am trying to put a user's objectGuid as the primary key onto a

    > datatable
    >> > and be used as the unique key on editing / updating. But I seem to get
    >> > casting error with the following statement
    >> > dt.Columns.Add(new DataColumn("object_Guid", typeof(string)));
    >> >
    >> > Any idea?
    >> >
    >> > TIA
    >> >
    >> > --
    >> >
    >> >
    >> >

    >>
    >>

    >
    >
     
    Joe Kaplan \(MVP - ADSI\), May 28, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Unlv?=

    How to filter Active Directory using objectGUID ?

    =?Utf-8?B?Unlv?=, Oct 19, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    3,427
    =?Utf-8?B?Unlv?=
    Oct 19, 2005
  2. kevin
    Replies:
    11
    Views:
    5,816
    Andrew McDonagh
    Jan 8, 2005
  3. heyo
    Replies:
    3
    Views:
    939
    Dan Pop
    Apr 1, 2004
  4. pete
    Replies:
    4
    Views:
    809
    Dan Pop
    Apr 2, 2004
  5. Wally Barnes
    Replies:
    3
    Views:
    533
    Wally Barnes
    Nov 20, 2008
Loading...

Share This Page