Monday, November 30, 2009

Difference between C# and VB.NET

This question is asked almost in every interview, if you have worked with both the language. There are syntax difference, keyword difference in both the language. But the interviewer is not looking for such answers. One need to explain the functional difference between both the languages.

Here are some key difference in C# and VB.NET other than syntax differences.

  1. In C#, using keyword is used to release unmanaged resources. (Not available in VB.NET)
  2. Optional parameter is supported in VB.NET. (Not available in C# 3.0 or below version).
  3. Structure and unstructured error handling (On Error GoTo) is supported in VB.NET. (Unstructured error handling is not supported in C#).
  4. Event gets bind automatically in VB.Net.
  5. VB.NET is not case sensitive where C# is.
  6. Shadowing: – This is a VB.Net Concept by which you can provide a new implementation for the base class member without overriding the member. You can shadow a base class member in the derived class by using the keyword “Shadows”. The method signature, access level and return type of the shadowed member can be completely different than the base class member.

    Hiding: – This is a C# Concept by which you can provide a new implementation for the base class member without overriding the member. You can hide a base class member in the derived class by using the keyword “new”. The method signature, access level and return type of the hidden member has to be same as the base class member.

    Comparing the two:-
    1) The access level, signature and the return type can only be changed when you are shadowing with VB.NET. Hiding and overriding demands these parameters as same.
    2) The difference lies when you call the derived class object with a base class variable. In class of overriding although you assign a derived class object to base class variable it will call the derived class function. In case of shadowing or hiding the base class function will be called.

  7. Visual Basic .NET can also force parameters to be passed by value, regardless of how they are declared, by enclosing the parameters in extra parentheses. There is no way to achieve this thing in C#.
    For Example:
    1Dim y As Integer = 5
    2Dim z As Integer
    3z = Add(y) //This will set both Y and Z to 6.
    4z = Add((y)) //This will set Z to 6 but Value of Y will not be change, as we have included extra parenthese while calling.

    The Add function:

    1Public Function Add(ByRef x As Integer) As Integer
    2x = x + 1
    3Return x
    4End Function

I know this is not a complete list, so if you know any other difference kindly post your comments.

Enjoy..

Friday, November 27, 2009

AsyncFileUpload Control - New Control in Ajax Control ToolKit

Ajax Control Toolkit is not new for every dotnet developer. A new version of the AJAX Control Toolkit is now available for download from the CodePlex website.


This new version of the AJAX Control Toolkit contains two new controls:

SeaDragon Java Script Code (SJC)
 - The SJC control allows you SeaDragon scripts are used to display an image, and to zoom in and out of that image using mouse button keys without resizing the window. I saw the demo and it's really cool control.



AsyncFileUpload - Finally, we have a control which uploads file asynchronously. This new control enables you to perform file uploads without doing a postback. The control displays a throbber image during upload and raises client and server events when the upload is complete. Check the live demo here.
In this article, we are going to take a look at AsyncFileUpload control.


Note: This control will only work with .NET 3.5 or higher version.

AsyncFileUpload Control Features

As we know, File Upload control of ASP.NET does not work with in update panel. If we want to place it in update panel, then also postback trigger is required to upload the file. This cool control allows you to upload the file in asynchronous manner. Below are few key points about this control:
  1. It works within the Update Panel.
  2. Uploads the file without any postback.
  3. Provides Client Side and Server side events.
  4. Different coloring options for showing file upload. As for example, it shows green color if upload is successful, otherwise it shows red if there is unsuccessful upload.
  5. You can show the loading image while file uploading is in progress.
But it also comes with certain disadvantages.
  1. When I was working with the control, once the file is uploaded there is no way to clear the content of file upload control.
  2. I went into the source code of this control and noticed that the control stores the file in Session. It never clears the session, which means every time to navigate back to the page it loads the last file uploaded into the text box.
  3. There is no way to cancel the upload.
  4. There is no way to monitor the progress (How much % is completed) of uploading.
  5. Uploading starts as soon as you select the file. It stores the files in the session.
Besides, these disadvantages control looks good.


Now let's go thru some of the properties of this control.

Properties and Methods

