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/