Wednesday, May 8, 2013

Shortcut Keys in Visual Studio Editor

Some Useful Visual Studio shortcut keys


I would like to share some on the shortcut keys that are available in visual studio that will be very useful for the developers for fast codeing. I have segregated them into different parts.

VS Tools and Explore Bars (Windows) Related



Solution ExplorerCtrl +W, S 
Server ExplorerCtrl + W, L
Error ListCtrl + W, E
Output WindowCtrl + W, O
Property WindowCtrl + W, P
Tool BoxCtrl + W, X

Code File Related 



Comment SelectionCtrl +E, C or Ctrl + K, C 
Uncomment SelectionCtrl + E, U or Ctrl + K, U
Format DocumentCtrl + E, D
Make UPPERCASECtrl + Shit, U
Make lowercaseCtrl + U
Place BookmarkCtrl + B + E
Clear BookmarkCtrl + B + C
Toggle BookmarkCtrl + B + T
Previous BookmarkCtrl + B + P
Next BookmarkCtrl + B + N

Project Related 



Add New ClassShift + Alt + C
Add New ItemCtrl + Shift + A
Add Existing ItemShift + Alt + A

I use these shortcuts daily in my coding life and makes my life easy and fast to code. Hope this will effect your coding life and i would like to hear your feedback on this and please suggest if you have any. 

Your feedbacks are warmly welcomed.

Happy Coding :)

Friday, April 5, 2013

ASP.NET MVC Call multiple collection parameter action method from AJAX

Call multiple collection parameter action method using AJAX
This example is used to demonstrate how to send multiple collection data to a action method in MVC. 
First i have defined the View Model which is the type of the collection that is used as the parameter of the action method.
Second the Controller Action Method is defined to show that it takes to List<Employee> collections.
Third listing explains the View - javascript which stringify the object and send a AJAX request to the server.
View Model
public class Employee
{
  public int ID{get;set;}
  public string Name{get;set;}
  public string Age{get;set;}
}  
 
Controller Action Method

public ActionResult SaveData(List<Employee> updateEmployee, List<Employee> deleteEmployee)
{

 try
 {
   //Do your processing
 } 
 catch(exception)
 {
   // do your loggin
 }


View - javascript 
$(document).ready(function(){
 // Here i would like to simulate the collections by adding them statically for simplicity
 var uEmployee = new Object();
 var dEmployee = new Object();
//Create update employees
 var ut1Emp = new Object();
 ut1Emp.ID = 1; ut1Emp.Name="Desmond"; ut1Emp.Age="27";
 var ut2Emp = new Object();
 ut2Emp.ID = 2; ut2Emp.Name="Maria"; ut2Emp.Age="1";
 uEmployee.Push(ut1Emp );
 uEmployee.Push(ut2Emp );
 //Create delete employees
 var dt1Emp = new Object();
 dt1Emp.ID = 3; dt1Emp.Name="Jacintha"; dt1Emp.Age="25";
 var dt2Emp = new Object();
 dt2Emp.ID = 4; dt2Emp.Name="Angel"; dt2Emp.Age="1";
 dEmployee.Push(dt1Emp );
 dEmployee.Push(dt2Emp );

 var param = { 'updateEmployee': uEmployee, 'deleteEmployee': dEmployee}; 

 $.ajax({ url: "SaveData",
          type: 'POST',
          contentType: "application/json; charset=utf-8" , // this is important
          data: JSON.stringify(param), // This is important
          failure: function (errorMSG){
              alert('Error occurred on saving data');

          },
          success: function (data) {
              alert('Successfully saved');

          }
        });

});
 

Sunday, July 8, 2012

Keyword 'this' is not valid in a static property, static method, or static field initializer this.Page.User.Identity.Name

[WebMethod] is static and if the User.Identity.Name is used this error "Keyword 'this' is not valid in a static property, static method, or static field initializer" is thrown


Reason being this.Page.User.Identity is non static and usage is not valid. 
Instead using " HttpContext.Current.User.Identity.Name " will resolve the error since current is static. 


ex
someVariable = someMethodArgument(HttpContext.Current.User.Identity.Name);

Friday, February 10, 2012

How to win Interviews

I would like to share my experience to those who face the interviews.
  • Look at the roles and responsibility of the job
  • Basic overview of the employer on what is their main business, achievements and products or service they provide
  • Be there at least 20 minutes earlier
  • While facing the interview be open and honest
  • If  you don't know the straight answer say you don't know but think logically and try to find possible way, tell your interviewer this way it should happen (Reason we are not born in this world with full of knowledge, important is to think instantly what could be the possible solution)
  • When you are asked about some coding solution, think about the solution and while explaining tell them the answer and the steps to achieve the answer. Very important if you have implemented smiler solution in the past please refer 
  • Be friendly and don't get panic if you dont know the answer.
When you learn new technology learn the basic concepts through an ebook or product web site like MSDN

Good luck guys i will update this when things get into my memory. Best luck for your future...


Friday, January 13, 2012

Single or multiple Image upload on ASP.NET MVC3

I spent a great time searching for an example of Upload Image on ASP.NET MVC3 web site.

These are the two simple steps you need to do. (single image)

Create the view
---------------------

<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="firstName" id="firstName" />
<input type="file" name="file" id="file1" />
<input type="submit" />

*Note - Make sure the name that you give for the input type file is important

or If u are using html helper to create the form

@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="text" name="firstName" id="firstName" />
    <input type="file" name="file" id="file1" />
    <input type="submit" />
}