Some Available Properties
  1. CompleteBackColor : This Property set the background color of the control on successful upload. Default Value is "Lime".
  2. ErrorBackColor : This Property set the background color of the control on unsuccessful upload. Default Value is "Red".


  3. UploadingBackColor : This Property set the background color of the control when file uploading is in progress.
  4. UploaderStyle : There are two options available for styling of the control. Traditional and modern. Default is "Traditional".
  5. ThrobberID : ID of control that is shown while the file is uploading. It will be used to display the loading/in progress image.
  6. HasFile : Returns a bool value which indicates control has a file or not.
Available Events:
    1. OnClientUploadError : This is a client side event. If there is an unsuccessful upload then specified JavaScript function will be executed.

    2. OnClientUploadStarted : This is also a client side event. Specified JavaScript function will be called when the uploading start. This event will be called before uploading and once this function is executed, uploading will start.

    3. OnClientUploadComplete : This is also a client side event. If there is successful upload then specified JavaScript function will be executed.

    4. onuploadedcomplete : This is a server side event which will be executed once the uploading is complete. One thing to notice over here is, as soon as you select the file, uploading starts but it remains in session. It is not stored on any physical location. In this event, we can specify the path and save the file into physical location.Things will be clear once we go thru the code.
    Available Methods:
    1. SaveAs() : This method saves the file on specified path.

    Demo

    Let’s create a new website and add reference of newly downloaded AjaxControl ToolKit dll. On default.aspx page, Place a script manager and register the ajax control toolkit dll.
    Now let’s place the AsyncFileUpload control.

    <cc1:AsyncFileUpload ID="AsyncFileUpload1" Width="400px" runat="server"
    OnClientUploadError="uploadError" OnClientUploadStarted="StartUpload"
    OnClientUploadComplete="UploadComplete"
    CompleteBackColor="Lime" UploaderStyle="Modern"
    ErrorBackColor="Red" ThrobberID="Throbber"
    onuploadedcomplete="AsyncFileUpload1_UploadedComplete" UploadingBackColor="#66CCFF" />

    We can place this within update panel or outside the update panel. As you can see, I have set the most of the properties and events which I have already explained above. Let’s place the Throbber control to show in progress image. It is not compulsory to have ThrobberID. If it is set then it will display the content of the control.

    < asp:Label ID="Throbber" runat="server" Style="display: none">;
              <img src="Images/indicator.gif" align="absmiddle" alt="loading" />;
    </asp:Label>

    I have also placed a label on the page, which shows the status of the uploading. Value of this control get’s updated via Client Side function.

    < asp:Label ID="lblStatus" runat="server" Style="font-family: Arial; font-size: small;"> < /asp:Label>

    Let’s place the JavaScript functions.

    <script type="text/javascript" language="javascript">

    function uploadError(sender,args)
    {
      document.getElementById('lblStatus').innerText = args.get_fileName(), "<span style='color:red;'>" + args.get_errorMessage() + "</span>";
    }

    function StartUpload(sender,args)
    {
        document.getElementById('lblStatus').innerText = 'Uploading Started.';
    }

    function UploadComplete(sender,args)
    {
      var filename = args.get_fileName();
      var contentType = args.get_contentType();
      var text = "Size of " + filename + " is " + args.get_length() + " bytes";
      if (contentType.length > 0)
      {
        text += " and content type is '" + contentType + "'.";
      }
      document.getElementById('lblStatus').innerText = text;
    }
    </script> 

    The UploadComplete function displayes the file name, it’s size and content type on the screen.
    Below is server side code of UploadedComplete event.

    protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {
        System.Threading.Thread.Sleep(5000);
        if (AsyncFileUpload1.HasFile)
        {
     string strPath = MapPath("~/Uploads/") + Path.GetFileName(e.filename);
     AsyncFileUpload1.SaveAs(strPath);
        }
    }
    That’s it. We are good to go now. Now just run the application.

    Conclusion

    This is a cool control which some good user experience but as stated above I cannot find a way to clear the content of the control even if you refresh the page. Another thing that is quite annoying is as soon as you select any file, it starts uploading. Another lacking feature is it does not show the progress in percentages. Despite, few disadvantages, I found this control pretty useful. Hope to see some enhancement in this tool.
    Enjoy..
    back to top