troubleshooting Question

windows service application

Avatar of rengsons
rengsonsFlag for India asked on
Networking.NET ProgrammingProject Management Software
3 Comments1 Solution333 ViewsLast Modified:
Hai,

I am doing Windows Service project using C#. Here i can start the service(In the service manger it started).But nothing is working.
For Example i want to insert data to DB.But i cant.My sample code  have attached.Please guide me the right path to solve the problem.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Data.SqlClient;
 
namespace TrukeeService1
{
    public partial class Service1 : ServiceBase
    {
        
       // private System.ComponentModel.Container components = null;
 
        public Service1()
        {
            InitializeComponent();
       
        }
 
        protected override void OnStart(string[] args)
        {
           
            SqlConnection connection = null;
            SqlCommand command = null;
            try
            {
 
                connection = new SqlConnection(" Data Source=.;uid=sa;pwd=sternasys;Initial Catalog=Trukee_ManagerTest;Integrated Security=True");
                
                connection.Open();
                SqlCommand command1 = new SqlCommand("insert into TBL_boxid (Box_ID,Firmware_version,Company_Name,Location) values('101','101','Sterna','CBE')");
                               
                command1.ExecuteNonQuery();
                connection.Close();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
           
 
            
        }
 
        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
            SqlConnection connection = null;
            SqlCommand command = null;
            try
            {
 
                //Data Source=.;uid=sa;pwd=sternasys;Initial Catalog=TruKee;Integrated Security=True
                connection = new SqlConnection(" Data Source=.;uid=sa;pwd=sternasys;Initial Catalog=Trukee_ManagerTest;Integrated Security=True");
                //"Server=localhost;Database=SampleDatabase;Integrated 
                //Security=false;User Id=sa;Password=;");
                SqlCommand command1 = new SqlCommand("insert into TBL_boxid (Box_ID,Firmware_version,Company_Name,Location) values('101','101','Sterna','CBE')");
                //"INSERT INTO MyServiceLog (vc_Status, dt_Created) 
                //VALUES ('" + Message + "',getdate())", connection);
                connection.Open();
                command1.ExecuteNonQuery();
                connection.Close();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
           
        }
 
 
 
    
    
    }
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros