JQuery to get file from a webservice

Note: I have moved to blog at my own domain, so kindly visit this post over there for recent updates or comments. http://www.imranbalouch.com/blog/index.php/2011/07/jquery-to-get-file-from-a-webservice/

Recently we were working on a project, which was ASP.NET 3.0 based and used jquery to get and set data. Our design was to have Presentation layer at separate place and business layer at separate place, so we introduced webservices in it.

We had a use case in which user has to download file, either PDF or ZIP file. In traditional server side code we use to put the byte array of file in Response and user get a download option but in jquery we got stuck as byte array wasn’t getting handled by jquery.

The soultion that we implemented was that we created a server side static method with return type string in our ASP.NET and marked it as a WebMethod.

public static string DownloadFile(Int64[] requestId)

if input parameter array has one value, system returns PDF otherwise ZIP file.
Our server side gets data from the web service and writes the file in a folder and return the file virtual path like http://localhost/myapp/tempfolder/myfile.zip to the calling jquery method.

jquery method than uses that url by calling window.open method and user gets that file downloaded.

I feel like that its not a better way of doing it but still don’t have any other.
I will be glad to have a better suggestion from anyone.

All is Well!

Certificate Import Exception

Note: I have moved to blog at my own domain, so kindly visit this post over there for recent updates or comments. http://www.imranbalouch.com/blog/index.php/2011/04/certificate-import-exception/

While importing a .pfx or .p12 certificate you can come across an exception saying:

An internal error occurred. This can be either the user profile is not accessible or the private key that you are importing might require a cryptographic service provider that is not installed on your system.

The reason for this exception is that You have insufficient permissions to access the DriveLetter:Documents and SettingsAll UsersApplication DataMicrosoftCryptoRSAMachineKeys folder on the computer.

By giving correct permissions to your user on above folder you can get rid of this exception.



Lets Play JavaScript, trips and tricks

Note: I have moved to blog at my own domain, so kindly visit this post over there for recent updates or comments. http://www.imranbalouch.com/blog/index.php/2011/03/lets-play-javascript-trips-and-tricks/

I am writing this blog for different tips and tricks for javascript on demand of a friend of mine who is new to this world, it might contain normal and newbies methods and might have complex scripts. I will be updating it on regular basis.

String Manupulation

To remove/replace some character(s) from string use String.replace() method:

<script type="text/javascript">
        var str1 = "imran balouch";
        str1 = str1.replace("imran","");
        alert (str1);
Don't forget to assign back the results or replace method to variable.

To get a substring from a string you can use either substring method or slice method.

var str = "Imran Balouch!";
var newStr = str.substring(0, str.length-1);
// alert Imran Balouch

The slice() method takes at least one integer argument, which determines after which character the substring will be extracted.

var str = new String("Imran Balouch!");
alert( str.slice(6) );
the alert will show Balouch!

If you pass negative integer to slice method,slice() will cycle backwards (from end to start) through the string until it has iterated through as many characters as the absolute value of the integer passed.

var str = new String("Imran Balouch!");
alert( str.slice(-8) );
the alert will show Balouch!

The second argument optionally accepted by the slice() method is also an integer, and determines the length of the substring returned.

var str = new String("Imran Balouch!");
alert( str.slice(6,7) );
the alert will show Balouch

Other useful methods can be:

Length is a read-only property of JavaScript strings: your scripts can retrieve a string's length (the number of characters it contains), but cannot set it to a new length value.The length property contains and returns a positive integer, or zero in the case of an empty string.
 var str = new String("Imran Balouch!");
alert( str.length.toString() );
alert will show 14.
The charAt() method, a string method that allows you to determine which character is at a given position in a string.
var str = new String("Imran Balouch!");
alert(str.charAt(str.length-1) );
the alert will show !
alert will show 14.

Javascript and C#

If you have created a javascript method and want to call from server side than following can be helpful for you:

<script type="text/javascript">
function callMeFromServer()
    alert("Imran Balouch!");

now is your server side code (following is C# code), you can get this method called by:
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "Testing", "callMeFromServer();", true);
ScriptManager class is present under System.Web.UI

If you are in need of building a javascript array from server side, following can be helpful for you, again its C# code:

System.Random random = new System.Random();
System.Text.StringBuilder clientArray = new System.Text.StringBuilder(String.Empty);
clientArray.Append(" var myArray= new Array(3);");
ScriptManager.RegisterStartupScript(this, typeof(String), "BuildMyArray" + random.Next(), clientArray.ToString(), true);

Above code will create a javascript array named myArray and you can access it from client side as:

<script type="text/javascript">
function useMyArray()
          alert("Array is null, some problem occured.");

All for now,I will be editing this post off and on.
All is Well!!

