Feeds:
Posts
Comments

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#)

http://www.dotnetfunda.com/articles/article1662-saving-html-5-canvas-as-image-on-the-server-using-aspnet.aspx

 

Download Source  [My new blog Address]
Let me know in case of any problem.

Recently I have observed that listbox scrollbar has virtualization problem when we re-bind listbox. Let me explain this with an example.

I have added 50 items into listbox control programmatically. Here is the screenshot for the same.

Image after 50 Items bound to listbox

Normal Listbox Binding

Re-bind listbox

Re-bind button click without scroll bar move

As you can see I have added two buttons to Reset and to Re-bind listbox again.

If I click on any of the button everything will work as expected but real problem is when we move scroll bar to bottom of the listbox and we re-bind listbox data.

See what kind of problem you shall face.

Problem displaying data

Solution :

Use scrolltoview function after assigning ItemsSource to listbox. See code snippet below.

 List<ListBoxItem> a=GetItemsList(txt,count);

listbox1.ItemsSource = a;

if(a.Count()>0)

{

listbox1.UpdateLayout();

listbox1.ScrollIntoView(a[0]);

}

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.

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.

I search a lot in Google for a whole day but didn’t find any perfect example to populate data into gridview. Then I decided to do it myself and I find it easy at the end of the day.

In the example which I illustrated below follows the flow:

  1. On document ready method it initiates jQuery AJAX call.
  2. Response will be redirected to the ASPX page and in tern server will respond with JSON data.
  3. jQuery AJAX call back success method used to generates HTML Table with data.
  4. Add HTML to DIV.

Here is the code:

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Untitled Page</title>

<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>

<script src="js/commons.js" type="text/javascript"></script>

</head>

<body>

<form id="form1" runat="server">

<div id="divperson">

<strong>[Newly generated HTML will seat here]</strong>

</div>

</form>

</body>

</html>

Default.aspx.cs

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

if (Request.QueryString["id"] == "person")

GetString();

}

}

public void GetString()

{

JavaScriptSerializer _serializers = new JavaScriptSerializer();

string _personJSON = _serializers.Serialize(Person.GetPersonDetails());

Response.Clear();

Response.Write(_personJSON);

Response.End();

}

}

<strong>//Person Class and Members</strong>

public class Person

{

public string Name { get; set; }

public string State { get; set; }

public string PhoneNumber { get; set; }

public Person(string name, string state, string phone)

{

Name = name;

State = state;

PhoneNumber = phone;

}

public static List<Person> GetPersonDetails()

{

List<Person> _person = new List<Person>();

_person.Add(new Person("Hitesh", "Gujarat", "123"));

_person.Add(new Person("Urvish", "Maharastra", "345"));

_person.Add(new Person("Deepak", "Madhya Pradesh", "456"));

return _person;

}

}

I just created Person class, created some properties and a static method to generate list of Person class objects. You need to include System.Collections.Generic and System.Web.Script.Serialization name space for List Class and JavaScriptSerializer class respectively.

In Page_Load method, based on the querystring value the GetString() method will call and JSON response will be generated and submitted as response. You need to catch this response on client side to retrive values within JSON formatted data.

commons.js (a separate JS file)

$('document').ready(function(){

$.ajax({

type: "GET",

data:({id:"person"}),

url: "Default.aspx",

dataType: "json",

success:function(resultdata){

var person;

var _html='<table border="1">';

_html += '<tr>';

_html += '<th>Name</th>';

_html += '<th>State</th>';

_html += '<th>Phone</th>';

_html += '</tr>';

for(index in resultdata)

{

_html +='<tr>' ;

_html +='<td>' + resultdata[index].Name + '</td>';

_html +='<td>' + resultdata[index].State + '</td>';

_html +='<td>' + resultdata[index].PhoneNumber + '</td>';

_html +='</tr>' ;

}

_html +='</table>' ;

$('#divperson').html(_html);

}

});

});

Nothing fancy, using jquery.ajax method I made a GET request to Default.aspx page with some data. Page has return JSON data which I parsed in jquery.ajax success method.

Before you attempt to run this code in your app, make sure you have included jquery-1.4.2.min.js file.

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.

Update:

I am getting lot or queries regarding Message PDU for single message and concatenated message. So What I have done is I have created a quick sample to generate PDU for single message and for multipart SMS.

Go to my new website www.hiteshagja.com to download sample code

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 was having same problem not able to send long SMS using AT command. But reading this blog I was sure that I was missing something in my PDU which cause problem. Then I thought of taking help from someone who is master in this so I did contact 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 explained me where my mistake was.  And after that I had 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

Message 1 - PDU

Message 1 – PDU

Message 2 - PDU

Message 2 – PDU

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.

Update:

I am getting lot or queries regarding Message PDU for single message and concatenated message. So What I have done is I have created a quick sample to generate PDU for single message and for multipart SMS.

Go to my new website www.hiteshagja.com to download sample code

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.

Follow

Get every new post delivered to your Inbox.