, , , , , ,

How to Make Windows Service Using C# 2008

I have an task to run a Stored procedure after every 30 minutes.
My first choice is to create a job on Sql server. But I also have to write a log file along with the stored procedure.

Now, my choice will be Windows service. Creating a windows service is a very easy task but how to deply......

let me explain how to work with windows service.

Creating a program that can run in the background of the operating system has any benefits, this is very important as the program can run without a graphic user interface. Complex service that are to run are longer time, and without the need the user to login the system. Windows services are harder to develop, debug, and test. But if you software are to run in server that require to start up automatically with the server is turn on, Windows service is your choice.

Creating your skeleton project in Visual Studio 2008

To create a Windows Service project in Visual Studio 2008, you must select from the Visual C# Windows Projects. Give your Windows Service a name, and then select OK







As a result Visual Studio 2008 creates you an empty solution with a Windows Service project as a default to the development.







Switch to code view, to start developing.







As you can see that Visual Studio 2008 has simplify the layout and initialization of the code for Windows Service development. Now let’s start by creating the PingJob() method;

public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
System.Threading.ThreadStart job = new System.Threading.ThreadStart(PingJob);

System.Threading.Thread thread = new System.Threading.Thread(job);

thread.Start();
}

protected override void OnStop()
{
Environment.Exit(-1);
}

static void PingJob()
{
while (true)
{
try
{
string fname = "c:\\a.txt";
FileInfo file1 = new FileInfo(fname);
StreamWriter sw = File.AppendText("c:\\a.txt");
sw.WriteLine("This is a demo for appending text content to a file");
// Writing a string directly to the file
sw.WriteLine(DateTime.Now); // Writing content read from the textbox in the form
sw.Close();
//System.Net.IPHostEntry objIPHE = System.Net.Dns.GetHostEntry("www.google.com");
System.Threading.Thread.Sleep(2000); // wait 15 seconds
}
catch (Exception ex)
{

EventLog.WriteEntry("PingTest Service", ex.Message, EventLogEntryType.Warning);

}

}

}
}

Do a Build Solution (Ctrl+Shift+B) to make sure that all your codes don’t have any bugs.


Creating an Installer for you Windows Service

From you solution select File>Add>New Project… from menu








Then from Add New Project, select Other Project Types, and choose Setup Project








Then Visual Studio 2008 will prepare a setup shell for you configure the set require of Solution, and now let add our Windows Service to our installer. From the Application Folder right-click, select Add>Project Output.








From the Add Project Output Group windows select the Primary output and make sure that your Windows Service Project is select has the project, then select OK.








From the Setup properties configure your properties of your installer;







Switch to code mode to start configure the behavior of your windows service application, doing this by selecting ProjectInstaller.Designer.cs.







Modify the service installer as you require, as for me I usually modify account the service will run as, the service startup type, and the service name that will appear in Windows Service MMC.

private
void InitializeComponent()

{


this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();


this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();


//


// serviceProcessInstaller1


//


this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;


//this.serviceProcessInstaller1.Password = null;


//this.serviceProcessInstaller1.Username = null;


this.serviceProcessInstaller1.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.serviceProcessInstaller1_AfterInstall);


//


// serviceInstaller1


//


this.serviceInstaller1.ServiceName = “PingTestService”;


this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;


// ProjectInstaller


//


this.Installers.AddRange(new System.Configuration.Install.Installer[] {


this.serviceProcessInstaller1,


this.serviceInstaller1});

}
















Do a Build Solution to test that does your solution contains any bugs, let’s hope the best. Then Build the Installer from the Setup project by right-click on the Setup project, and choose build the project. Therefore you can see that there is Install/Uninstall option if you complete building the installer, and that you right-click again.

Now go to command prompt:-

C:\Windows\Microsoft.NET\Framework\v2.0.50727>installutil C:\WinService\WindowsService1\bin\Debug\WindowsService1.exe


Download Code from this location
Share:
Read More
, , , ,

How to Debug JavaScript Using C#

