I am using below code to stop or start the windows service. Which is I am doing sucessfully.
Expand|Select|Wrap|Line Numbers
- ManagedComputer mc = default(ManagedComputer);
- mc = new ManagedComputer();
- Service svc = default(Service);
- svc = mc.Services["SQLSERVERAGENT"];
- if (svc.ServiceState == ServiceState.Running)
- {
- svc.Stop();
- Response.Write (computerName + "Service Stoped");
- }
- else
- {
- svc.Start();
- Response.Write (computerName + "Service Started");
- }
When I try to do that I am getting following error so please help me to remove that error.
Microsoft.SqlServer.Management.Smo.SmoException was unhandled by user code
HelpLink="http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=9.00. 1399.00&LinkId=20476"
Message="An exception occurred in SMO while trying to manage a service."
Source="Microsoft.SqlServer.Smo"
StackTrace:
at Microsoft.SqlServer.Management.Smo.Wmi.ManagedComp uter.TryConnect()
at Microsoft.SqlServer.Management.Smo.Wmi.WmiSmoObjec t.get_Proxy()
at Microsoft.SqlServer.Management.Smo.Wmi.WmiSmoObjec t.get_Proxy()
at Microsoft.SqlServer.Management.Smo.Wmi.WmiSmoObjec t.ImplInitialize(String[] fields, OrderBy[] orderby)
at Microsoft.SqlServer.Management.Smo.Wmi.WmiSmoObjec t.Initialize()
at Microsoft.SqlServer.Management.Smo.Wmi.ServiceColl ection.GetObjectByName(String name)
at Microsoft.SqlServer.Management.Smo.Wmi.ServiceColl ection.get_Item(String name)
at TestService.tempFunc() in e:\Working Folder\BI_Server_Monitor.root\BI_Server_Monitor\BI _Server_Monitor\TestService.aspx.cs:line 37
at TestService.Page_Load(Object sender, EventArgs e) in e:\Working Folder\BI_Server_Monitor.root\BI_Server_Monitor\BI _Server_Monitor\TestService.aspx.cs:line 21
at System.Web.Util.CalliHelper.EventArgFunctionCaller (IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Cal lback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException: System.UnauthorizedAccessException
Message="Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"
Source="mscorlib"
StackTrace:
at System.Runtime.InteropServices.Marshal.ThrowExcept ionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Management.ManagementScope.InitializeGuts(O bject o)
at System.Management.ManagementScope.Initialize()
at System.Management.ManagementScope.Connect()
at Microsoft.SqlServer.Management.Smo.Wmi.ManagedComp uter.TryConnect()
InnerException: