Friday, March 6, 2015

Configure TinyMCE editor in ASP.NET MVC5

TinyMCE editor is one of the most popular opensource online editor. Its very simple to add TinyMCE editor in your ASP.NET application. In your ASP.NET MVC5 application go to TOOLS>Library Package Manager>Package Manager Consolee write Install-Package TinyMCE then press enter. It will download TinyMCE editor in your application. To see the full feature or demo of TinyMCE editor go to this link TinyMCE demo
Now we will add a controller in name TinyMCEController in the project.

public class TinyMCEController : Controller
{
    //
    // GET: /TinyMCE/
    public ActionResult Index()
    {
       return View();
    }

    // An action that will accept Html Content
    [HttpPost]
    public ActionResult Index(TinyMCEContent model)
    {
       return View();
    }
}

I have to add another model class that is TinyMCEContent class in the model.

    public class TinyMCEContent
    {
        // This attributes allows your HTML Content to be sent up
        [AllowHtml]
        public string HtmlContent { get; set; }

        public TinyMCEContent()
        {

        }
    }

Now we need a view to display the TinyMCE editor. So now we will create a folder name TinyMCE inside Views. I will create index views(index.cshtml) right click mouse in TinyMCE folder. Check following image if you have any confusion.
Now we are ready for setting the TinyMCE editor. I have written following code for displaying TinyMCE editor in the index.cshtml file.
When you click the button Save, it will post the content of the editor in [HttpPost] method. The TinyMCE editor will be look like following when you run the solution.
Is n't it so simple to add TinyMCE editor in ASP.NET MVC application? :)

Sunday, November 2, 2014

C# basic OOP interview questions and answer

Q1:What is polymorphisms?
Ans: Polymorphism is one of the very basic question for the interviewers. To answer what is Polymorphism, The most common answer we get is one interface multiple method or multiple form. We declare several method with same name but with different parameter is the easiest example of polymorphisms also known as function overloading.
Following is the example of function overloading:

using System;

class Calculator
{
    static void Add(int x,int y)
    {
        Console.WriteLine("result is:"+(x+y));
    }

    static void Add(int x,int y,int z)
    {
        Console.WriteLine("result is:"+(x+y+z));
    }
    
    static void Add()
    {
        Console.WriteLine("Invalid number");
    }
}

Following is the example of overriding also known as virtual method:

using System;

class Vehicle
{
    protected decimal avgSpeed;
   // Declared virtual so it can be overridden. 
   public virtual decimal Speed()
   {
      return avgSpeed;
   }
}

class Car : Vehicle
{
    protected decimal avgSpeed;
    protected decimal horsePower;
   // Declared virtual so it can be overridden. 
   public override decimal Speed()
   {
      return avgSpeed*horsePower;
   }
}

The override function should be define virtual. Polymorphisms could be understand easily if we understand function overloading and overriding. Operator overloading could be another example of polymorphisms.

Q2:What is abstract method?
Ans:Abstract method has no implementation. The class that inherit abstract class have to implement all the abstract method in the derived class.
Following is the example of the abstract method:
abstract class Vehicle
{
   abstract public void wheels();
   abstract public int brakes(int brakes);
   abstract public int speed();
}

class Car : Vehicle
{
   override public void wheels()
   {  
      // Initialization code goes here.    
   }

   override public int brakes(int brakes)
   {
     // Initialization code goes here.    
   }
  
   override public  public int speed()
   {
     // Initialization code goes here.    
   }
}




Q3:What is static properties of a class?
Ans: In C# a static class is same as a non static class but it is not possible to create instance of the static class. We access static class by the name of static class dot static class function or properties name.
Example: Helper.Save(); Static class does not have instance variable. Normally static class handle input variable and performs some operations. Static class remain life time of the application.
Static member can be declare in non static class. Static member is always callable by class name. Important properties to remember that static member cannot access the non static properties of the class.
Q4: What is difference between encapsulation and abstraction?
Ans: Abstraction means showing that is only important to developer. The concepts of abstraction suggest to show only minimal things to the end developer when developing software. Encapsulation described as hiding complexity of code to the developer. Insertion could be implement in different way by using array or list but that should not a problem for the end user. End user just call the method and get the expected output and don't need think about the implementation as it is hide by the concepts encapsulation.

Saturday, July 12, 2014

ASP.NET MVC 5 sample project

I will show how to create a sample asp.net mvc 5 project. First I will use visual studio 2013 web express for creating asp.net mvc 5 project. I create a new project by File> New Project > ASP.NET Web Application > MVC project. Now according to our requirements I create model schema customer.
 public class Customer 
    {
        [Key]
        public int CustomerId { get; set; }

        [Required]
        [StringLength(256, ErrorMessage = "Company cannot be longer than 256 characters.")]
        [Display(Name = "Company")]
        public string Company { get; set; }
                             
        [Required]
        [StringLength(256, ErrorMessage = "First Name cannot be longer than 256 characters.")]
        [Display(Name = "First Name")]
        public string Name { get; set; }

        [Required]
        [StringLength(256, ErrorMessage = "Last Name cannot be longer than 256 characters.")]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }
        
        [Required]
        [StringLength(64, ErrorMessage = "Street cannot be longer than 64 characters.")]
        [Display(Name = "Street")]
        public string Street { get; set; }

        [Required]
        [StringLength(64, ErrorMessage = "Ciy cannot be longer than 64 characters.")]
        [Display(Name = "City")]
        public string City { get; set; }

        [Required]
        [StringLength(64, ErrorMessage = "Province Name cannot be longer than 64 characters.")]
        [Display(Name = "Province")]
        public string Province { get; set; }

        [Required]
        [StringLength(10, ErrorMessage = "Post Code cannot be longer than 10 characters.")]
        [Display(Name = "Post Code")]
        [DataType(DataType.PostalCode)]
        public string PostCode { get; set; }

        [Required]
        [Display(Name = "Telephone Number")]
        [StringLength(16, ErrorMessage = "Telephone cannot be longer than 16 characters.")]
        [DataType(DataType.PhoneNumber)]
        public string Telephone { get; set; }

        [Display(Name = "Fax Number")]
        public string Fax { get; set; }

        [Required(ErrorMessage = "Email Address is required.")]
        [Display(Name = "Email")]
        [DataType(DataType.EmailAddress)]
        public string Email { get; set; }

        [Display(Name = "Registration")]
        public string Registration { get; set; }

        [Required]
        [Display(Name = "Creation Date")]
        [DataType(DataType.Date)]
        public DateTime CreationDate { get; set; }
                           
       }
I used data annotation for validating data for our schema that is consist on System.ComponentModel.DataAnnotations.Schema . The [Key] annotation work as a primary key in the schema. Then I created a contract schema.
public class Contract
    {
        [Key]
        public int Id { get; set; }

        [Required]
        public virtual int CustomerId { get; set; }

        [ForeignKey("CustomerId")]
        public virtual Customer Customer { get; set; }

        [Required]
        [Display(Name = "Start Date")]
        [DataType(DataType.Date)]
        public DateTime StartDate { get; set; }

        [Required]
        [Display(Name = "End Date")]
        [GenericCompare(CompareToPropertyName = "StartDate", OperatorName = GenericCompareOperator.GreaterThanOrEqual)]
        [DataType(DataType.Date)]
        public DateTime EndDate { get; set; }

        [Required]
        [Display(Name = "License")]
        public Guid License { get; set; }

       
    }
The two table has One to zero -one relationship between them. So I created a customer instances in contract class with CustomerId as Foreign key. After creating model we will create a context class. The context class coordinates Entity Framework functionality for a given data model is the database context class. I added model class in the context class.
public class ByWayContext: DbContext
{
        static ByWayContext() 
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<ByWayContext>());
        }

        public DbSet<Customer> Customers { get; set; }

        public DbSet<Contract> Contracts { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            Database.SetInitializer<ByWayContext>(null);
        }
}
After creating model we will create controller(MVC5 Controller with View, Using entity Framework) based on our model.
Now automatically create,edit, list and delete option created for customer model class.

Tuesday, July 26, 2011

Make your enter key work like tab in ASP.NET using jquey

 As I am developing a solution for Insurance industry I have some form where huge data entry required. There are plenty of data entry operator who entry data in the form. As they are currently using oracle form where enter key work like tab in windows application. So my department head told me make data entry form like oracle form where operator will use enter key like tab as it is there practice. So I used javascripts and jquery for solving this problem.

  <script type="text/javascript">
    $(document).ready(function(){
        $("input").not( $(":button") ).keypress(function (evt) {
        if (evt.keyCode == 13) {
        iname = $(this).val();
        if (iname !== 'Submit'){
            var fields = $(this).parents('form:eq(0),body').find('button, input, textarea, select');
            var index = fields.index( this );
        if ( index > -1 && ( index + 1 ) < fields.length ) {
            fields.eq( index + 1 ).focus();
        }
        return false;
       }
    }
   });
  });
</script>

Please add this scripts in your data entry page and add jquery-1.4.1.min.js in your page header link. Now enter key will work like tab but when its found button with text submit will work like button.
Hope it will work for you. 











  

Sunday, July 17, 2011

How to clear the previous history of asp.Net textbox

Its a problem everybody faced in their browser. The browser cookie saved the textbox previous history. Sometimes it become security issue.
Click on image to view large
So we should clear previous history from textbox. So I just write a textbox properties in Page_Prerender event in my page(codebehind).
protected void Page_PreRender(Object sender, EventArgs e)
{
     txtPassword.Attributes.Add(“autocomplete”, “off”)
     txtConfirmPassword.Attributes.Add(“autocomplete”, “off”);
}
Hope It will solve your problem too.

Oracle database connection from ASP.NET(C#)

Here is the class DBconnection wrote for connecting oracle database from my asp.net 3.5 application.
I just create a oracle static connection properties. When any body wants to create connection the connection properties first check whether the connection exist or not. If connection exist it just return the connection. If connection is not exist then new connection will be created and returned.




Click on image to view large
Hope you it will help you connecting oracle database from ASP.NET.



Wednesday, July 13, 2011

Autocomplete textbox in ASP.NET using Ajaxcontroltoolkit

AutoComplete is an ASP.NET AJAX extender that can be attached to any TextBox control, and will associate that control with a popup panel to display words that begin with the prefix typed into the textbox.


To make a textbox autocomplete suggestion we can use jquery or ASP.NET ajaxcontroltoolkit. Today I will try to describe how to make autocomplete textbox using ajaxcontroltoolkit. I think its not a complicated work but I saw a lot of question in different forum. Let starts......

We need a  textbox and autocomplete item from visual studio 2008 toolbox.
We need web service. For adding web service a need a web service class. We can add web service by following.

Then we add a method GetCompany that returns string array. We will fetch data from database using prefix string than convert the list of data by string array format. Please check following.
Then we call the method from the ajax autocomplete tool. Like this..

Here is autocomplete tool necessary properties. Service method is the method we need to call from web service class. Service path is the web service path where the asmx page reside.
 You should add a script manager in your page top. 
Hope its done. How simple it is?
Example.








Search This Blog

Visitor Counter