Wednesday, August 31, 2016

How to Debug the Window service in Dotnet both C# and VB.Net

There Two ways you can debug the service

 First :

 Go to Service.Designer.vb page find below mentioned code and comment mentioned places, later create new instance of the class and call the main method check below code.
 --- VB.NET
 ' The main entry point for the process
    <MTAThread()> _
    <System.Diagnostics.DebuggerNonUserCode()> _
    Shared Sub Main()
        Comment/Uncomment --'Dim ServicesToRun() As System.ServiceProcess.ServiceBase

        ' More than one NT Service may run within the same process. To add
        ' another service to this process, change the following line to
        ' create a second service object. For example,
        '
        '   ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
        '
        Comment/Uncomment --'ServicesToRun = New System.ServiceProcess.ServiceBase() {New UniDocBulkImportService}

        Comment/Uncomment --'System.ServiceProcess.ServiceBase.Run(ServicesToRun)

Dim objClsMain as New ClsMain() -- Create new instance for your main class in your service
objClsMain.MainMethod()         --- Call the Main method in above class where service start processing

    End Sub

 --- C#.NET
Go to Program.cs page find below mentioned code and comment mentioned places, later create new instance of the class and call the main method check below code.
static void Main()
        {
           Comment/Uncomment // ServiceBase[] ServicesToRun;
           Comment/Uncomment // ServicesToRun = new ServiceBase[]
            Comment/Uncomment //{
            Comment/Uncomment //    new Unidoc_CHAD_IN_Service()
           Comment/Uncomment // };
           Comment/Uncomment // ServiceBase.Run(ServicesToRun);
         clsMain ObjClsMain =new clsMain(); -- Create new instance for your main class in your service
ObjClsMain.MainMethod();  --- Call the Main method in above class where service start processing
         
        }
Second :

 Add new windows form to your service project right click on project and go to properties
 under ApplicationType ---> select WindowsFormApplication
 and UnderStartup project select Form1 so directlry debugger starts from Form1.
 same C# also.

Difference between Abstract Class and Interface

What is an Abstract Class?

An abstract class is a special kind of class that cannot be instantiated. it allows other classes to inherit from it. In simple words, if you inherit this class need to implement methods in this class.
Ex: Like in your project you have all the EDIT,DELETE,UPDATE and SELECT methods, if you want maintain to same hierarchies for all the classes then place these methods in abstract class and inherit this class in all your modules.
Real life example of Abstract : Mobile,TV,Watch and human body...... etc.


What is an Interface?

An interface is not a class. An interface has no implementation; Method without body, As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses.


Difference between them is
that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.

Abstract class  : static methods, main method and constructor.

Interface: no static methods, main method or constructor.

Tuesday, June 28, 2016

Validation with Regular expression


Validation code behind using Regularexpression class Regex 
//Calling method
value  = yourvalue(string,int or Email)
bool bCheck = NumberValidation(value);  - Calling method
bCheck  -- Return Value
if (bCheck == false)
{
 //not matching
}
Else
{
 //matching

}

1. Number validation  - Only numbers (1,2,3,...)

private bool NumberValidation(string value)
{
Regex regex = new Regex("^[0-9]*$");
if (regex.IsMatch(value))
{
countCheck = 0;
return true;
}
else
{
if (countCheck == 1 && tPalceholdervalue != "")
{
countCheck = 0;
MessageBox.Show("Only numbers may be entered here", "Restriction");
return false;
}
return false;
}
}

2. Alphanumeric  (combination numbers and alphabets )

private bool AlphNumeric(string value)
{
Regex regex = new Regex(@"[a-zA-Z0-9/'.]{1,40}");
if (regex.IsMatch(value))
{
countCheck = 0;
return true;
}
else
{
if (countCheck == 1 && tPalceholdervalue != "")
{
countCheck = 0;
MessageBox.Show("No Special Characters", "Restriction");
return false;
}
return false;
}
}

3.Email Validation

private bool EmailCheck(string value)
{

Regex regex = new Regex(@"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$");
if (regex.IsMatch(value))
{
countCheck = 0;
return true;
}
else
{
if (countCheck == 0 && tPalceholdervalue != "")
{
countCheck = 1;
MessageBox.Show("Ex: yourname@email.com", "Restriction");
return false;
}
return false;
}
}

4. Name Validation
private bool NameValidation(string value)
{
value = value.TrimEnd();
Regex regex = new Regex(@"^[0-9a-zA-Z ]+$");
if (regex.IsMatch(value))
{
countCheck = 0;
return true;
}
else
{
if (countCheck == 1 && tPalceholdervalue != "")
{
countCheck = 0;
MessageBox.Show("Special Characters are not allowed", "Restriction");
return false;
}
return false;
}
}

5. Date Validation 
private bool DateValidation(string value)
{
Regex regex = new Regex(@"\d{1,2}\/\d{1,2}/\d{4}");
if (regex.IsMatch(value))
{
countCheck = 0;
return true;
}
else
{
if (countCheck == 1 && tPalceholdervalue != "")
{
countCheck = 0;
MessageBox.Show("Date format : MM/DD/YYYY", "Restriction");
return false;
}
return false;
}
}

6. Boolean Validation

private bool BooleanValidation(string value)
{
if (value.ToUpper() == "YES" || value.ToUpper() == "NO")
{
countCheck = 0;
return true;
}
else
{
if (countCheck == 1 && tPalceholdervalue != "")
{
countCheck = 0;
MessageBox.Show("Ex: Yes or No", "Restriction");
return false;
}
return false;
}
}