Unable to save MapDocument after adding layers

2246
5
07-15-2010 06:52 AM
BogdanVamanu
New Contributor
Hi

I've been struggling with this for days now and I've no ideea why it doesn't work...
The story is short: I've an ASP.NET application and I wanna create a new mxd file, load some shape layers into and then save it for further referencing by a MapService.

Everything works great but saving the MapDocument. I get:

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
when trying to use Save

or

"A lock violation has occurred. (Exception from HRESULT: 0x80030021 (STG_E_LOCKVIOLATION))" when using SaveAs with the same filename

or

"Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))" when saving as different name.

I use ArcGIS 9.3 SP1 and Visual Studio 2008.

The code goes like this:

'===========================

        'create the map document

        Dim pMapDoc As IMapDocument
        pMapDoc = New MapDocument
        pMapDoc.New(fName)
        pMapDoc.Save()
        pMapDoc.Close()

        'change the name of the data layer
        pMapDoc = New MapDocument
        pMapDoc.Open(fName)
        Dim pMap As IMap = pMapDoc.Map(0)
        pMap.Name = "Layers"
        pMapDoc.Save()
        pMapDoc.Close()

        'add the previously loaded layers (NL and NLPoint)
        If pMap.LayerCount = 0 Then cMap.SpatialReference = RE.SpatialReference
        pMap.AddLayer(NL)
        pMap.AddLayer(NLPoint)

        'if I break here the map has the layers and stuff... so, it's ok

        'try to refresh (save) the mxd
        pMapDoc.Open(fName)
        pMapDoc.SetActiveView(pMapDoc.Map(0))      'do I need it?..
        pMapDoc.ReplaceContents(pMap)

        pMapDoc.Save(False, True)

        'or
        'Dim fname1 As String = "E:/AUTOMXD/my1.mxd"
        'pMapDoc.SaveAs(fname1, False, True)
        'End If
       
        pMapDoc.Close()
'===========================


Just some other remarks...

1. I've granted access to the destination folder to all the possible users (SOM, SOC, WEBSERVICE, ASPNET,...)
2. WEIRD!!! 'Save' method works until adding the layers (e.g. changing the name of the group layer)
3. WEIRD!!! even thou 'Access denied', the mxd is created on file (half the size of an empty one)!!! ArcMAP crashes when trying to open it.
4. I've also tried to open an already created mxd file and save it. Same result.
5.....

Please, any of you have any idea?..
Thanks
0 Kudos
5 Replies
JohnMacdonald
New Contributor
Just a thought...

Have you checked out whether the Map Service is creating a lock on the MXD?

Am just investigating how to to lock a MXD through ArcObjects.

In my vb.Net app I

  • open a MXD

  • change the data sources for a few layers

  • then save or saves as


During this if I open the MXD in ArcMap make changes and save ArcMap is quite happy, however if I try and save changes through my vb.net app while the document I am trying to save to is open in ArcMap then I get similar error as you.

The arcObjects IMapDocument.Open statement doesn't appear to prevent access by ArcMap & maybe others.

Regards

John
0 Kudos
SuyashMate
New Contributor
Hi John

I am trying to open stored shapefiles in a web form (.aspx) in my .net application.From some inputs from the forum I have created a MXD (map document) for all the individual shape files.Can you please tell me as to how the MXD be opened in a web form(.aspx)

Thanks
0 Kudos
RodrigoSalvador
New Contributor
Hi everyone

I would open a new topic, but it might be interesting for all to see that this issue happens a lot of times.

My code looks like this:
this.pMapDocument.Open(sPath, "");

for (i = 0; i < this.pMapDocument.MapCount; i++)
{
    this.pMap = this.pMapDocument.get_Map(i);
    this.pMapDocument.ActiveView.Activate(i);
    //several commands to change layer properties here
}
          this.pMapDocument.ReplaceContents((IMxdContents)pMap);
          //IObjectCopy lip_ObjCopy = new ObjectCopyClass();
          //pMap = (IMap)lip_ObjCopy.Copy(pMapDocument.get_Map(0));
          //lip_ObjCopy = null;
          this.pMapDocument.Save(pMapDocument.UsesRelativePaths, false);
          this.pMapDocument.Close();

(The lines commented at ending I found in research, bur they made no difference)

So, when I attempt to save, the code generates a STR_E_LOCKVIOLATION exception, even the .mxd is not ReadOnly. The very annoing thing is that I've saved the changes once, yesterday (it worked after I added .Close() in the ending of the code), and this error starts happening besides I did not changed the code. I made tests for many .mxd files. I'm using ArcInfo license for 9.3 version.

Does anyone know what's going on?

Edit: The method SaveAs also don't work using the same string (referenced by sPath in my code) as reference. But works fine on creating another file. I still waiting for some light.

Many thanks

Rodrigo Salvador
0 Kudos
RodrigoSalvador
New Contributor
Hi

My issue is resolved, and I must post the answer to that. I've pasted here the relevant part of the code, but I did not say that pMapDocument.Save() was inside an if clause. So, the first call of a map (which occurs while the form is loading) was never close.

So, if you want to save some information, you must do it and then close de document. And also be sure that all other documents are close at this point. That's how I make it work.

Good job for all

Rodrigo Salvador
0 Kudos
bernabasberhanu
New Contributor
A) Creating a new Table
how to Write a Macro a Macro code in vb that creates the �??Parcel�?� table (a new table) with the associated fields, load the table to the current data frame and add the required records, the field like
P_ID (Long)
L_Use_Code (Long)
L_Use_Name (string)
Owner_ID (Long)

B)Creating a new feature class
how to Write a Macro code in vb that creates a new shape file feature class called �??Parcel�?� and adds it to the map window

c)how to Write a macro code in vbthat Relate table to a layer

d) how to Write a macro that shows a feature cursor operation to display the values of a given field for each row in a feature layer table.
0 Kudos