Failed to initialize object as reader

Dear All,

I have created a small tool to trace SQL queries executed from our application. The tool is working fine with SQL server 2012 without any issues. 

SqlConnectionInfo connectionInfo = new SqlConnectionInfo();
            connectionInfo.ServerName = ConfigurationManager.AppSettings["ServerName"];
            connectionInfo.DatabaseName = ConfigurationManager.AppSettings["Database"];
            connectionInfo.UserName = ConfigurationManager.AppSettings["UserId"];
            connectionInfo.Password = ConfigurationManager.AppSettings["Password"];
            connectionInfo.UseIntegratedSecurity = false;
            ConnectionInfoBase connectionInfoBase = connectionInfo;          
            string applicationName = ConfigurationManager.AppSettings["ApplicationName"];
            TraceServer trace = new TraceServer();
            try
            {
                trace.InitializeAsReader(connectionInfoBase, ConfigurationManager.AppSettings["Template"]);
                SQLTouchRunningStatus(true);
                while (trace.Read())
                {
                    if (_backgroundWorker.CancellationPending)
                    {
                        SQLTouchRunningStatus(false);
                        e.Cancel = true;
                        break;
                    }

                    string eventClass = trace["EventClass"].ToString();
                    if (eventClass.Equals("RPC:Completed") || eventClass.Equals("SQL:BatchCompleted"))
                    {
                        if (applicationName.Equals(trace["ApplicationName"].ToString(), StringComparison.OrdinalIgnoreCase))
                        {
                            string textData = trace["TextData"].ToString();
                            if (textData.Contains("network protocol"))
                                continue;

                            // InvokeRequired required compares the thread ID of the
                            // calling thread to the thread ID of the creating thread.
                            // If these threads are different, it returns true.
                            if (this.InvokeRequired)
                            {
                                DisplayTraceData dataSource = new DisplayTraceData(GetTraceData);
                                this.Invoke(dataSource, new object[] { textData });
                            }
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                string message = exc.Message;
                if (exc.InnerException != null)
                    message += ", " + exc.InnerException.Message;

                if (this.InvokeRequired)
                {
                    DisplayTraceData dataSource = new DisplayTraceData(GetTraceData);
                    this.Invoke(dataSource, new object[] { message });
                }
            }
        }

However, when i try to run the same app in my colleague machine (He has SQL 2008 R2 installed, i get the below error message)

Failed to initialize object as reader., Could not load file or assembly 'Microsoft.SqlServer.Instapi, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.

I try the following to solve the problem:

   1) Install latest SharedManagementObjects Pacakge 

   2) Add assembly binding to the app config.

But still i received the same error message.

Could any one please advise me on this.

Thanks.

Best Regards,

Mohamed Nafly


  • Edited by Nafly Wednesday, September 02, 2015 6:29 AM
September 2nd, 2015 6:29am

Hi Nafly ,

I think that  "'Microsoft.SqlServer.Instapi, Version=11.0.0.0" means that you have asked to use a dll concerning SQL Server 2012 , which is not existing when SQL Server 2008 R2 is installed ( the dll should be found in the folder 10 even if the version is 10.5 )

The only way I know is to include the SMO dll in your project managing the application , the executable will be bigger but , usually , it is working in the correct way. Be careful that you don't forget some dll corresponding to the 4.0 version which could be uninstalled on your colleague machine.

Have a nice day

Free Windows Admin Tool Kit Click here and download it now
September 8th, 2015 8:37pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics