Error when create a Server object using SMO

Please help: I got this error and can't figure out what went wrong.

The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception. on the line that create Server object.

This is the code  (Visual C# 2005 - SQL Server 2000 - Windows Active Directory)

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

using Microsoft.SqlServer.Management.Smo;

using Microsoft.SqlServer.Management.Common; 

namespace SSMO

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

Server oSvr = new Server("myServer");

foreach (Database oDB in oSvr.Databases)

{

cboDatabases.Items.Add(oDB.Name.ToString());

}

}

}

 



Answer this question

Error when create a Server object using SMO

  • Anthony Yott

    I guess there should be additionally some inner exceptions which should clarify th error in detail.

    HTH, Jens Suessmeyer.

    ---
    http://www.sqlserver2005.de
    ---


  • wpf314

    This is the error:

    System.TypeInitializationException was unhandled
    Message="The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception."
    Source="System.Data"
    TypeName="System.Data.SqlClient.SqlConnection"
    StackTrace:
    at System.Data.SqlClient.SqlConnection..ctor()
    at Microsoft.SqlServer.Management.Common.ServerConnection..ctor()
    at Microsoft.SqlServer.Management.Smo.ExecutionManager..ctor(String name)
    at Microsoft.SqlServer.Management.Smo.Server..ctor(String name)
    at SSMO.Form1.Form1_Load(Object sender, EventArgs e)
    at System.Windows.Forms.Form.OnLoad(EventArgs e)
    at System.Windows.Forms.Form.OnCreateControl()
    at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
    at System.Windows.Forms.Control.CreateControl()
    at System.Windows.Forms.Control.WmShowWindow(Message& m)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
    at System.Windows.Forms.ContainerControl.WndProc(Message& m)
    at System.Windows.Forms.Form.WmShowWindow(Message& m)
    at System.Windows.Forms.Form.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
    at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
    at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
    at System.Windows.Forms.Control.set_Visible(Boolean value)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.Run(Form mainForm)
    at SSMO.Program.Main()
    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()


  • learnerplates

    As the constructor of the SQLConnection is throwing an error, this might be just a slight misconfiguration problem. As I searched through the Net I found that this could be something based on a misconigured app.config file or the lack of a bad installation where the performance counters of the SQL Connection classes are not fully installed and registered. Never had that, but try to use a more simple command to make sure that this is based on that error:

    new
    System.Data.SqlClient.SqlConnection()).Dispose();


    HTH, Jens Suessmeyer.

    ---
    http://www.sqlserver2005.de
    ---


  • Error when create a Server object using SMO