Click here to Skip to main content
16,017,954 members
Home / Discussions / C#
   

C#

 
GeneralRe: Replacing system tray clock Pin
Luc Pattyn29-Nov-06 5:33
sitebuilderLuc Pattyn29-Nov-06 5:33 
QuestionConnecting to a server via GSM Pin
Waskira27-Nov-06 2:13
Waskira27-Nov-06 2:13 
AnswerRe: Connecting to a server via GSM Pin
chris_do27-Nov-06 3:41
chris_do27-Nov-06 3:41 
GeneralRe: Connecting to a server via GSM Pin
Waskira27-Nov-06 4:33
Waskira27-Nov-06 4:33 
QuestionOpening a new console window Pin
Dario Solera27-Nov-06 1:53
Dario Solera27-Nov-06 1:53 
AnswerRe: Opening a new console window Pin
stancrm27-Nov-06 3:51
stancrm27-Nov-06 3:51 
AnswerRe: Opening a new console window Pin
Nader Elshehabi27-Nov-06 8:59
Nader Elshehabi27-Nov-06 8:59 
QuestionCan't run windows service Pin
saikat_0027-Nov-06 1:07
saikat_0027-Nov-06 1:07 
Hi, I've problem creating windows service, that can insert values into database using Sql-server 2000 and platform is ASP.NET 2.0 (C#), anyway I followed the whole code using from 'the code project' website; http://www.codeproject.com/cs/system/WindowsService.asp
I write the whole code below again, and please let me know, what's wrong...
Thanks.

// WindowsService.cs

using System;
using System.Diagnostics;
using System.ServiceProcess;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace WindowsService
{
class WindowsService : ServiceBase
{
///
/// Public Constructor for WindowsService.
/// - Put all of your Initialization code here.
///

public WindowsService()
{
this.ServiceName = "My Windows Service";
this.EventLog.Source = "My Windows Service";
this.EventLog.Log = "Application";

// These Flags set whether or not to handle that specific
// type of event. Set to true if you need it, false otherwise.
this.CanHandlePowerEvent = true;
this.CanHandleSessionChangeEvent = true;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.CanStop = true;

if (!EventLog.SourceExists("My Windows Service"))
EventLog.CreateEventSource("My Windows Service", "Application");
}

///
/// The Main Thread: This is where your Service is Run.
///

static void Main()
{
ServiceBase.Run(new WindowsService());

Test.ConnectionManager.Instance().Cb_Wap_NewItem(); // I addded this part from my


}

///
/// Dispose of objects that need it here.
///

/// <param name="disposing" />Whether or not disposing is going on.
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}

///
/// OnStart: Put startup code here
/// - Start threads, get inital data, etc.
///

/// <param name="args" />
protected override void OnStart(string[] args)
{
base.OnStart(args);
}

///
/// OnStop: Put your stop code here
/// - Stop threads, set final data, etc.
///

protected override void OnStop()
{
base.OnStop();
}

///
/// OnPause: Put your pause code here
/// - Pause working threads, etc.
///

protected override void OnPause()
{
base.OnPause();
}

///
/// OnContinue: Put your continue code here
/// - Un-pause working threads, etc.
///

protected override void OnContinue()
{
base.OnContinue();
}

///
/// OnShutdown(): Called when the System is shutting down
/// - Put code here when you need special handling
/// of code that deals with a system shutdown, such
/// as saving special data before shutdown.
///

protected override void OnShutdown()
{
base.OnShutdown();
}

///
/// OnCustomCommand(): If you need to send a command to your
/// service without the need for Remoting or Sockets, use
/// this method to do custom methods.
///

/// <param name="command" />Arbitrary Integer between 128 & 256
protected override void OnCustomCommand(int command)
{
// A custom command can be sent to a service by using this method:
//# int command = 128; //Some Arbitrary number between 128 & 256
//# ServiceController sc = new ServiceController("NameOfService");
//# sc.ExecuteCommand(command);

base.OnCustomCommand(command);
}

///
/// OnPowerEvent(): Useful for detecting power status changes,
/// such as going into Suspend mode or Low Battery for laptops.
///

/// <param name="powerStatus" />The Power Broadcase Status (BatteryLow, Suspend, etc.)
protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
{
return base.OnPowerEvent(powerStatus);
}

///
/// OnSessionChange(): To handle a change event from a Terminal Server session.
/// Useful if you need to determine when a user logs in remotely or logs off,
/// or when someone logs into the console.
///

/// <param name="changeDescription" />
protected override void OnSessionChange(SessionChangeDescription changeDescription)
{
base.OnSessionChange(changeDescription);
}

private void InitializeComponent()
{

}
}
}


// WindowsServiceInstaller.cs

using System;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

namespace WindowsService
{
[RunInstaller(true)]
public class WindowsServiceInstaller : Installer
{
///
/// Public Constructor for WindowsServiceInstaller.
/// - Put all of your Initialization code here.
///

public WindowsServiceInstaller()
{
ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new ServiceInstaller();

//# Service Account Information
serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
serviceProcessInstaller.Username = null;
serviceProcessInstaller.Password = null;

//# Service Information
serviceInstaller.DisplayName = "Test C# Windows Service";
serviceInstaller.StartType = ServiceStartMode.Automatic;

// This must be identical to the WindowsService.ServiceBase name
// set in the constructor of WindowsService.cs
serviceInstaller.ServiceName = "My Windows Service";

this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);
}
}
}


