Install windows services
Go to "Start" - "All Programs" -"Microsoft Visual Studio 2012" - "Visual Studio Tools" then click "Developer Command Prompt for VS2012".
Go to folder location in which exe is present in command prompt and use the below command to install the windows service application (SampleWindowsService).
InstallUtil.exe “SampleWindowsService.exe”
Successful message will be displayed in command prompt after successful installation.
Go to services and see your windows services which will be names as Service1.
Now this service will be running continuously and will write text in LogFile.txt file which is created in the same folder from where your exe is executed. For every 30 seconds task will be executed and will write new text in that LogFile.
Start and stop your service from services. It will log the start and stop enteries in LogFile as shown below.
3/10/2016 1:21:25 PM: Test window service started
3/10/2016 1:21:55 PM: Job is running
3/10/2016 1:22:25 PM: Job is running
3/10/2016 1:22:55 PM: Job is running
3/10/2016 1:23:25 PM: Job is running
3/10/2016 1:23:55 PM: Job is running
3/10/2016 1:24:25 PM: Job is running
3/10/2016 1:24:41 PM: Test window service stopped
Go to folder location in which exe is present in command prompt and use the below command to install the windows service application (SampleWindowsService).
InstallUtil.exe “SampleWindowsService.exe”
Successful message will be displayed in command prompt after successful installation.
Go to services and see your windows services which will be names as Service1.
Now this service will be running continuously and will write text in LogFile.txt file which is created in the same folder from where your exe is executed. For every 30 seconds task will be executed and will write new text in that LogFile.
Start and stop your service from services. It will log the start and stop enteries in LogFile as shown below.
3/10/2016 1:21:25 PM: Test window service started
3/10/2016 1:21:55 PM: Job is running
3/10/2016 1:22:25 PM: Job is running
3/10/2016 1:22:55 PM: Job is running
3/10/2016 1:23:25 PM: Job is running
3/10/2016 1:23:55 PM: Job is running
3/10/2016 1:24:25 PM: Job is running
3/10/2016 1:24:41 PM: Test window service stopped
Creating Windows Services
File – New Project – Select language as C# and windows service as application type.
Enter name for the project. Example: SampleWindowsService
Click on ok.
Right-click the "Service1.cs" file in Solution Explorer and rename it "to" Scheduler or any other name that you want to give it. Then click “click here to switch to code view”.
In the code view, you can see two methods called OnStart() and OnStop(). The OnStart() triggers when the Windows Service starts and the OnStop() triggers when the service stops.
Create a new class file(Library.cs) in the project with a method to log messages using below code.
Code in Library.cs file
using System;
Enter name for the project. Example: SampleWindowsService
Click on ok.
Right-click the "Service1.cs" file in Solution Explorer and rename it "to" Scheduler or any other name that you want to give it. Then click “click here to switch to code view”.
In the code view, you can see two methods called OnStart() and OnStop(). The OnStart() triggers when the Windows Service starts and the OnStop() triggers when the service stops.
Create a new class file(Library.cs) in the project with a method to log messages using below code.
Code in Library.cs file
using System;
using System.IO;
namespace SampleWindowsService
{
public static class Library
{
public static void WriteErrorLog(string Message)
{
StreamWriter sw = null;
try
{
sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory+"LogFile.txt",true);
sw.WriteLine(DateTime.Now.ToString() + ": " + Message);
sw.Flush();
sw.Close();
}
catch { }
}
}
}
Scheduler.cs
using System;
using System.ServiceProcess;
using System.Text;
using System.Timers;
namespace SampleWindowsService
{
public partial class Scheduler : ServiceBase
{
private Timer timer1 = null;
public Scheduler()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer1 = new Timer();
this.timer1.Interval = 30000;//every 30 secs
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
timer1.Enabled = true;
Library.WriteErrorLog("Test window service
started");
}
private void timer1_Tick(object sender, ElapsedEventArgs e)
{
//Write code her to do some scheduled task
Library.WriteErrorLog("Job is running");
}
protected override void OnStop()
{
timer1.Enabled = false;
Library.WriteErrorLog("Test window service
stopped");
}
}
}
Go to design view of Scheduler.cs – right click on the design window and select Add Installer.
Now ProjectInstaller.cs file will be added to your project in solution explorer.
Open ProjectInstaller design window – Right click on serviceInstaller1 – Select properties
Change StartType property to automatic so that service will be automatically started.
Open ProjectInstaller design window – Right click on serviceProcessInstaller1 – Select properties
Change Account property to LocalSystem.
Build the project and see the exe file created in project location.
Understanding Windows Services
Windows services are the applications with no GUI. They run in the background of windows operating system.
In Start menu - all programs search for Services to view all the installed services in the system. You can start or stop the services here.
They are used to execute a particular scheduled task continuously at regular intervals of time without user intervention.
Real time example: sending a newsletter everyday afternoon or send an email alert to the customer for every one hour.
In Start menu - all programs search for Services to view all the installed services in the system. You can start or stop the services here.
They are used to execute a particular scheduled task continuously at regular intervals of time without user intervention.
Real time example: sending a newsletter everyday afternoon or send an email alert to the customer for every one hour.
3 Layered Architecture
If an application is divided into 3 layers for better maintenance of project then it is known as 3 layered architecture.
There
are 3 layers in 3 layered architecture. They are:
1. Presentation layer
2. Business logic layer
3. Data access layer
1. Presentation layer
2. Business logic layer
3. Data access layer
Presentation layer
It
is the User interface through which user will communicate with the application.
It is the GUI part of the application which contains UI controls.
Business logic layer
Programming
logic will be present in business logic layer. It acts as interface between
presentation and data access layer.
Data Access layer
It
contains code to interact with the database and it gives information of
database to business logic layer.
Example:
create table
tbl_layers(sno int, name varchar(20),course varchar(20))
insert into
tbl_layers values(1,'sudhir','JAVA')
create procedure
Usp_Insert
@id int,
@name
varchar(20),
@course
varchar(20)
as begin
insert into
tbl_layers values(@id,@name,@course)
end
Presentation layer
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _3_layer_architecture
{
public partial class Form1 : Form
{
DAL dobj = null;
public Form1()
{
dobj
= new DAL();
InitializeComponent();
}
private void BtnInsert_Click(object sender, EventArgs e)
{
BAL balObj = new BAL();
balObj.Id = Convert.ToInt32(TxtId.Text);
balObj.Name=TxtName.Text;
balObj.Course = TxtCourse.Text;
int i=
dobj.insert(balObj);
}
}
}
Bussiness logic layer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _3_layer_architecture
{
class BAL
{
public int Id { get; set; }
public string Name { get; set; }
public string Course { get; set; }
}
}
Data access layer
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
namespace _3_layer_architecture
{
class DAL
{
SqlConnection con = new SqlConnection("Data Source=WINBC250150-JUL\\SQLEXPRESS;Initial
Catalog=master;Integrated Security=True");
public int insert(BAL obj)
{
con.Open();
SqlCommand cmd = new SqlCommand("Usp_Insert", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", obj.Id);
cmd.Parameters.AddWithValue("@name", obj.Name);
cmd.Parameters.AddWithValue("@course", obj.Course);
int i= cmd.ExecuteNonQuery();
con.Close();
return i;
}
}
}
Validations – Regular Expressions in Windows Forms Applications
Text given by user as input is validated before saving it in database tables on client side using Regular Expressions.
Regular Expressions are the patterns which are used to validate the text based on some conditions.
Regex is a class under System.Text.RegularExpressions namespace which is used to validate text based on the given pattern using IsMatch method.
IsMatch method will take string as input to validate with pattern and return true if the text is matched with the pattern or returns false if the text is not matched with the pattern.
Writing patterns
^ - start
$ - end
[a-z] – lower case letters are allowed
[A-Z] – Upper case letters are allowed
[0-9] – Numbers are allowed
[a-zA-Z] – both lower and upper case letters are allowed
[a-zA-Z0-9] – input can be lower case/upper case or numbers
{10} – only 10 characters is valid
{6,10} – 6 to 10 characters are valid
Examples:
Regular Expressions are the patterns which are used to validate the text based on some conditions.
Regex is a class under System.Text.RegularExpressions namespace which is used to validate text based on the given pattern using IsMatch method.
IsMatch method will take string as input to validate with pattern and return true if the text is matched with the pattern or returns false if the text is not matched with the pattern.
Writing patterns
^ - start
$ - end
[a-z] – lower case letters are allowed
[A-Z] – Upper case letters are allowed
[0-9] – Numbers are allowed
[a-zA-Z] – both lower and upper case letters are allowed
[a-zA-Z0-9] – input can be lower case/upper case or numbers
{10} – only 10 characters is valid
{6,10} – 6 to 10 characters are valid
Examples:
10 digit phone number - ^[0-9]{10}$
6 to 15 characters are valid which may
contain lower/upper letters -
^[a-zA-Z]{6,15}$
^[a-zA-Z]{6,15}$
Example to display popup with error message if text is
not a valid
//Validate text with given pattern and return true for
valid and false for invalid text
public bool TextValidate(string Text, string Pattern)
{
//Create regular expression object
with specific pattern
var regex = new Regex(Pattern);
//Check if text matched with
pattern
if (regex.IsMatch(Text))
{
return true;
}//if text not matched with pattern
else { return false; }
}
private void BtnRegister_Click(object
sender, EventArgs e)
{
//intialize errormsg as empty
string errormsg = string.Empty;
//Username - 6 to 15
chars(Upper/Lower) Ex: SudhirChekuri
if (!TextValidate(TxtUsername.Text, "^[a-zA-Z]{6,15}$"))
{
errormsg = "Invalid
Username\nShould be between 6 to 15 letters";
}
//Password and cnfrm pwd match
else if(TxtPassword.Text!=TxtCnfmPwd.Text)
{
errormsg = "Password
and Confirm Password Mismatch";
}//Email Id - ex: sudhir@gmail.com
else if
(!TextValidate(TxtEmailId.Text, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))
{
errormsg = "Invalid
Email Id";
}
//phone number - 10 digits
Ex:9985333333
else if
(!TextValidate(TxtPhoneNo.Text, "^[0-9]{10}$"))
{
errormsg = "Invalid
Phone Number\nEnter 10 digit number";
}
if (errormsg != string.Empty)
{
MessageBox.Show(errormsg);
}
}
Restaurant Management System project with ADO.NET and Database
Restaurant Management System is a windows application with two modules - Consumer and Admin.
SQL Server database is used to store data
Click here to download
SQL Server database is used to store data
Click here to download
Restaurant Management System Windows Application - Static Data
Restaurant Management System is a windows application with two modules - Consumer and Admin.
In this app all the data required for the app is static in nature ie., data is populated to controls from arrays and other collections.
Click here to download Project Source code with static data
Features:
Consumer:
- Register
- Login
- Order - categories, products, product details, order product
Admin
- Login
- Home page for navigation to different pages
- Registered Users
- Orders
- Manage Items - Add/Update/Delete category, Add/Update/Delete Products, Add details
In this app all the data required for the app is static in nature ie., data is populated to controls from arrays and other collections.
Click here to download Project Source code with static data
Features:
Consumer:
- Register
- Login
- Order - categories, products, product details, order product
Admin
- Login
- Home page for navigation to different pages
- Registered Users
- Orders
- Manage Items - Add/Update/Delete category, Add/Update/Delete Products, Add details