Client Java Script is one of the most important things in web development but not the best and easiest to develop. Building of bigger and more complicated scripts, especially using DOM model or form field values can cause a lot of frustration and head pain. Moreover, JavaScript debugging is not easy and obvious as should be. But there is a hope. Before start, we should ensure that client script debugging is not disabled in IE as it is by default.

Suitable options are located on advanced tab of Internet Options where both script debugging checkboxes should be unchecked.

Go to Tools>>Options >> and refer image below.







You can also download code from http://www.4shared.com/file/106977680/64e2c2b2/DebugJavascript.html

Now open your code and put Debugger; key word in your code. Refer image below





Now run your code, on IE. You will find and window. Refer image below.







Click yes to start debugging. It will take some time to open debugger because you are debugging first time. Refer Image below.









Now, you can easily debug your Java Script code with VS debugger.

Hope this code will help you to come out from hectic task to Java Script Debugging.

You can also use Fire Bug to debug your code on Mozilla Firefox.

You can also refer following links for more help:
http://support.microsoft.com/kb/816173
http://dotnetsavvyblog.blogspot.com/2007/11/how-to-debug-javascript-dom.html





Share:
Read More
, ,

launch of Visual Studio 2010 and .NET Framework 4 Beta 1

Microsoft Launched Visual Studio 2010 Beta 1.

For more information please visit
http://blogs.msdn.com/jasonz/archive/2009/05/20/general-download-of-vs2010-net-framework-4-0-beta-1.aspx

Can also download from
http://msdn.microsoft.com/hi-in/vstudio/dd582936(en-us).aspx

One new language suite F#
http://msdn.microsoft.com/hi-in/fsharp/default(en-us).aspx

The final release will take time to be launched.

I really want to hear your feedback! I will definitely follow blog comments, but the best way to give us that feedback is through the Visual Studio 2010 / .NET Framework 4 Beta 1 site. You can get your issues and suggestions routed to the teams directly by going through this route.
Share:
Read More
, , , , , , , ,

Remote Desktop Access: Team viewer, Remote, Desktop, Sharing, Files Sharing, Data Sharing, Team Sharing, Team Meetings

In today’s world of technology remote desktop sharing has become a common way of working on remote pc’s or servers.

We all know how to access Remote desktop, but what if I have to share my desktop to give a demo of application or module or anything.

Suppose I have to give a simple demo to a friend, like how to create a Power Point Slides. Now, the first idea is to create a document or to create a pictorial document or create a video and upload to youtube.com or some other alternate.

But here is the solution.

You can use Team Viewer

http://www.teamviewer.com/download/index.aspx














Install team Viewer to both the PC (From where you have to access and to which you have to access)

Ask for the Id and password of the machine which you want to access.

Now enter ID into box and click connect to partner. If ask for password provide password.

Now, you can view desktop of your partner or friend.

You can also take control into your hand. You can also view more option like file transfer etc. For more help


http://www.teamviewer.com/products/firststeps.aspx


Something about Team Viewer


Team Viewer is the fast, simple and friendly solution for remote access over the Internet - all applications in one single, very affordable module:

Remote access to PCs and servers via Internet

Instantly take control over a computer anywhere on the Internet, even through firewalls. No installation required, just use it fast and secure.

Training, sales and teamwork

Team Viewer can also be used to present your desktop to a partner on the Internet - great for team collaboration, trainings and sales presentations.

File transfer, chat and more

Share your files, chat; switch the direction during a teamwork session, and a lot more is included in Team Viewer.
Share:
Read More
, , , , ,

How to use DateDiff Function in C#

How to use DateDiff Function in C#.

As earlier in ASP and VB6 we have too may conversion and date time functions.

After working for so many years, we all had become habitual of working with the same.

We have almost all the functions available in c#. But, sometimes lost control on some functions.

Like for datediff WE have to write so much code in C#. At this moment we just remember functions of VB.

Now, say thanks to Microsoft… What we can use is:-

Add a reference of Microsoft.VisualBasic and access the DateDiff function like following ex.

