Feeds:
Posts
Comments

Archive for the ‘.net’ Category

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]);

}

Advertisements

Read Full Post »

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.

Read Full Post »

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.

Read Full Post »

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

Read Full Post »

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

Read Full Post »

Most of us use Microsoft AJAX Toolkit and Controls but we don’t know when Ajax Request starts and when it ends. Sometimes we need to do enabling and disabling controls during postback. This can be done using the code below.

var _employee=null;
var _department=null;
var _subdepartment=null;

function pageLoad(sender, args){
// Register start request and end request event handler Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequest); }

// fired when ajax request starts
function beginRequest(sender, args){
_ employee = $get('ctl00_ContentPlaceHolder1_ddlemployee);
_ department = $get('ctl00_ContentPlaceHolder1_ddldepartment);
_ subdepartment = $get('ctl00_ContentPlaceHolder1_ddlsubdepartment);
_ employee.disabled=true;
_ department.disabled=true;
_ subdepartment.disabled=true; }

// fired when ajax request ends
function endRequest(sender, args) {
_ employee.disabled=false;
_ department.disabled=false;
_ subdepartment.disabled=false;
}

Here I have added two event handlers to request manager class instance. Event will be fired when request starts or ends. Isn’t it easy? 🙂

Ref : http://mattberseth.com/blog/

Read Full Post »

In this article I am going to explain how you can use at commands for mobile application development. Before I am going to explain AT command I want to share some knowledge with you. AT commands are for GSM modem only so in order to execute at command you need mobile with GSM modem. Each mobile has either a Text mode or PDU mode or both. Depend upon the different mobile manufacture. So there are different at commands for different modes.

How can you execute AT Command ?
You can execute AT commands by programmatically or by HyperTerminal.
Go to Start >> Programs >> Accessories >> Communications >> HyperTerminal

And then connect your mobile using serial port and start play with AT commands.

If you want to develop a mobile application then you should go with C/C++, VB, VB.net, C#.net. My choice of language is C#.net.

.net has a class for serial port communication which you can use directly and built your application. You need the deep knowledge of threading and delegate to develop such kind of application.
Capabilities of AT commands are seamless. By using AT commands you can send SMS,Receive SMS, Call etc… In short you can handle all your mobile handset function by using your PC.
AT commands for Text Mode :
AT+CSMS Select Message Service
AT+CPMS Preferred Message Storage
AT+CMGF Message Format
AT+CSCA Service Centre Address
AT+CSMP Set Text Mode Parameters
AT+CSDH Show Text Mode Parameters
AT+CSCB Select Cell Broadcast Message Types
AT+CSAS Save Settings
AT+CRES Restore Settings
AT+CNMI New Message Indications to TE
AT+CMGL List Messages
AT+CMGR Read Message
AT+CNMA New Message Acknowledgement to ME/TA
AT+CMGS Send Message
AT+CMSS Send Message from Storage
AT+CMGW Write Message to Memory
AT+CMGD Delete Message

AT commands for PDU Mode :
AT Attention command
AT+CMGL List message
AT+CMGR Read message
AT+CMGS Send message
AT+CMSS Send from storage
AT+CMGW Write message to memory
AT+CMGD Delete message
You can download complete AT command list from the links below.

For text mode :
 Download PDF
For pdu mode :
Download PDF
These two files contain the detailed information on what are the commands for Mobile Handset and what will be the output of each command. It is easy believe me !!
 
NOTE : all the content I wrote is related to mobile application development using AT commands. AT commands are very useful for serial port communication with attached device on serial port.

Read Full Post »