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.

Sunday, April 4, 2010

Send Long SMS/Multipart SMS/Concatenated SMS

Here I am writing about most awaited post of my blog with great excitement. In this post I will explain you , how to send long SMS or multipart SMS or concatenated SMS. Before I explain you I want to share my experience during searching for the secret of long SMS. When I started searching on internet about “how to send concatenated sms”, I found resource on wiki which explains the basic of long SMS and PDU format for sending long SMS. But this page (wiki) contains link at bottom of the page and this link (trust me) one of the best resource on the internet to learn about concatenated SMS. Here is the link if you want to read this blog : http://mobiletidings.com/2009/02/18/combining-sms-messages/. But still I am having same problem not able to send long SMS using AT command. But reading this blog I am sure that I am missing something in my PDU which cause problem. What I have done is I have contacted Franco (owner of http://www.franco7777.net/), who is one of the commenter on “http://mobiletidings.com/2009/02/18/combining-sms-messages/” blog. He gave me very nice response and explain me where my mistake is. And after that I have got the clear idea about sending long SMS over network.


Now I want to show you how you can send long SMS. But before reading this post further I will strongly recommend you to read http://www.dreamfabric.com/sms/ and grab basic idea about SMS PDU structure. When you send single SMS ( <= 160 Character) there is not UDH exist in the message but when you try to send long SMS ( > 160 Character) you need to add this UDH field into the message PDU. So the basic question is how to add this header in to the message. One more thing you should know to send concatenated SMS, and the thing is when you send single SMS there is no User Data Header so little less confusion and you don’t need to add padding bit to encode your message into GSM 7-bit encoding scheme but if you are going to send concatenated SMS, UDHL is exist and it is in septets so you need to add one padding bit compulsory to encode your SMS. Read http://mobiletidings.com/2009/02/18/combining-sms-messages/ blog to get the clear idea because I don’t want to post same content again in this post.


So basically there are 2 secret to send long SMS

  1. Add User Data Header

  2. Add Padding bit to encode your message.


If I want to send

Message:

  • Message 1: “This is Part 1.

  • Message 2: “This is Part 2.


Message PDU should be

[caption id="attachment_166" align="alignnone" width="544" caption="Message 1 - PDU"]Message 1 - PDU[/caption]

[caption id="attachment_165" align="alignnone" width="544" caption="Message 2 - PDU"]Message 2 - PDU[/caption]

That’s it , you are done !!

Things to know before sending Long SMS is , UDH will occupy 6 octets so original message size should be reduce.

Please let me know if I am missing something so I can update this post.


Send Long SMS/Multipart SMS/Concatenated SMS

Send Long SMS/Multipart SMS/Concatenated SMS

Here I am writing about most awaited post of my blog with great excitement. In this post I will explain you , how to send long SMS or multipart SMS or concatenated SMS. Before I explain you I want to share my experience during searching for the secret of long SMS. When I started searching on internet about “how to send long sms”, I found resource on wiki which explains the basic of long SMS and PDU format for sending long SMS. But this page (wiki) contains link at bottom of the page and this link (trust me) one of the best resource on the internet to learn about concatenated SMS. Here is the link if you want to read this blog : “http://mobiletidings.com/2009/02/18/combining-sms-messages/”. But still I am having same problem not able to send long SMS using AT command. But reading this blog I am sure that I am missing something in my PDU which cause problem. What I have done is I have contacted Franco (owner of “http://www.franco7777.net/”), who is one of the commenter on “http://mobiletidings.com/2009/02/18/combining-sms-messages/” blog. He gave me very nice response and explains me where my mistake is. And after that I have got the clear idea about sending long SMS over network.

Now I want to show you how you can send long SMS. But before reading this post further I will strongly recommend you to read “http://www.dreamfabric.com/sms/” and grab basic idea about SMS PDU structure. When you send single SMS ( <= 160 Character) there is not UDH exist in the message but when you try to send long SMS ( > 160 Character) you need to add this UDH field into the message PDU. So the basic question is how to add this header in to the message. One more thing you should know to send concatenated SMS, and the thing is when you send single SMS there is no User Data Header so little less confusion and you don’t need to add padding bit to encode your message into GSM 7-bit encoding scheme but if you are going to send concatenated SMS, UDHL is exist and it is in septets so you need to add one padding bit compulsory to encode your SMS. Read “http://mobiletidings.com/2009/02/18/combining-sms-messages/” blog to get the clear idea because I don’t want to post same content again in this post.

So basically there are 2 secret to send long SMS

  1. Add User Data Header

  2. Add Padding bit to encode your message.


If I want to send

Message:

Message 1: “This is Part 1.

Message 2: “This is Part 2.

Message PDU should be

******Image*******

That’s it , you are done !!

Things to know before sending Long SMS is , UDH will occupy 6 octets so original message size should be reduce.

Please let me know if I am missing something so I can update this post.

Sunday, December 20, 2009

Avoid Using Updatepanel

Updatepanel is part of AJAX Extension 1.0 and later provided by Microsoft. Update panel is used to hide page refresh using some standard mechanism. But What about speed? In my experience updatepanel is the best solution if your page does not contain large number of components. And make sure controls you used is supported by updatepanel. Because controls like GridView,Treeview and more , are not supported by updatepanel. So you can expect behaviours change if you put it inside updatepanel.


I have recently trusted on updatepanel and used in my page where speed is the primary concern and that page was almost dead. So avoid using updatepanel where speed is primary requirement.


I have used Prototype AJAX in my application. It’s faster and better. Give it a try. I tried to use jQuery AJAX but it has not that much support for ASP.net 2.0, its very good if you working on ASP.net 3.0 or ASP.net 3.5 version of .net.


So the conclusion is , avoid using updatepanel to get better speed , insted you can use Prototype or jQuery AJAX to get the best speed and performance of your application. Googling around the web and you will get many working example of protory and jquery.

Saturday, September 19, 2009

MySQL With .Net 2.0/3.0/3.5

MySQL is open source database. We had always seen MySQL with PHP. Generally we know MySQL is database engine for PHP. It’s true because .net does not have inbuilt support for MySQL database. We need to install other tools to make it work perfectly. In order to work with MySQL in .net you need to install MySQL Server and MySQL Connector. You can create function and procedure in MySQL version 5.1 and later. At the same time you will get great user interface to manage database. You will get interface just like MS SQL server management studio. MySQL Connector is used to add database connectivity support in Visual Studio. Download Connector depending upon the .net framework you use and Visual studio you work on. Version 5.1 GA (Generally Available) and later is recommended for .net framework 2.0 and higher.



Problems I faced:

  1. Syntax of MySQL is very different from MS SQL server.

  2. Error console of MySQL is not that user friendly.

  3. Migration of MS SQL Database to MySQL is quite difficult.

  4. Because it’s not Microsoft product you won’t find any inbuilt programming level support. Ex. It’s really hard to work use LINQ if your backend is MySQL.


Download Latest MySQL Database Server and Connector (Click Here).