Wednesday, August 26, 2009

How do i know my system version. 32 bit or 64 bit?

Hi Guys,

From long time many people asked me about how do I know my system version whether 32 bit or 64 bit. When I was very new to IT industry, at that time I too don’t have any idea about it. But some things will come with experience. Please follow the below points to know your system version.

  • Go to Start button.
  • Type sysdm.cpl and click OK or press enter.
  • Now, it will open a new window. Click on General tab. Here you will find the information about the OS.
  • The only clue that you know, whether my OS is 32-bit or 64-bit is, For 64-bit specially the OS name displayed as "Windows server 2003 64-bit Operating system". Where as 32-bit is simply Windows server 2003.

If you are using Windows Vista OS, then you can directly see the version details by going to computer properties from Start button.

  • Directly go to Start button, and right click on Computer and select Properties. There you find an entry for System Type.
  • OR, you can go to Control Panel and then Go to System.

This information is very helpful many times before install any software to check for version validation. Now, you know what your system type is. Don’t you?

Tuesday, August 18, 2009

Get Querystring parameters in Page Webmethods ASP.NET

Today, at my work I have to find a way on how to get the query string parameters and use them in logic. I mean, I can get them in javascript side and send them as parameters, but I don’t want to implement it that way as I have everything in query string, I can get it from URL and use it in server side code. And second thing is, the query string parameters are not plain text, they are encrypted with a strong algorithm. So, below is the small function I written which work perfect.

//Get Querystring name value collection
    public static NameValueCollection GetQueryStringCollection(string url)
    {
        string keyValue = string.Empty;
        NameValueCollection collection = new NameValueCollection();
        string[] querystrings = url.Split('&');
        if (querystrings != null && querystrings.Count() > 0)
        {
            for (int i = 0; i < querystrings.Count(); i++)
            {
                string[] pair = querystrings[i].Split('=');
                collection.Add(pair[0].Trim('?'), pair[1]);
            }
        }
        return collection;
    }

So, this function is a static one because, we are calling it from a Webmethod. And this function is expecting url as the parameter. Actually, it is not complete url, it is just the complete querystring url which starts from the character '?'. Now, how to call and use this function? Find below.

NameValueCollection collection = GetQueryStringCollection(HttpContext.Current.Request.UrlReferrer.Query);
        if (collection != null && collection.Count > 0)
        {
            string id = HttpContext.Current.Server.UrlDecode (collection["id"]);
        } 

The above statement returns the querystring value which has the key "id". This way you can get any querystring value, just by passing it's key name to the collection.

HttpContext.Current.Request.UrlReferrer.Query – Which holds the complete querystring url starts from '?'. So, this way you can get the query string data and use it where ever you want in Page Webmethods.

Is this what you are looking for? Or do you have any other ways to get the querystring data? Please post your ideas on it here.

Call ASP.NET server side event in JQuery

For one of my requirement, I need to implement this functionality on how to call server side event in JQuery. There are many scenarios on why we need to implement it or what is the need for it. I have a button and when user click on it, I need to show a client side light box which exactly functioning as window confirm box. If user selects OK, then I need to do some client side validations and if everything passed, then i need to make a call to server to execute the actual server side click event. I think this is general scenario. There are many cases other than this. So, here is a simple solution on how to call server side event in JQuery. Enjoy this interesting blog post.

1. Create a protected page variable in Server side ASPX.cs file as shown below.

protected string serversideEvent = string.Empty;

2. In Page_Load event, set the serversideEvent variable to the button click event as below.

serversideEvent = Page.ClientScript.GetPostBackEventReference(btnSubmit, string.Empty);

3. In your JQuery, use this function to evaluate on specific condition as shown in below line.

eval(<%=serversideEvent %>);

"btnSubmit" is the ID of the control <asp:Button in ASPX page. and GetPostBackEventReference will get the click event of the related button and return it in the form of string.

That's it. Whenever the line mentioned in 3rd step called, it will call server side event and execute it.

How awesome it is? Very simple and nice way to do it. Isn’t a good and valuable find?

Get difference between Dates in Java script

It is very simple and easy to implement the difference between two dates. I received some queries on how to do it. So, this is the post for them.

function parseDate(str) {
    var date = str.split('/');
    return new Date(date[2], date[0] - 1, date[1]);
}

function GetDaysBetweenDates(date1, date2) {
    return (date2 - date1) / (1000 * 60 * 60 * 24)
}

"parseDate" is the function which is for converting a string to Date object. And the method "GetDaysBetweenDates" is expecting two date parameters which calculate the difference between the dates and return the result in number of days. You can change the formula as you want to return in time, months, weeks etc…

Enjoy!!!

Jquery – How to set a value in Drop down as selected

You know, sometimes small things will take long time to implement. Usually this will happen if we are not familiar with them. For this, plenty of documentation is available around, we should read it before starting it. So, here I am talking about JQuery and the features of it. I know how to select a value in a drop down and populate it using plain javascript. But when comes to JQuery, for me it took sometime to find a way to do that. Below is the way on how to populate a dropdown value as selected.

$("#ddlSource option[value='1']").attr("selected", "selected");

Here, ddlSource is the ID of the HTML select [drop down list] control. The meaning of the above statement is, find the control which has the ID "ddlSource" and in it find the option which has give value and select it. "1", I am using for example. You can pass any value.

If you are using the server side controls, and want to populate the value in JQuery, then use below statement.

$("#<%=ddlSource.ClientID %> option[value='1']").attr("selected", "selected");

But this is not the right way of doing it. You can change the server side drop down values on client side, but which results you the error message "Invalid Postback or Callback event" when submitting data to server. So, take care when you deal with server side controls in javascript.

For best practice Please try to use <select runat="server" ID="ddlSource" ></select> instead of <asp:DropDownList /> to avoid the invalid callback error messages. I know, the immediate question in your mind, how to get the value using <select> control in C# code. Use below code for it.

string selectedValue = Request.Form[ddlSource.UniqueID]; //UniqueID is because Form is expecting name instead of ID.

Is this helpful?

Tuesday, August 11, 2009

JQuery dialog open only first time problem, solution

This is a big problem to me for long time in JQuery. I downloaded the Jquery UI min file and started using in my projects. On the way, there are some requirements where I need to show the dialog box and execute some logic. But, whenever I click some button to open a dialog, very first time only it opens like a charm. From the time when I close it, it will never open again by clicking on open dialog link. This gave me lot of frustration and I don’t know where the exact problem is. After reading all the documentation in JQuery docs, finally found the problem and fixed it in seconds.

Solution:

$("#dialog").dialog({ modal: true, resizable: false,
draggable: false, width: 515, height: 245 });

$("#dialog").dialog('open');

I know everyone is declaring only the first statement in their code and no one will write the second statement to open the dialog. Below is the explanation for it.

As far as I understood, what JQuery framework doing internally is, When we write the first statement, it is treating it as the initiation of the dialog for the div or HTML control. Because of adding "model:true", it will open for the first time, when you click on a link and never open again once you closed. Because no where we mentioned to open the dialog in the first statement.

And about second statement, JQuery is calling the dialog property and forcibly loading the dialog to the DOM and shows it on the screen. So, open property/attribute will do the trick. So, whenever you want to use a dialog box in JQuery, this is the code you need to use to open it as many times as you want.

Is this what you are looking for? Or do you know any other ways to implement it? Please post all your ideas here.