Tuesday, March 9, 2010

Create a Windows Service in .net

Hi,
Following are the steps to create a basic windows service to write a file in .net:
include following namespaces:
using System.Text;
using System.IO;
using System.Timers;
Create a class level timer:
    private Timer timer = new Timer();
On Start Event:
    protected override void OnStart(string[] args)
    {
        timer.Elapsed += new ElapsedEventHandler(TimerElapsed);
        timer.Interval = 10000;
        timer.Enabled = true;
    }
Create Event handler:
    private void TimerElapsed(object source, ElapsedEventArgs e)
    {
        Logger();
    }
Create function:
    public void Logger()
    {
        string mydocument =
      Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        string[] txtList = Directory.GetFiles(mydocument, "*.txt");
        StringBuilder sb = new StringBuilder();
        string FileName = @"\log.txt";
        using (StreamReader sr = new StreamReader(mydocument + FileName))
        {
            sb.AppendLine(DateTime.Now.ToString());
            sb.AppendLine("---------------------------------------------");
            sb.Append(sr.ReadToEnd());
            sb.AppendLine();
        }
        using (StreamWriter outfile =
            new StreamWriter(mydocument + FileName))
        {
            outfile.Write(sb.ToString());
        }
    }


InstallUtil command to install the service:
    installutil ServicePath/Servicename.exe


To uninstall the service:
    installutil /u ServicePath/Servicename.exe

Friday, February 26, 2010

Custom Validator in JQuery.Validate

$.validator.addMethod(
"regexName",
function(value, element) {
var check = false;
var re = new RegExp('^[a-zA-Z _0-9.]+$');
return this.optional(element) || re.test(value);
},
"No special characters allowed!"
);


rules: {
LoginName: {
required: true,
regexName:true
},

Open fancybox on Input Click

1)
$(document).ready(function()
{
$("#BTN").click(function()
{
$('test desc').fancybox().click();
});
});


2)

$("#buttontoclick").click(function() {
$('Friendly description').fancybox({
overlayShow: true
}).click();
});


3)

        function OpenPage(id) {
            $('dummy_anchor').fancybox({
                'frameWidth': 420, 'frameHeight': 413 // 'frameHeight': 232
            }).click();
        }

JS file with Server Side Variable

Javascript object and class concept

<_script_start>
var path = {
currentUrl: "",
getPath: function() {
return this.currentUrl;
},
TEST: function() {
alert(this.currentUrl);
}
}

script type="text/javascript">
path.currentUrl = '';
path.TEST();

authorized.net complete

after including previous four classes call this function on button click




private void DoPayment()
{
AuthorizeNetRequest objAuthorizeNetRequest = new AuthorizeNetRequest();

// This is the account information for merchant account given by Authorize.Net people in email
// I can see transaction history here.

objAuthorizeNetRequest.Login = LOGINID;
objAuthorizeNetRequest.Amount = Convert.ToDouble(txtAmount.Text);
objAuthorizeNetRequest.CardNumber = txtCreditCardNo.Text;
objAuthorizeNetRequest.CardExpirationDate = drpExpirationDate.SelectedValue + drpYear.SelectedValue.Substring(2);

objAuthorizeNetRequest.TransactionType = AuthorizeNet.TransactionType.AUTH_CAPTURE;

///transaction types
///default is auth capture


//objAuthorizeNetRequest.TransactionType = AuthorizeNet.TransactionType.AUTH_ONLY;
//objAuthorizeNetRequest.TransactionType = AuthorizeNet.TransactionType.CREDIT;
//objAuthorizeNetRequest.TransactionType = AuthorizeNet.TransactionType.VOID;

///we can use other types but will have to provide transaction id if we are going to
///PRIOR_AUTH_CAPTURE type as this will capture prior authorized transaction.

//objAuthorizeNetRequest.TransactionId = "";
//objAuthorizeNetRequest.TransactionType = AuthorizeNet.TransactionType.PRIOR_AUTH_CAPTURE;

objAuthorizeNetRequest.CcvNumber = txtCCV.Text;

// Below is the API created by me by registering for test account.
objAuthorizeNetRequest.TransactionKey = TRANSACTION_KEY;
AuthorizeNetFields allFields = new AuthorizeNetFields();

allFields.x_First_Name = txtCardName.Text;
allFields.x_Address = "";
allFields.x_City = "";
allFields.x_State = "";
allFields.x_Country = "U.S.A";
allFields.x_Phone = "";
allFields.x_Zip = "";
allFields.x_Tax = "";
allFields.x_Email = "";
allFields.x_Description = "";

allFields.x_Ship_to_first_name = txtCardName.Text;
allFields.x_Ship_to_address = "";
allFields.x_Ship_to_city = "";
allFields.x_Ship_to_state = "";
allFields.x_Ship_to_country = "U.S.A";
allFields.x_Ship_to_phone = "";
allFields.x_Ship_to_zip = "";

AuthorizeNetResponse objAuthorizeNetResponse = AuthorizeNet.CallAuthorizeNetMethod(objAuthorizeNetRequest, allFields);
if (objAuthorizeNetResponse.IsSuccess)
{
Response.Write(objAuthorizeNetResponse.SuccessMessage +" ID:"+ objAuthorizeNetResponse.TransactionId);
}
else
{
Response.Write("Error : " + objAuthorizeNetResponse.Errors);
}
}