Exception: Operation must be an updatable query when you use an local Access File (.mdb)

SOLUTION
The Access File .mdb is readonly, from sourcecontrol

Change it on startup with the following Helper: RemoveReadOnlyFlagAccessFile()


Code Snippet to remove ReadOnly

public static string RemoveReadOnlyFlagAccessFile()
        {
            string returnValue = "";

            string connString = GetAccessConnectionString();
            if (string.IsNullOrEmpty(connString))
            {
                returnValue = "ConnectionString not found";
            }
            else
            {
                //FORMAT
                //connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\AccessFile\oracle-reverse-engineered-modified-manually.mdb;User Id=admin;Password=;"
                if (connString.ToUpper().Contains("OLEDB") == false)
                {
                    returnValue = "ConnectionString is not for MsAccess. doesnt contain OLEDB. ConnString: " + connString;
                }
                else
                {
                    int indexStart = connString.IndexOf("Data Source=");
                    if (indexStart <= 0)
                    {
                        returnValue = "ConnectionString is not correct. doesnt contain Data Source=. ConnString: " + connString;
                    }
                    else
                    {

                        string tempString = connString.Substring(indexStart + 12);
                        int indexEnd = tempString.IndexOf(".mdb");
                        if (indexEnd <= 0)
                        {
                            returnValue = "ConnectionString is not correct. doesnt contain .mdb. TempConnString" + tempString;
                        }
                        else
                        {
                            string filePath = tempString.Substring(0, indexEnd + 4);
                            string fullPath = Path.GetFullPath(filePath);

                            if (File.Exists(fullPath) == false)
                            {
                                returnValue = "File doesnt exist: " + fullPath;
                            }
                            else
                            {
                                returnValue = "File " + fullPath + " is: " + File.GetAttributes(fullPath).ToString() + "\n";

                                try
                                {
                                    File.SetAttributes(fullPath, FileAttributes.Normal);
                                    returnValue += "File " + fullPath + " NOW is: " + File.GetAttributes(fullPath).ToString() + "\n";
                                }
                                // TODO catch explicit exception
                                catch (Exception ex)
                                {
                                    Logger.Write("EXCEPTION in File.SetAttributes:   " + ex.ToString());
                                    returnValue += "Exception on File.SetAttributes";
                                }
                                
                            }


                        }
                    }
                }
            }

            //Logger.Write("FileUtils.RemoveReadOnlyFlagAccessFile  returnValue: \n" + returnValue);
            return returnValue;

        }


        private static string GetAccessConnectionString()
        {
            

            if (System.Configuration.ConfigurationManager.ConnectionStrings.Count == 0)
            {
                throw new ApplicationException("Set ConnectionString in .config file!");
            }
            if (System.Configuration.ConfigurationManager.ConnectionStrings.Count == 1)
            {
                return System.Configuration.ConfigurationManager.ConnectionStrings[0].ConnectionString;
            }
            else
            {
                // try "MasterMSAccess", then empty, then firstone
                if (System.Configuration.ConfigurationManager.ConnectionStrings["MasterMSAccess"] != null)
                {
                    return System.Configuration.ConfigurationManager.ConnectionStrings["MasterMSAccess"].ConnectionString;
                }
                else if (System.Configuration.ConfigurationManager.ConnectionStrings[""] != null)
                {
                    return System.Configuration.ConfigurationManager.ConnectionStrings[""].ConnectionString;
                }
                else
                {
                    return System.Configuration.ConfigurationManager.ConnectionStrings[0].ConnectionString;
                }

            }
        }

No comments:

Post a Comment

Latest Posts

Popular Posts