Friday, August 15, 2008

JSonSerialization Error Solution

Sometime when working with ScriptSevice and ScriptMethod in ASP.net webservice following error may occur.

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

Reason for this is JavaScript Serialization setting . This allow maximum 2097152 character by default. It method return string larger then this then it throw above error.

Solution for this is to put following configuration in web.config file.

<connectionStrings>
</connectionStrings>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="500000"></jsonSerialization>
</webServices>
</scripting>
</system.web.extensions>




In above configuration instead you can set maxJsonLength to certain value so it allow larger string.

Maximum value for that field is 2147483647 as this property is System.Int32.

More information you can find it on following location.
http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.maxjsonlength.aspx

Wednesday, August 13, 2008

bool vs bitvector32 in C#

In application sometime you need more boolean fields.Also that memory usage and performance is main concern.

For example you require 50 boolean fields. If you create 50 boolean variable
like
bool b1,b2,b3....b50;
or
create array of 50 than bool[] b = new bool[50]; then also each bool veriable require 1 byte. so total space usage 50 * 1 = 50bytes.

If you use bitvector32 instead of creating array or 50 variable then it just require 4byte. Each bitvector32 can hold upto 32 different flags.

so for 50 we just need two bitvector32,and space usage reduce to 8 byte instead of 50 byte.


BitArray is also provide functionality like bitvector32 but it is class so it overhead with instance of class, but bitvector32 is simple structure so it reduce that over head too.

Example:

BitVector32 bv = new BitVector32(0); This will assign all bits to 0, as 0 binary representation is 00000000000000000000000000000000.

BitVector32 bv = new BitVector32(-1); This will assign all bits set to 1. As –1 binary representation is 11111111111111111111111111111111. (Two’s complement).

You can choose BitArray over BitVector32 when size of array like 40 , 48. That is not in multiple of 32. ( This is just a suggestion).

More information you can find at following location.
http://msdn.microsoft.com/en-us/library/system.collections.specialized.bitvector32.aspx
http://msdn.microsoft.com/en-us/library/eahchzkf.aspx(C# ref)

Tuesday, August 12, 2008

Find distinct value from DataSet/DataTable in .NET

Suppose you have dataset that contain data in following manner

ID Name Country
----------------------------
1 X USA
2 Y INDIA
3 Z INDIA
This is a DataTable name EmpInfo.

Now you want to select distinct country value from above table.

DataTable country = EmpInfo.DefaultView.ToTable(true,"Country");

Now country datatable contain distinct country value from EmpInfo Table.

You get more information from following link.
http://msdn.microsoft.com/en-us/library/wec2b2e6.aspx

Tuesday, July 1, 2008

Custom SessionID In ASP.net 2.0

By default asp.net 2.0 create sessionid like this kidt1m55iyqcx2vrtkn3z0ba. But requirement is such that you need to use your own algorithm to generate sessionid. For this pupose you have to create class that inherit from SessionIDManager class and override it CreateSessionID and Validate methods.
For Example : I want to use GUID as sessionid .
public class CustomSessionID : System.Web.SessionState.SessionIDManager
{
public override string CreateSessionID(HttpContext context)
{
return Guid.NewGuid().ToString();
}
public override bool Validate(string id)
{
try
{
Guid idtest = new Guid(id);
if (idtest.ToString() == id)
return true;
}
catch
{
return false;
}
return false;
}
}

You can also use DateTime.Now().Ticks as SessionID, or any custom string as a sessionid.

This is very useful case when you want to functionality like same sessionid generate for sameuser.

You also have to set web.config sessionStat Section in following manner. Here i assume that above class present in App_code directory . but you can create class library and refrence that class in sessionState session.

web.config setting.

<sessionState sessionIDManagerType="CustomSessionID,App_code" >
</sessionState>