Postback behavior of dropdown in firefox

Note: I have moved to blog at my own domain, so kindly visit this post over there for recent updates or comments. http://www.imranbalouch.com/blog/index.php/2011/03/postback-behavior-of-dropdown-in-firefox/

I am not sure that either it is behavior of firefox or not but that was a strange thing that happened to me during a development.

I have a web application in ASP.NET and C#. On a page I have a drop down list, which works fine in IE. However in firefox if I click on the dropdown, it get expanded, now if i type some charcter, relevant value get selected, now if i click out side the dropdown anywhere on my page, dropdown collpased, new value is shown as selected but no post back occurs even auto post back is set to true.

I even put a client side function to call on change of value, but that also didn’t get called.

I feel like that it is behavior of firefox, caz it appears that firefox postbacks data after dropdown looses focus. on 1st click the focus is at the dropdown and on 2nd click dropdown looses focus. But the wiered thing is that in dropdown new value is being displayed to the user.

But on the other hand if we see it is a nice behavior of firefox, caz on dropdown list if autopoastback is set to true than in IE, if u move focus to dropdown and type A, first value starting with A is selected and page is post backed where as firefox waits for lossing focus so that if selected value is not your desired one than u can reach desired value and avoid extra post backs.

A way out that I found to avoid it in case of mouse usage is to capture the onclick event of dropdown in javascript and hold a value that the dropdown is opened now and capture onclick event of page and see if previous click was on dropdown and new click is not on dropdown than using dopostback, make a post back for the dropdown. I know currently its a wiered solution but right now i figured out only this way to handle it.

All is Well…

Infragistics Ultrawebgrid igtbl_updateAddNewBox(this.Id) throws javascript exception

Note: I have moved to blog at my own domain, so kindly visit this post over there for recent updates or comments. http://www.imranbalouch.com/blog/index.php/2011/02/infragistics-ultrawebgrid-igtbl_updateaddnewboxthis-id-throws-javascript-exception/

While working with Infragistics, ultra webgrid, you might have come across javascript exception, “Object expected” at method igtbl_updateAddNewBox(this.Id).

One basic reason to this exception is that AllowAddNewDefault property of DisplayLayout of Grid is not set to yes, set it to yes and you will get rid of this exception.

But I came across this one in another scenario, and wasted a good amount of time on it. I had 2 aspx pages having master page and update panel. Both pages have ultra web grids in update panel, older page was working fine but the new one was showing strange behavior. After page loaded for first time if i click any cell of grid for editing, it throw the javascript exception but if some postback occurred than it worked fine.

I took grid from older page but no hope, after 6-7 hours it hit my mind that there was a basic difference between both pages, older page was sending synchronous calls to our middle layer web service where as new page (problem child) was sending asynchronous calls to the web service, I removed the asynchronous calls to synchronous ones and removed Async=”true” from the Page directive and here we go, it was working fine.

I am not sure what was the in depth reason behind it but it worked for me, and posting it here, if anyone falls in same trouble. Also I was adding a new row in grid via javascript and was throwing same error on addrow function, which also vanished after it.

All is Well!

Out of Memory/Memory Leakage on my Dell laptop with windows 7

Note: I have moved to blog at my own domain, so kindly visit this post over there for recent updates or comments. http://www.imranbalouch.com/blog/index.php/2010/12/out-of-memorymemory-leakage-on-my-dell-laptop-with-windows-7/

Recently i got ma new laptop, Dell with intel i5 with 4GB RAM along with windows 7, 32 bit. I was facing an out of memory message after 3-4 hours of usage. It was running me mad. After doing some web searching and inspecting my system’s memory I figured that bcmwltry.exe which is associated with the Dell Wireless WLAN Card Network Controller, was creating trouble for me.

To demonstrate the leak, open the Windows task manager.
Click the processes tab
Click the “Show processes from all users” button.
Under the view menu, click Select Columns, and select all the Memory options as well as Page Faults.
Click OK to see the new columns and then click Page Faults to sort the processes by page faults.

And here we go.

So I stopped DW WLAN Tray Service and killed the process and haven’t experienced this issue and my wireless communication isn’t having any problem.

Basics of CSS to modify your profile at M2E Part II

I hope you are enjoying your journey with M2E profile editing.
Here are few more tips for it:
On your profile under friends tab, you can change the look and feel of each friend’s result by playing with:
background-color: transparent;
border:1px #000000 dotted;
and to play with friend’s photo u can use:
border:1px rgb(118,173,255) dotted;
to give some look to the text area where your friends give comments use following css class:
textarea {
color: #6A6962;
background: none;
font-size: 12px;
border: 1px dotted rgb(118,173,255);
Hope to have you a nice and kool journey…