Thursday, March 8, 2012

jQuery HTML5 Signature Pad

Recently I come across situation where I need to provide browser based Signature Pad and It should work fine in all the devices including apple devices. So I don't have any other option left then HTML5 Canvas.

If you want to create a browser based signature pad then visit the link below. http://thomasjbradley.ca/lab/signature-pad/ To save signed signature visit the link below. (CODE IS IN C#)  

Dealing with DateTime in C#

Recently I come across very ugly error which makes my application crashed. Actually it was problem converting DateTime for specific format. You should make your DateTime conversion independent of client computer’s culture setting.  This thought in mind I am going to present this post.

1.       Convert String  to DateTime

Let’s take an example:

Suppose you have
 String strDate = "10/23/2011"; 

Which is in MM/dd/yyyy format and you want to convert it in DateTime variable then you should write like
 DateTime dt = Convert.ToDateTime(strDate,System.Globalization.CultureInfo.InvariantCulture); 

You could write above code if you are sure that the format is in MM/dd/yyyy. If the format is in say dd/MM/yyyy then DatTime.ParseExact is the perfect option for you.

Code above can be rewritten as
 DateTime dt = DateTime.ParseExact(strDate, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture); 
In all above code lines I have mentioned System.Globalization.CultureInfo.InvariantCulture parameter, which will make date format, culture independent.

2.       Convert Datetime to Specific Format

Let’s say you have
 DateTime dt = DateTime.Now; 
and you want to convert it in different format. 

Example : Format : dd-mmm-yyyy
 String s = dt.ToString("dd/MMM/yyyy"); 

But this will be overridden by current culture.

So you should do something like this
String s = dt.ToString("dd/MMM/yyyy", System.Globalization.CultureInfo.InvariantCulture); 

In short, CultureInfo parameter plays an important role.

Run Executable (exe/bat) file from Web Page

To run an exe from your web page is quite easy process. I have used simple JavaScript to accomplish this task. Have a look at the code listed below.

function WriteToFile() {

var fso, s,folderName;

fso = new ActiveXObject("Scripting.FileSystemObject");

if (!fso.FolderExists("C:\\HITESH"))

{

folderName=fso.CreateFolder("C:\\ HITESH ");

}

if (!fso.FileExists("C:\\ HITESH \\LaunchApp.bat")){

s = fso.OpenTextFile("C:\\ HITESH \\LaunchApp.bat" , 8, 1, -2);

s.writeline("IF EXIST d:\\hitesh GOTO LABEL1”);

s.writeline("IF EXIST k:\\hitesh GOTO LABEL2”);

s.writeline(":LABEL1”);

s.writeline("D: ”);

s.writeline("D:\\hitesh\\AppTest.EXE”);

s.writeline("EXIT”);

s.writeline(":LABEL2”);

s.writeline("K: ”);

s.writeline("K:\\hitesh\\AppTest.EXE”);

s.writeline("EXIT”);

s.Close();

}

}

function LaunchApp() {

WriteToFile();

var oShell = new ActiveXObject("WScript.Shell");

var prog = "C:\\ HITESH \\LaunchApp.bat";

oShell.run ('"'+prog+'"',1);

}
This javascript will create an object of WScript.Shell and will execute DOS based batch file. In addition with this I have also created LaunchApp.bat file programmatically if it does not exist on the client side (So you don’t need to worry about whether file exist or not J). This file will execute EXE depending upon conditions specified in the LaunchApp.bat. LaunchApp() is the starting point.

Encode Text Message to PDU

Text Message must be encoded properly into PDU before submitting it to network. [Note: this is applicable if you are going to send SMS in PDU mode]. In this post I am going to explain how you can do this, but before that please download sample code from Planet Source code. This code contains Convert, Sending and Receiving modules. Frankly speaking I have got encoding/decoding logic from this code and it works perfectly for me. Only thing missing in this code is Padding Bit logic. Code is quite simpler and easily understandable. Please go through it and tell me if you have problem encoding text message into PDU.

How to add Padding bit for multipart SMS PDU?

Open the code > go to Convert Module > Find “CharHex”  Method.
Do replace this function with the code shown below.

Public Function CharHex(ByVal Txt As String, ByVal bit As Integer, ByVal PaddingBit As Boolean)
Dim i As Integer, bin As String, nbin As String, n As String
Dim bil As Integer, sisa As Integer, lbin As Integer, nol As String
bin = ""
nbin = ""
If bit = 7 Then
For i = 1 To Len(Txt) Step 2
n = Mid(Txt, i, 2)
bin = HexToBin(n) & bin
Next
bil = Len(bin) \ bit
sisa = Len(bin) Mod bit
For i = 1 To (Len(bin) - sisa) Step bit
' MsgBox Chr$(HexToDec(BinToHex(Mid(bin, i + Sisa, bit))))
nbin = Chr$(HexToDec(BinToHex(Mid(bin, i + sisa, bit)))) & nbin
Next
Else
For i = 1 To Len(Txt)
n = Mid(Txt, i, 1)
bin = Biner(Asc(n)) & bin
Next
If PaddingBit = True Then
bin = bin + "0"
End If
sisa = Len(bin) Mod bit
If sisa > 0 Then
For i = 1 To bit - sisa
nol = nol & "0"
Next
End If
bin = nol & bin
bil = Len(bin) \ bit
For i = 1 To bil
nbin = nbin & BinToHex(Mid(bin, Len(bin) + 1 - bit * i, bit))
Next
End If
CharHex = nbin
End Function


That's it !!

 Do let me know if you have any problem encoding text to PDU.