Wednesday, January 30, 2013

Windows Service: Starting a Service on Installation

I have been working on a Window Service project and I need to start the service after it gets installed since I need to manually run it after installation. This is very frustrating for me since I have been testing the service also.

So I have found the following solution which I am going to share with you which solved my problem and Service gets started after installation.

Below is the code for the Installer class which I have used in my Service project:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;


namespace Service
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : System.Configuration.Install.Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
            this.AfterInstall += new InstallEventHandler(ProjectInstaller_AfterInstall);
        }

        void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
        {
            ServiceController sc = new ServiceController(serviceInstaller1.ServiceName);
            sc.Start();
        }

        

    }
}

Hope this will help to resolve such issue.


ASP.Net: Html Text at End of Downloaded File

I am having this issue a few days ago which is quite strange for me since I am new to development in ASP.Net and I have shifted from Windows Application Development in .Net.

Issue:
The issue is when you save a file from any webpage then some html text gets appended with the file which some times makes the file corrupt.

The code which I am using in order to save the file is shown below:

FileStream fStream = new FileStream("C://1.pdf", FileMode.Open, FileAccess.Read);
long FileSize = fStream.Length;

byte[] Buffer = new byte[(int)FileSize];
fStream.Read(Buffer, 0, (int)FileSize);
fStream.Close();

Response.Buffer = true;
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=Output.pdf");
Response.BinaryWrite(Buffer);
Response.Flush();

Solution:
The solution is really simple which I got from google search.

The working code is shown below:

FileStream fStream = new FileStream("C://1.pdf", FileMode.Open, FileAccess.Read);
long FileSize = fStream.Length;

byte[] Buffer = new byte[(int)FileSize];
fStream.Read(Buffer, 0, (int)FileSize);
fStream.Close();

Response.Buffer = true;
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=Output.pdf");
Response.BinaryWrite(Buffer);
Response.End();

The only issue is with the Response.Flush() line which I use often when working as a Desktop developer. But with web its not the case. So we have to call Response.End() and should not call Response.Flush().

Hope many will get benefit from this.

Saturday, January 26, 2013

CSS: Comments

All comments begin with a forward slash and asterisk (/*), and end with the
asterisk followed by the forward slash (*/). This is a very simple and easy to remember method
that you may prefer to use in more complicated or important styles.


Example:
/* Default styling for paragraphs */
p {
color: #F00;
font-size: 12px;
}
/* Make all top-level headings gray and 16px high */
h1 {
color: #333;
font-size: 16px;
}


Another method preferred by many designers further highlights the rule by adding a dashed line
with the comment a great way of carving up the style sheet and making it more visually
manageable.

Example:
/* Default styling for paragraphs
-------------------------------------------------------- */
p {
color: #F00;
font-size: 12px;
}

Friday, January 25, 2013

CSS: Defining a Style

CSS syntax is made up of the following entities:
  1. Selector (the element or tag you wish to control)
  2. Property
  3. Value

Example:
Below I am going to show an example in order to illustrate the style better.

body{
padding: 10px;
}

body - Selector
padding - Property
10px - Value

Always place a semicolon at the end of a property.

Hope you have got a better idea of the entities involved in CSS style.

 

Book: Beginning CSS Web Development


Few days ago I came accross this nice book on CSS which I found very much useful and I have learned a lot from it. Although I am a software devleoper myself but I am not a designer but I always feel lack of good designing capabilities and abilities in me. Although I tried a lot to learn how to design a website but I was not able to do so. The main reason is that I have not found any useful resource which will help me right from the beginning to advance level. But this book helped me a lot and now feel I have better understanding of CSS then before.

I want to share the basic topics which are covered in this book. Table of contents section is shown below. Hope you will got a better idea of what the book covers and if you want to read this book then you can pick it up.

Table of Contents:

PART 1 ■ ■ ■ Get to Know CSS
■CHAPTER 1 Getting Started . . . . . . . . . . .  . . . . . . . . . . . . . . . . . 3
■CHAPTER 2 Core Concepts of CSS . . . . . . . . .. . . . . . . . . . . . . . . . . 17
■CHAPTER 3 CSS Building Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . 39
■CHAPTER 4 Text . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . 55
■CHAPTER 5 Color, Backgrounds, and Images . . . .. . . . . . . . . . . . . . . . . 79
■CHAPTER 6 Lists . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . 103
■CHAPTER 7 Links . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . 129
■CHAPTER 8 Tables and Definition Lists . . . . .  . . . . . . . . . . . . . . . . 145
■CHAPTER 9 Forms . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . 167

PART 2 ■ ■ ■ Logical Layouts
■CHAPTER 10 Layout Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
■CHAPTER 11 Classic Layouts . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
■CHAPTER 12 Layout Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . 275
■CHAPTER 13 The Journey from Layout to Template . . . . . . . . . . . . . . . . . 291
■CHAPTER 14 Usability and Accessibility Enhancements .  . . . . . . . . . . . . . 315
■CHAPTER 15 Tips, Tricks, and Troubles . . . . . . . . .. . . . . . . . . . . . . 329
■CHAPTER 16 Case Study: The Dead Goods . . . . . . . . .. . . . . . . . . . . . . 347
■APPENDIX CSS Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 387


CSS: Types of CSS Styles

There are 3 types of CSS Styles which have been used in Web pages which are listed below:
  1. Inline Styles
  2. Embedded Styles
  3. External Styles
Now I am going to explain each one of them one by one.
Inline Styles:
Inline styles make use of the style attribute applied to specific tags within the document,
where the actual style value is declared using the form name:value, or property:value, if you
want to use the correct terminology.

Example:
Replace this:

With:
.
This very simple declaration will ensure that the paragraph text will be red.

Embedded Styles:
Embedded styles still have you working exclusively within the (X)HTML template, but this time
all styles are grouped together in the head of the document, as part of one element.

Example:
Within the head section of the html page place below line:
.
This results in text contained within paragraphs on the web page to be red.

External Styles:
External Styles have been placed in a separate external style sheet and it contains are the styles which are required for the full site even if it contains thousands of pages.

Example:
Place below line within the head section of the web page:


