Exception When Registering a Database
wkhazzard
Posts: 20
I am getting an ArgumentOutOfRangeException when I register a database to create a snapshot. The code looks like this:
And here's the exception:
System.ArgumentOutOfRangeException was unhandled
Message=Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Source=mscorlib
ParamName=index
StackTrace:
at System.Collections.BitArray.Set(Int32 index, Boolean value)
at System.Collections.BitArray.set_Item(Int32 index, Boolean value)
at RedGate.SQLCompare.Engine.DatabaseLevelPermissions.set_Item(DatabaseLevelPermissionAction permission, PermissionType value)
at o.H()
at o.F()
at o.a(n )
at RedGate.SQLCompare.Engine.Database.Register(ConnectionProperties connectionProperties, Options options)
at SaveDatabaseSnapshot.Program.Main(String[] args) in C:\Projects\Utilities\SaveDatabaseSnapshot\Program.cs:line 24
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
The exception is occuring in the .Register call. This code works fine one half a dozen other databases and I have no way to step into your code so I can't quite figure out why this one particular database is causing this error. Any ideas?
Thanks,
using (Database stagingDB = new Database()) { var connectionProperties = new ConnectionProperties( args[1], args[2]); try { Console.WriteLine("Registering database " + connectionProperties.DatabaseName); stagingDB.Register(connectionProperties, Options.CaseSensitiveObjectDefinition | Options.DecryptPost2kEncryptedObjects); // Save a snapshot of the database to WidgetStaging.snp string snapshotFile = String.Format( @"{0}\{1}-{2}.snp", args[0], args[1].Replace('\\', '_'), args[2].Replace(' ', '_')); Console.WriteLine("Saving snapshot file \"{0}\"...", snapshotFile); stagingDB.SaveToDisk(snapshotFile); } catch (SqlException e) { Console.WriteLine("{0} occurred returning message '{1}'.", e.GetType().Name, e.Message); Console.WriteLine("Could not connect to database '{0}' on server '{1}'.", connectionProperties.DatabaseName, connectionProperties.ServerName); return -2; } return 0; }
And here's the exception:
System.ArgumentOutOfRangeException was unhandled
Message=Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Source=mscorlib
ParamName=index
StackTrace:
at System.Collections.BitArray.Set(Int32 index, Boolean value)
at System.Collections.BitArray.set_Item(Int32 index, Boolean value)
at RedGate.SQLCompare.Engine.DatabaseLevelPermissions.set_Item(DatabaseLevelPermissionAction permission, PermissionType value)
at o.H()
at o.F()
at o.a(n )
at RedGate.SQLCompare.Engine.Database.Register(ConnectionProperties connectionProperties, Options options)
at SaveDatabaseSnapshot.Program.Main(String[] args) in C:\Projects\Utilities\SaveDatabaseSnapshot\Program.cs:line 24
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
The exception is occuring in the .Register call. This code works fine one half a dozen other databases and I have no way to step into your code so I can't quite figure out why this one particular database is causing this error. Any ideas?
Thanks,
Kevin Hazzard, C# MVP & Friend of Red Gate
http://devjourney.com
http://devjourney.com
Comments
Have you tried connecting to it using the SQL Compare program to see if that works ok?
Redgate Software
Options.CaseSensitiveObjectDefinition | Options.DecryptPost2kEncryptedObjects
in my code that appear to be set in the stand-alone comparison tool. My code still crashes without those options, by the way. This database is identical in schema, collation, etc. to 5 others that I am snapshotting without issue.
Digging in a bit, I did find one difference between this database and the rest. The database I'm having trouble with shows "Guest account enabled for: ..." checked on the mapping page of my login. All the others do not show that check mark. I am a sysadmin on that server so I didn't think that would matter. And as I said before, using the SQL Compare tool, I am able to make a snapshot of the troublesome database.
I can't clear the "Guest account enabled for: ..." checkbox. And the help on MSDN says to disable the guest account on the status page of the login properties for the guest account. But I don't have a "guest" user in my server logins. Strange. Any advice there?
Thanks,
http://devjourney.com
I guess potentially this could affect things if it's trying to read some information about the guest account as the database has it enabled, but this doesn't work as it doesn't really exist?
Perhaps this link will be of use:
http://www.mssqltips.com/tip.asp?tip=1172
The other thing would be to try a profiler trace when attempting to connect and seeing what the last query that ran was; this may give a little more information about the area that's causing trouble.
Redgate Software
I ran the profiler as you recommended. Now I'm almost certain that it's the guest account is causing the issue. The last thing the shown in the profiler before the crash is a query that gets user and object permissions. I've attached it below along with two sets of results that show the successful "non-guest" access and the failing "guest access results. I've snipped the local users from the results in both cases to hide some details about my database from public eyes. But the most interesting part of the results is that none of the schemas have real data in them in the guest case. I suppose this is entirely my problem now. I must figure out how to remove the guest access from the database. The link you sent and the Microsoft help allude to disabling the guest login but I don't have a guest login showing in the server's security configuration. In a future version of the SDK's Database.Register method, it may be useful to determine if guest access has been enabled and report on that when exceptions occur.
Thanks,
Kevin
Here's the code that was executing and the two result sets:
/* successful "non-guest" results
ColumnPermissionID uid UserName ObjectName ObjectOwner class type state ObjectType columns
0 0 public sp_helpdiagrams dbo 1 EX G P NULL
0 2 guest sp_helpdiagrams dbo 1 EX D P NULL
0 0 public sp_helpdiagramdefinition dbo 1 EX G P NULL
0 2 guest sp_helpdiagramdefinition dbo 1 EX D P NULL
0 0 public sp_creatediagram dbo 1 EX G P NULL
0 2 guest sp_creatediagram dbo 1 EX D P NULL
0 0 public sp_renamediagram dbo 1 EX G P NULL
0 2 guest sp_renamediagram dbo 1 EX D P NULL
0 0 public sp_alterdiagram dbo 1 EX G P NULL
0 2 guest sp_alterdiagram dbo 1 EX D P NULL
0 0 public sp_dropdiagram dbo 1 EX G P NULL
0 2 guest sp_dropdiagram dbo 1 EX D P NULL
0 0 public fn_diagramobjects dbo 1 EX G FN NULL
0 2 guest fn_diagramobjects dbo 1 EX D FN NULL
0 1 dbo NULL 0 CO G NULL NULL
<snip/>
*/
/* errored "as guest" results
ColumnPermissionID uid UserName ObjectName ObjectOwner class type state ObjectType columns
0 0 public NULL 0 AL G NULL NULL
0 0 public NULL 0 ALAK G NULL NULL
0 0 public NULL 0 ALAR G NULL NULL
0 0 public NULL 0 ALAS G NULL NULL
0 0 public NULL 0 ALCF G NULL NULL
0 0 public NULL 0 ALDA G NULL NULL
0 0 public NULL 0 ALDS G NULL NULL
0 0 public NULL 0 ALED G NULL NULL
0 0 public NULL 0 ALFT G NULL NULL
0 0 public NULL 0 ALMT G NULL NULL
0 0 public NULL 0 ALRL G NULL NULL
0 0 public NULL 0 ALRT G NULL NULL
0 0 public NULL 0 ALSB G NULL NULL
0 0 public NULL 0 ALSC G NULL NULL
0 0 public NULL 0 ALSK G NULL NULL
0 0 public NULL 0 ALSM G NULL NULL
0 0 public NULL 0 ALSV G NULL NULL
0 0 public NULL 0 ALTG G NULL NULL
0 0 public NULL 0 ALUS G NULL NULL
0 0 public NULL 0 AUTH G NULL NULL
0 0 public NULL 0 BADB G NULL NULL
0 0 public NULL 0 BALO G NULL NULL
0 0 public NULL 0 CORP G NULL NULL
0 0 public NULL 0 CP G NULL NULL
0 1 dbo NULL 0 CO G NULL NULL
0 2 guest NULL 0 AL G NULL NULL
0 2 guest NULL 0 ALAK G NULL NULL
0 2 guest NULL 0 ALAR G NULL NULL
0 2 guest NULL 0 ALAS G NULL NULL
0 2 guest NULL 0 ALCF G NULL NULL
0 2 guest NULL 0 ALDA G NULL NULL
0 2 guest NULL 0 ALDS G NULL NULL
0 2 guest NULL 0 ALED G NULL NULL
0 2 guest NULL 0 ALFT G NULL NULL
0 2 guest NULL 0 ALMT G NULL NULL
0 2 guest NULL 0 ALRL G NULL NULL
0 2 guest NULL 0 ALRT G NULL NULL
0 2 guest NULL 0 ALSB G NULL NULL
0 2 guest NULL 0 ALSC G NULL NULL
0 2 guest NULL 0 ALSK G NULL NULL
0 2 guest NULL 0 ALSM G NULL NULL
0 2 guest NULL 0 ALSV G NULL NULL
0 2 guest NULL 0 ALTG G NULL NULL
0 2 guest NULL 0 ALUS G NULL NULL
0 2 guest NULL 0 AUTH G NULL NULL
0 2 guest NULL 0 BADB G NULL NULL
0 2 guest NULL 0 BALO G NULL NULL
0 2 guest NULL 0 CL G NULL NULL
0 2 guest NULL 0 CO G NULL NULL
0 2 guest NULL 0 CORP G NULL NULL
0 2 guest NULL 0 CP G NULL NULL
0 2 guest NULL 0 CRAG G NULL NULL
0 2 guest NULL 0 CRAK G NULL NULL
0 2 guest NULL 0 CRAS G NULL NULL
0 2 guest NULL 0 CRCF G NULL NULL
0 2 guest NULL 0 CRDF G NULL NULL
0 2 guest NULL 0 CRED G NULL NULL
0 2 guest NULL 0 CRFN G NULL NULL
0 2 guest NULL 0 CRFT G NULL NULL
0 2 guest NULL 0 CRMT G NULL NULL
0 2 guest NULL 0 CRPR G NULL NULL
0 2 guest NULL 0 CRQU G NULL NULL
0 2 guest NULL 0 CRRL G NULL NULL
0 2 guest NULL 0 CRRT G NULL NULL
0 2 guest NULL 0 CRRU G NULL NULL
0 2 guest NULL 0 CRSB G NULL NULL
0 2 guest NULL 0 CRSC G NULL NULL
0 2 guest NULL 0 CRSK G NULL NULL
0 2 guest NULL 0 CRSM G NULL NULL
0 2 guest NULL 0 CRSN G NULL NULL
0 2 guest NULL 0 CRSV G NULL NULL
0 2 guest NULL 0 CRTB G NULL NULL
0 2 guest NULL 0 CRTY G NULL NULL
0 2 guest NULL 0 CRVW G NULL NULL
0 2 guest NULL 0 CRXS G NULL NULL
0 2 guest NULL 0 DL G NULL NULL
0 2 guest NULL 0 EX G NULL NULL
0 2 guest NULL 0 IN G NULL NULL
0 2 guest NULL 0 RF G NULL NULL
0 2 guest NULL 0 SL G NULL NULL
0 2 guest NULL 0 SPLN G NULL NULL
0 2 guest NULL 0 SUQN G NULL NULL
0 2 guest NULL 0 TO G NULL NULL
0 2 guest NULL 0 UP G NULL NULL
0 2 guest NULL 0 VW G NULL NULL
0 2 guest NULL 0 VWDS G NULL NULL
<snip/>
*/
http://devjourney.com
Redgate Software