Thursday, July 22, 2010

Load external DLL from Silverlight

public partial class MainPage : UserControl
{
private string xmlPath;

public MainPage()
{
InitializeComponent();

this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(new Uri("http://localhost:56121/SL/Test/Test1.dll", UriKind.RelativeOrAbsolute));
}

void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
AssemblyPart assemblyPart = new AssemblyPart();
Assembly assembly = assemblyPart.Load(e.Result);

UserControl userControl = assembly.CreateInstance("Test1.MainPage") as UserControl;

if (userControl != null)
{
LayoutRoot.Children.Add(userControl);
}
Type type = userControl.GetType();
MethodInfo[] methodInfoArray = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
foreach (MethodInfo methodInfo in methodInfoArray)
{
if (methodInfo.Name == "XMLLoad1")
{
methodInfo.Invoke(userControl, new object[] { "http://localhost:56121/SL/Test/Test1.xml" });
break;
}
}
}
}

Tuesday, July 20, 2010

Suffle a List and Find the index of Item

int index = infoList.IndexOf(item);

public class Util
{
public static List RandomPermutation(List list)
{
List retList = new List(list);
//list.CopyTo(retList.ToArray(), 0);

Random random = new Random();
for (int i = 0; i < list.Count; i++)
{
int swapIndex = random.Next(i, list.Count);
if (swapIndex != i)
{
T temp = retList[i];
retList[i] = retList[swapIndex];
retList[swapIndex] = temp;
}
}
return retList;
}
}

Monday, July 19, 2010

How to Retrieve Subdomain from URI

Hi,

Herewith, I have given below the code to retrieve the uri.

Uri uri = new Uri("http://t1.testing.com/Web1.aspx?Id=5");

public static string RetrieveSubDomain(Uri url)
{
string subDomain = "";
if (url.HostNameType == UriHostNameType.Dns && (!(url.HostNameType == UriHostNameType.Unknown)))
{
string host = url.Host;
int length = host.Split('.').Length;
if (length > 2)
{
int lastIndex = host.LastIndexOf(".");
int index = host.LastIndexOf(".", lastIndex - 1);
subDomain = host.Substring(0, index);
}
}

return subDomain.ToLower();
}

Monday, May 17, 2010

Parse XML file in Javascript

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
function verify() {
if (xmlDoc.readyState != 4) {
return false;
}
}
function ParseSliderXML(path) {
xmlDoc.async = "false";
xmlDoc.onreadystatechange = verify;
xmlDoc.load(path);
var ticker = xmlDoc.documentElement;
//alert(ticker);
return ticker.xml;
}

Monday, April 26, 2010

How to get the online user count in a website

1) Add the tag in your web config file under system.web tag.

membership defaultprovider="TestProvider"
providers
add name="TestProvider" type="System.Web.Security.SqlMembershipProvider" connectionstringname="Test"
providers
membership


2) Use sql server for session state

3) int count = Membership.GetNumberOfUsersOnline();