Create the controller
[HttpPost]
        public ActionResult Upload(string firstName, HttpPostedFileBase file)
        {            
                if (file.ContentLength > 0)
                {
                    using(Image img= Image.FromStream(file.InputStream))
                    {
                        //--Initialize the size of the array
                        byte[] imgData = new byte[file.InputStream.Length];

                        //--Create a new BinaryReader and set the InputStream
                        //-- for the Images InputStream to the
                        //--beginning, as we create the img using a stream.
                        BinaryReader reader = new BinaryReader(file.InputStream);
                        file.InputStream.Seek(0, SeekOrigin.Begin);

                        //--Load the image binary.
                        imgData = reader.ReadBytes((int)file.InputStream.Length);

                        //--Create a new image to be added to the database
                    }
                }
            return RedirectToAction("Index");
        }     

For multiple Image upload 

The View
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="firstName" id="firstName" />
<input type="file" name="files" id="file1" />
<input type="file" name="files" id="file2" />   
<input type="submit" />


or If u are using html helper to create the form

@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="text" name="firstName" id="firstName" />
    <input type="file" name="files" id="file1" />
    <input type="file" name="files" id="file2" />
    <input type="submit" />
}

The Controller

[HttpPost]
        public ActionResult Upload(string firstName, IEnumerable<HttpPostedFileBase> files)
        {
            foreach (var file in files)
            {
                if (file.ContentLength > 0)
                {
                    using(Image img= Image.FromStream(file.InputStream))
                    {
                        //--Initialize the size of the array
                        byte[] imgData = new byte[file.InputStream.Length];

                        //--Create a new BinaryReader and set the InputStream
                        //-- for the Images InputStream to the
                        //--beginning, as we create the img using a stream.
                        BinaryReader reader = new BinaryReader(file.InputStream);
                        file.InputStream.Seek(0, SeekOrigin.Begin);

                        //--Load the image binary.
                        imgData = reader.ReadBytes((int)file.InputStream.Length);

                        //--Create a new image to be added to the database
                    }
                }
            }
            return RedirectToAction("Index");
        }  

Reference
http://garfbradazweb.wordpress.com/2011/08/16/mvc-3-upload-sql-server-entity-framework/


Sunday, December 11, 2011

Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

Problem :
When i created the MVC3 project while having the Domain on a separate project and reffed the dll to the web project i came across this error "Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information."

Solution:
Remove the reference to the Entity Framework dll and add the Entity Framework dll on the domain bin project again.

Reason :
When i started the web project i added reference to EF 4.1 but while completing the domain i refereed to EF 4.2. This was the cause to the problem.

 :) coding

Monday, October 10, 2011

Problem Installing MVC 3

Warning Message
"This product is incompatible with the Microsoft Visual Studio Async CTP. Please uninstall the component, then try to install this product again"




Solution
Go to Control Panel -> Click on View Installed Updates -> Under Microsoft Visual Studio 2010 Ultimate or type Async in search box and then -> select the Microsoft Visual Studio Async CTP and Uninstall.