Saturday, March 26, 2011

how to find application root dynamically

sometimes you might have experienced some of the images are not appearing on your web page because of inappropriate path. so you need to know the root of the application dynically, because that keep chaning from development server to testing server, testing server to production server.

so you just need to do the following.

public static string ApplicationRoot
{
get
{
string root = null;
if (HttpContext.Current.Request.ApplicationPath != "/")
root = HttpContext.Current.Request.Url.GetLeftPart(System.UriPartial.Authority) + "/" + HttpContext.Current.Request.ApplicationPath;
else
root = HttpContext.Current.Request.Url.GetLeftPart(System.UriPartial.Authority) + "/";

if (!root.EndsWith("/"))
root += "/";

return root;
}
}


now whenever you are adding some image in any usercontrol, and you want to make sure the image is appearing allover the places then just use the following code

< img src='< %=ApplicationRoot +"Images/myImage.jpg"% >' >

Friday, March 25, 2011

JSON with extention method, Json Serialization

Extension method allows you to add new method to the existing class without modifying the code, recompiling or modifying the original code.

public static class ExtensionsMethods
{

public static string Reverse(this string content)
{
char[] charArray = new char[content.Length];
int len = content.Length - 1;
for (int i = 0; i <= len; i++)
{
charArray[i] = content[len - i];
}
return new string(charArray);
}


/*Now suppose you want to have some functionality ToJason from all your custom object, you dont have to modify the source code, in the extention method class you just need to add the following line.*/

public static string ToJASON(this object myObject)
{

// you need reference of System.Web.Script.Serialization;
JavaScriptSerializer js = new JavaScriptSerializer();

return js.Serialize(myObject);
}
}


Now lets look at how to use the method

Suppose you have a object called Client

Client client = new Client();
client.Address = "Client's House";
client.ClientCode = "000C123";
client.ClientId = 98;
client.ClientName = "Client the God";
client.Email = "ac@gmail.com";

string jsonClient = client.ToJASON();
Response.Write(jsonClient);

the json out put will be :

{"ClientId":98,"ClientName":"Client the God","ClientCode":"000C123","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":null,"Email":"ac@gmail.com","Address":"Client\u0027s House"}

// now get a list of client. and make a jasonstring.
string jsonClients = MasterDTO.GetClients().ToJASON() ;
Response.Write(jsonClients);

the output will be
"}[{"ClientId":0,"ClientName":" Client - 0","ClientCode":"0010","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 0Client - 0","Email":" 0Client0@gmail.com","Address":null},{"ClientId":1,"ClientName":" Client - 1","ClientCode":"0011","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 1Client - 1","Email":" 1Client1@gmail.com","Address":null},{"ClientId":2,"ClientName":" Client - 2","ClientCode":"0012","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 2Client - 2","Email":" 2Client2@gmail.com","Address":null},{"ClientId":3,"ClientName":" Client - 3","ClientCode":"0013","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 3Client - 3","Email":" 3Client3@gmail.com","Address":null},{"ClientId":4,"ClientName":" Client - 4","ClientCode":"0014","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 4Client - 4","Email":" 4Client4@gmail.com","Address":null},{"ClientId":5,"ClientName":" Client - 5","ClientCode":"0015","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 5Client - 5","Email":" 5Client5@gmail.com","Address":null},{"ClientId":6,"ClientName":" Client - 6","ClientCode":"0016","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 6Client - 6","Email":" 6Client6@gmail.com","Address":null},{"ClientId":7,"ClientName":" Client - 7","ClientCode":"0017","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 7Client - 7","Email":" 7Client7@gmail.com","Address":null},{"ClientId":8,"ClientName":" Client - 8","ClientCode":"0018","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 8Client - 8","Email":" 8Client8@gmail.com","Address":null},{"ClientId":9,"ClientName":" Client - 9","ClientCode":"0019","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 9Client - 9","Email":" 9Client9@gmail.com","Address":null},{"ClientId":10,"ClientName":" Client - 10","ClientCode":"00110","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 10Client - 10","Email":" 10Client10@gmail.com","Address":null}]


Similarly you also can convert the JSON string back to an object form by using the following extention method.

public static object JASONToObject(this string myObjectString)
{
JavaScriptSerializer js = new JavaScriptSerializer();

return js.DeserializeObject(myObjectString);
}

Friday, March 11, 2011

Server 2008 R2 Installation problem

Installing Microsoft SQL Server 2008 R2 Management Studio Express is become really pain. while installing getting the following error.

Rule "Previous releases of Microsoft Visual Studio 2008" failed.

A previous release of Microsoft Visual Studio 2008 is installed on this computer. Upgrade Microsoft Visual Studio 2008 to the SP1 before installing SQL Server 2008. Visual Studio SP1 can be found at http://go.microsoft.com/fwlink/?LinkId=159627.


As many suggestion found on google , ihave installed sp1, and also changed the following key value in registry, then also it does not work.

Even if i change
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DevDiv\VNS\Servicing\9.0\EXP\1033]
"Install"=dword:00000001
"InstallerType"="MSI"
"SP"=dword:00000001
"SPIndex"=dword:00000001
"SPName"="SP1"

What would be the solution.?

ETG Consultancy

Web Designing Development Analysis & Promotion
Asp.Net 2.0 SQL WWF WCF SEO Marketing Ajax JQuery NHibernate MVC