Add coded value to existing domain

2680
5
01-03-2011 01:15 PM
GregRieck
Occasional Contributor III
Hello Developers,

Is it possible to update a coded value domain when the schema has previously been opened and is in an edit session? I keep getting "An exclusive lock could not be acquired" error message. Isn't there a way to release locks, update the domain and return the user to their previous state? If not what does everyone else do for maintaining domains? I have several Type tables that I use as coded value domains. Do I need to write an external application to maintain a sync between the Type tables and the coded value domains? It seems like a catch 22 here. I need an edit session to create the new "Type" but then I must stop editing and release any database locks to update the domain for the new Type. Plus of course I'd like the new Type and it's domain to be immediately available for use.

Greg

        public static Boolean AddToCVD(IWorkspace ws, string domainName, string sValue, ref int cvdValue)
        {
            // Get the requested domain from the workspace.
            IWorkspaceDomains2 wsd2 = ws as IWorkspaceDomains2;
            IDomain domain = wsd2.get_DomainByName(domainName);
            ICodedValueDomain pcvd = domain as ICodedValueDomain;
            if (pcvd != null)
            {
                ISchemaLock schemaLock = pcvd as ISchemaLock;
                try
                {
                    // Attempt to acquire an exclusive schema lock.
                    schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);
                    // Add the code and use IWorkspaceDomains2.AlterDomain to store the change.
                    if (domain.FieldType == esriFieldType.esriFieldTypeString)
                        pcvd.AddCode(sValue, sValue);
                    else
                        pcvd.AddCode(pcvd.CodeCount, sValue);
                    wsd2.AlterDomain(domain);
                    return true;
                }
                catch (COMException comExc)
                {
                    RecordError(comExc,"An exclusive lock could not be acquired");
                }
                finally
                {
                    // Set the schema lock to a shared lock.
                    schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
                    cvdValue = pcvd.CodeCount - 1;// the count was incremented above, return the previous count to get the new value
                }
            }
            return false;
        }
0 Kudos
5 Replies
AlexanderGray
Occasional Contributor III
I think you have answered your own question here.  The domain being part of the schema, you cannot edit it while editing the data.  Domains should be a rather static thing, if it isn't perhaps a domain is not appropriate.  In all the cases I have been using domains, I set them when I created the schema.  in the development phase I had to make some changes, I did so by kicking all the developers out of the database and doing it.  I know some times people update them in production systems, usually in scheduled maintenance.  If you have type tables that you need to keep synchronized,  there is duplication of the data.  Also if these tables change so often perhaps you should not be using domains.  Maybe you need another way of validating the data.
0 Kudos
GregRieck
Occasional Contributor III
Alexander,

Thank you for responding to my post. Are coded value domains supposed to be that rigid? Mostly I am using them because they present in the attribute editor as drop down combo boxes. I understand the need to lock the schema for coded value domain changes however it would be nice if there were a way to make changes to coded value domains. If need be I suppose I can write an application that updates the coded value domains outside of ArcMap. Even when I stop the edit session an attempt to alter the domain I still get a schema lock error. It seems you can't even have the map document open that pertains to the database you are attempting to alter. I've also tried altering the schema prior to starting an edit session. I load the map document and attempt to alter the schema and still get the schema lock error.

But really no one else has a need to alter coded value domains and no one else has ever written any code to do that. I find that hard to believe. Also, I don't think coded value domains are only used for attribute integrity. There are several other benefits to using them besides integrity, faster editing and combo boxes are just a couple.

Thanks again for your thoughts and input.

Greg
0 Kudos
AlexanderGray
Occasional Contributor III
Ya the advantages in the attribute editing tools with coded values are pretty nice.  I wish ESRI had come up with a way to do it with look up tables as an alternative to domains, perhaps in a non-enforced way.  Of course if you start having domains that change frequently, you have to think of what happens to you data if you remove domain values, etc.  If you run your code as the first thing you do in your arcmap session (before any other custom code is run and before an edit session is started) do you get the same problem acquiring the lock?  It could be an object used in code that has not been explicitly released (ComReleaser Class.)
0 Kudos
GregRieck
Occasional Contributor III
Yes, I thought of the ComReleaser as well. I'm pretty good about enclosing all my cursors inside the ComReleaser. I have my own snippet that inserts the ComReleaser and Try, Catch and error trapping statements for new procedures. I do have an extension that runs and events that are started when ArcMap starts. But no cursors should be called when an edit session hasn't even been started yet.

What's even more frustrating is that you can manually alter the domain while in an edit session and have the changes immediately available. It's just that you can't, for what ever reason, programmatically update the domain for the user. I've even tried removing the schema lock portion and it still won't work.
0 Kudos
AlexanderGray
Occasional Contributor III
You might want to look at this thread in the old forums
http://forums.esri.com/Thread.asp?c=93&f=985&t=206951
0 Kudos