// ConnectionManager.cs

using System;
using System.Collections;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;

namespace Test
{
class ConnectionManager
{
private SqlConnection connectionSql = null;
private SqlCommand commandSql = null;
private SqlCommand commandSql2 = null;

private SqlDataReader dataReaderSql = null;
private SqlDataReader dataReaderSql2 = null;

private String queryString = String.Empty;
private ArrayList returnArrayList = null;
private ArrayList returnArrayList2 = null;

private ConnectionManager()
{
returnArrayList = new ArrayList();
returnArrayList2 = new ArrayList();
connectionSql = new SqlConnection("Server=localhost;Database=dbMsgBox;User Id=sa; password=1234");

}


public static ConnectionManager Instance()
{
return new ConnectionManager();
}


public void Cb_Wap_NewItem()
{
commandSql = new SqlCommand("Select Top 1(out_msg_id) from tbl_outbox ORDER BY out_msg_id DESC", connectionSql);

connectionSql.Open();

dataReaderSql = commandSql.ExecuteReader();


if (dataReaderSql.Read())
{
try
{
dataReaderSql.Close();


SqlDataReader reader = null;

string sql = "Select Top 1(out_msg_id) as last_id from tbl_outbox ORDER BY out_msg_id DESC";
SqlCommand command = new SqlCommand();
command.CommandText = sql;
command.Connection = connectionSql;
reader = command.ExecuteReader();

if (reader.Read())
{
long last_entried_data = Convert.ToInt64(reader[0]);

long new_entried_data;

new_entried_data = last_entried_data + 200;


long mobNo = 5555;
long in_msg_id = 6980;
string body = "habi zabi";
decimal stat = Convert.ToDecimal(0);
int msg_typ = 0;
int send_port = 3838;

reader.Close();


commandSql2 = new SqlCommand("SET IDENTITY_INSERT [tbl_Outbox] ON;insert into tbl_Outbox(out_msg_id,mobileno,in_msg_id,body,status,msg_type,send_port) values(" + new_entried_data + "," + mobNo + "," + in_msg_id + ",'" + body + "'," + stat + "," + msg_typ + "," + send_port + "); SET IDENTITY_INSERT [tbl_Outbox] OFF;", connectionSql);

dataReaderSql2 = commandSql2.ExecuteReader();


}


dataReaderSql2.Close();

connectionSql.Close();

}
catch (Exception e)
{

connectionSql.Close();

Cb_Wap_NewItem();

System.Console.WriteLine(e);
}

}

}

}
}
AnswerRe: Can't run windows service Pin
Weckmann27-Nov-06 3:56
Weckmann27-Nov-06 3:56 
QuestionSaving & Loading TreeView especially Tag of Nodes Pin
Andy Rama27-Nov-06 0:54
Andy Rama27-Nov-06 0:54 
AnswerRe: Saving & Loading TreeView especially Tag of Nodes Pin
Not Active27-Nov-06 3:33
mentorNot Active27-Nov-06 3:33 
Question[Message Deleted] Pin
shyamy27-Nov-06 0:11
shyamy27-Nov-06 0:11 
AnswerRe: Conversion Pin
ejuanpp27-Nov-06 1:09
ejuanpp27-Nov-06 1:09 
AnswerRe: Conversion Pin
AB777127-Nov-06 1:28
AB777127-Nov-06 1:28 
AnswerRe: Conversion Pin
Guffa27-Nov-06 1:44
Guffa27-Nov-06 1:44 
AnswerRe: Conversion Pin
albCode27-Nov-06 1:44
albCode27-Nov-06 1:44 
QuestionProblem with response.binarywrite() Pin
Anish Gangadharan27-Nov-06 0:10
Anish Gangadharan27-Nov-06 0:10 
AnswerRe: Problem with response.binarywrite() Pin
Guffa27-Nov-06 3:48
Guffa27-Nov-06 3:48 
GeneralRe: Problem with response.binarywrite() Pin
Anish Gangadharan27-Nov-06 20:35
Anish Gangadharan27-Nov-06 20:35 
AnswerRe: Problem with response.binarywrite() Pin
Guffa28-Nov-06 3:03
Guffa28-Nov-06 3:03 
GeneralRe: Problem with response.binarywrite() Pin
Anish Gangadharan28-Nov-06 17:16
Anish Gangadharan28-Nov-06 17:16 
AnswerRe: Problem with response.binarywrite() Pin
Guffa28-Nov-06 20:28
Guffa28-Nov-06 20:28 
GeneralRe: Problem with response.binarywrite() Pin
Anish Gangadharan28-Nov-06 22:24
Anish Gangadharan28-Nov-06 22:24 
AnswerRe: Problem with response.binarywrite() Pin
Guffa28-Nov-06 22:33
Guffa28-Nov-06 22:33 
GeneralRe: Problem with response.binarywrite() Pin
Anish Gangadharan28-Nov-06 23:20
Anish Gangadharan28-Nov-06 23:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.