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.

About Me

Dear Readers,


I welcome you all to my very first blog with a warm welcome.  I would like to give you brief introduction about myself so that it I can stay one step closer to you. I am from Sri Lanka the beautiful isle of the Indian Ocean. I have completed my Masters Degree in Computer Science and I have more than 5 years of experience in Software Development.

I started with classic ASP, developed a web based CRM for call center after one year I moved to another company where I worked for another year on ASP.net and Oracle 9i and developed an ERP system. I could not find what I was looking for there as well, but the moment I went to the Interview at Duo Software, I was much happy and excited to join them.

At Duo I developed the CRM with customized fields, Campaign Manager, Soft Phone using Microsoft RTC, Quality Management System for Call Center, Predictive Dialer and Advanced Request Distribution Server (generalized version of Automated Call Distribution System ACD).

I started with .net 1.1 and gradually moved to .net 2.0, .net 3.0 and .net 4.0 along with WCF and SQL Server 2005 and 2008. I was able to reengineer my own products at Duo by applying proper design pattern to cater extendibility, maintainability and reusability.

I am always glad to share my experience with you and to answer your questions with my own experience. Looking forward to blog with you all we will share each other with what we know.

As Steve Jobs mentioned lets “be hunger be foolish”..