long nYears = Microsoft.VisualBasic.DateAndTime.DateDiff(Microsoft.VisualBasic.DateInterval.Year,
dt, dtNow,
Microsoft.VisualBasic.FirstDayOfWeek.System,
Microsoft.VisualBasic.FirstWeekOfYear.System);

By using the reference of visual basic you can use any function of Visual Basic or vice-versa.
Share:
Read More
, , , , ,

Client side validations in C# using javascript

Client-side validation uses the same error display mechanism as server-side checking.

Validating input data on web pages is usually a function performed by the server.
The web page allows the user to enter data, and when the Submit button is pressed, the browser wraps up the data into an HTTP request and sends it to the server.

The server checks each data field to make sure it is valid, and if any problems are found, a new form along with error messages is sent back to the browser.
Wouldn't it be much more useful if problems could be detected in the browser before a server request is made? This approach would provide two primary advantages.

It would lighten the load on the server, and, more importantly, it would notify the user of a problem with a data field almost immediately after he or she entered the bad data.
This supports the truism that errors are cheapest to fix the closer the detection is to the original creation of the error.

For example, if there is a problem with a zip code field and the user is notified just after he enters the bad zip code, then he is still thinking about zip code and can easily make the correction.
If the user isn't notified until the server response comes back, he's already stopped thinking about zip code—his mind has moved on to other concerns.
This problem of context switching is especially difficult when the server returns errors for many different fields.

Now, Let me explain how to check client side validations using Java script


To test this code make a new website. Add a new page with name "Default" and Paste the code provided below.

You can also download code.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Testing Client Side Validations.</title>

<script language="javascript" type="text/javascript">
function validate()
{
if (document.getElementById("<%=txtName.ClientID%>").value=="")
{
alert("Name Feild can not be blank");
document.getElementById("<%=txtName.ClientID%>").focus();
return false;
}
if(document.getElementById("<%=txtEmail.ClientID %>").value=="")
{
alert("Email id can not be blank");
document.getElementById("<%=txtEmail.ClientID %>").focus();
return false;
}
var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
var emailid=document.getElementById("<%=txtEmail.ClientID %>").value;
var matchArray = emailid.match(emailPat);
if (matchArray == null)
{
alert("Your email address seems incorrect. Please try again.");
document.getElementById("<%=txtEmail.ClientID %>").focus();
return false;
}
if(document.getElementById("<%=txtWebURL.ClientID %>").value=="")
{
alert("Web URL can not be blank");
document.getElementById("<%=txtWebURL.ClientID %>").value="http://"
document.getElementById("<%=txtWebURL.ClientID %>").focus();
return false;
}
var Url="^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"
var tempURL=document.getElementById("<%=txtWebURL.ClientID%>").value;
var matchURL=tempURL.match(Url);
if(matchURL==null)
{
alert("Web URL does not look valid");
document.getElementById("<%=txtWebURL.ClientID %>").focus();
return false;
}
if (document.getElementById("<%=txtZIP.ClientID%>").value=="")
{
alert("Zip Code is not valid");
document.getElementById("<%=txtZIP.ClientID%>").focus();
return false;
}
var digits="0123456789";
var temp;
for (var i=0;i<document.getElementById("<%=txtZIP.ClientID %>").value.length;i++)
{
temp=document.getElementById("<%=txtZIP.ClientID%>").value.substring(i,i+1);
if (digits.indexOf(temp)==-1)
{
alert("Please enter correct zip code");
document.getElementById("<%=txtZIP.ClientID%>").focus();
return false;
}
}
return true;
}
</script>

</head>
<body>
<form id="form1" runat="server">
Testing Client Side Validations.<br />
<br />
<br />
<div>
Name :
<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
Email :
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br />
Web URL :
<asp:TextBox ID="txtWebURL" runat="server"></asp:TextBox><br />
Zip :
<asp:TextBox ID="txtZIP" runat="server"></asp:TextBox><br />
<br />

<asp:Button ID="btnSubmit" OnClientClick=" return validate()" runat="server" Text="Submit" />
</div>
</form>
</body>
</html>


Run you application and try to test the validations. You will find all the validations are raised before raising the server side event if submit button
Share:
Read More