Create an external.css style sheet file and place the below line inside it.
p {color: #F00;}

The result of this will be that all the web pages which contains link inside the head section written above paragraphs text becomes red.

If you want to read about the benefits of External Styles then follow the below link:
Benefits of External Styles




CSS: Benefits of External CSS Styles

What are External CSS Styles?
External Styles are the css styles which are placed in a separate file with css extension. Most of the sites over the internet use External Styles since it has many benefits. I just want to discuss what are the benefits.

Below is a list of benefits of External Styles:
  1. One external style sheet only, and not the markup.
  2. Sitewide style changes are one-sheet.
  3. Once the browser accesses the style sheet, it is cached and need not be downloaded.
  4. Pages gets rendered faster due to style sheet caching.
  5. Saving on bandwidth due to style sheet caching.
  6. One CSS file replacing the same or similar code that would be needed in each and every (X)HTML page if working with embedded CSS.
  7. Markup can be devoid of any presentational information, keeping it lean and content-only.
Hope you have got a better idea of External style sheets.


 

Thursday, January 24, 2013

The type 'System.Web.Mvc.ModelClientValidationRule' exists in both

I think you may have often got this error when ever you are getting started working on MVC projects. Especially this occurs when you install MVC4 framework and you have MVC3 also installed so when ever you create a MVC3 project you might get this error on building the project in AccountController class:

The type 'System.Web.Mvc.ModelClientValidationRule' exists in both '....System.Web.WebPages.dll' and '........\System.Web.Mvc.dll'    .......................AccountModels.cs

Solution:

I just wanted to tell you the simple solution for this:

Open you project .csproj file in Notepad or any other text editor and replace this line with the new line which I will show you.

Previous Line:


Replace With:


After saving the file and reloading the project and then building the project again you will get surprised that you issue got resolved automatically.

Hope this might help many developers getting this issue.


Wednesday, January 23, 2013

Javascript: Show and Hide Controls By ID


Navigation section



Below is the code which you can copy within html form body to test it.
**********myprogrammingzone.blogspot.com************



Navigation section
**********myprogrammingzone.blogspot.com************

Wednesday, January 9, 2013

Code First Migration Commands

Code First:
"Whether you have an existing database or not, you can code your own classes and properties that correspond to tables and columns and use them with the Entity Framework without an .edmx file. That's why you sometimes see this approach called code only, although the official name is Code First. The mapping between the store schema and the conceptual model represented by your code is handled by convention and by a special mapping API. If you don't yet have a database, the Entity Framework can automatically create the database for you, or drop and re-create it if the model changes."

Hope you have got the idea of Code First Development approach.

There are two commands which are used too much for the migration and updation of database. I want to share the two commands since I myself use to forget the syntax when I write them in Package Manager in Visual Studio. Although its simple to press Tab Key if you forget someting then Package Manager will show intellisense for you.

Migration Command:
Add-Migration NameOfMigrationFile -StartUpProjectName ProjectName

NameOfMigrationFile - Give name to Migration cs File
ProjectName - Project which contains your Model classes

Update Database Command:
Update-Database -StartUpProjectName ProjectName

ProjectName - Project which contains your Model classes

Revert a Migration Command:
Update-Database -StartUpProjectName ProjectName -TargetMigration NameOfMigrationFile

ProjectName - Project which contains your Model classes
NameOfMigrationFile - Migration file name which needs to be reverted without .cs extension


Hope this helps you in your Code First migrations.

Tuesday, January 8, 2013

The Web server is configured to not list the contents of this directory.

Yesterday I am working on a project and I got the following error when I run the application:

HTTP Error 403.14 - Forbidden

The Web server is configured to not list the contents of this directory.

Logon MethodAnonymous

Logon UserAnonymous

0x00000000

But the solution was really simple and was provided within the error page.
I am copying the steps which are shown on the page:

  • If you do not want to enable directory browsing, ensure that a default document is configured and that the file exists.
  • Enable directory browsing using IIS Manager.
    1. Open IIS Manager.
    2. In the Features view, double-click Directory Browsing.
    3. On the Directory Browsing page, in the Actions pane, click Enable.
  • Verify that the configuration/system.webServer/directoryBrowse@enabled attribute is set to true in the site or application configuration file.





I have followed the above steps and my issue gets resolved. Screen shots have been attached for easier solution. Hope it will help you also.




Monday, January 7, 2013

ExtJs Learning Useful Site Urls

ExtJs is know as Extended JavaScript and a lot of newer projects are moving towards this new technology. Its quite close to the conventional JavaScript language with some advanced features like MVC model. If you are interested to learn about this then I am going to share some useful links which I followed myself and found them very useful.

ExtJs Introduction:
In order to learn some thing its better to understand the its basics. Below is the link which I have followed myself and is really beneficial for you.
http://docs.sencha.com/architect/2/#!/guide

ExtJs Examples:
You can find a lot of examples and sample code at the below link and I also found it very much useful.
http://dev.sencha.com/deploy/ext-4.0.0/examples/#sample-16


Hope you will find a lot from the above links.




Saturday, January 5, 2013

SQL: Replace String

Sometimes we need to replace some word from SQL Database table fields. In order to do that you have to follow the below query syntax:

Syntax:
Update TableName
set ColumnName = replace(ColumnName, 'String to be Replaced', 'String to be replaced With');

Example:
I am giving an example for this:

Update Post
set PostDescription = replace (PostDescription , '&', 'and');

This will replace '&' with 'and' whereever it founds this in the given column.


Hope now you can easily do it.

Book: Professional ASP.NET MVC 4

I have read this very nice book for MVC 4. I want to share the contents of the book with you.

Authors:
Jon Galloway (Author), Phil Haack (Author), Brad Wilson (Author), K. Scott Allen (Author)

Chapters:
CHAPTER 1 Getting Started -------------------------------------------------------------------------- 1
CHAPTER 2 Controllers ----------------------------------------------------------------------------- 31
CHAPTER 3 Views ----------------------------------------------------------------------------------- 47
CHAPTER 4 Models ---------------------------------------------------------------------------------- 71
CHAPTER 5 Forms and HTML Helpers ----------------------------------------------------------- 95
CHAPTER 6 Data Annotations and Validation --------------------------------------------------- 119
CHAPTER 7 Membership, Authorization, and Security ---------------------------------------- 137
CHAPTER 8 Ajax ------------------------------------------------------------------------------------ 189
CHAPTER 9 Routing --------------------------------------------------------------------------------- 221
CHAPTER 10 NuGet --------------------------------------------------------------------------------- 249
CHAPTER 11 ASP.NET Web API ------------------------------------------------------------------ 279
CHAPTER 12 Dependency Injection --------------------------------------------------------------- 297
CHAPTER 13 Unit Testing --------------------------------------------------------------------------- 341
CHAPTER 14 Extending MVC ---------------------------------------------------------------------- 365
CHAPTER 15 Advanced Topics --------------------------------------------------------------------- 365
CHAPTER 16 Real-World ASP.NET MVC: Building the NuGet.org Website ---------------- 423



I think this book covers all the basics of ASP.Net MVC 4 right from the start explaining MVC pattern and then discussing the advanced topics. A chapter for Web API is also included which is the new technique for replacing Web Services and it could be used with both the web and desktop applications. I hope you will surely learn a lot if you are a beginner.



ASP.NET 4.0 has not be registered on the Web server

Once I got this error after installing Visual Studio 2010 SP1 and installing ASP.Net MVC 4 on my system.

"ASP.NET 4.0 has not be registered on the Web server. You need to manually configure your Web server for ASP.NET 4.0 in order for your site to run correctly. Press F1 for more details."

After some searching I have found the solution which I want to share with you.

Solution:
Go to programs menu and open the Run command. Write the following command in the Open textbox:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis -i

Hit enter or click OK button.

A command prompt appears and then close automatically after doing some processing.

Now restart your Visual Studio and your issue gets resolved.