Posts

Showing posts with the label C#

New features in different version of asp.net mvc

ASP.NET MVC3 New Project Templates for HTML 5 and CSS 3 Added new Razor View Engine Support for Multiple View Engines(Razor & Aspx) Support for Unobtrusive JavaScript Partial page output caching Improved Model validation Controller improvements(Viewbag) Improved Dependency Injection ASP.NET MVC 4 ASP.NET Web API Provided Bundling and Minification Asynchronous Controllers using Task Supported OAuth and OpenID Supported Windows Azure SDK 1.6 and upper Adaptive rendering Empty Project Template Mobile Project Template Add Controller using right click ASP.NET MVC5 ASP.NET Identity Authentication Filters Bootstrap  Attribute Routing Filter Overrides ASP.NET MVC6 (vNext) JSON based project extension Single Programming Model Improved Cloud Computing Side by side deployment Used Roslyn compiler

Get OAuth 2.0 access token of Paypal using HttpClient in c#

Here called paypal Rest API using System.Net.Http.HttpClient for OAuth 2.0 access token try  { // ClientId of your Paypal app API          string  API ClientId = " ASCxaD7FzuvGbX-xxxxxxxx.....xxxxxxxxxxx-6RZDgwLuOyrCsy3 " ;                 // secret key of you Paypal app API string   API Secret = " ENldyFXuW46R7Wv0 xxxxxxxx.....xxxxxxxxxxx ee8pyH8 " ;          using ( var client = new System.Net.Http. HttpClient ())         {              var byteArray = Encoding .UTF8.GetBytes( API ClientId + ":" + API Secret );             client.DefaultRequestHeaders.Authorization = new Headers. AuthenticationHeaderValue ( "Basic" , Convert .ToBase64String(byteArray));              var url = new Uri ( "https://api.sandbox.paypal.com/v1/oauth2/token" , UriKind .Absolute);             client.DefaultRequestHeaders.IfModifiedSince = DateTime .UtcNow;              var requestParams = new List < KeyValuePai

Generics method using interface in c#

Implement an Interface with Generic Methods namespace  Win.Data { // Created Interface for field of table      public   interface   IFieldTable     {          string  Id {  get ;  set ; }          DateTime  TimeStamp {  get ;  set ; }          string  User {  get ;  set ; }          string  View {  get ;  set ; }          string  Name {  get ;  set ; }          //string QData { get; set; }     } // FieldString class inhirated from IFieldTable      public   class   FieldString  :  IFieldTable     {          public   string  Id {  get ;  set ; }          public   DateTime  TimeStamp {  get ;  set ; }          public   string  User {  get ;  set ; }          public   string  View {  get ;  set ; }          public   string  Name {  get ;  set ; }          public   string  Data {  get ;  set ; }     } // FieldInt class inhirated from IFieldTable      public   class   FieldInt  :  IFieldTable     {          public   string  Id {  get ;  set ; }          public  

What does generics in c#

Definition : Generics means allows to defining a class or method without particular any data type. It is a improve performance, productivity and type-safety. It is also said parametric polymorphism. It is most commonly use with collections like linked lists, arrays, stacks, queues, hash tables, enumerable queryable, trees etc. Perform operations such as add and delete items from the collection with regardless of the data type. Benefits : Casting is not required for accessing items from the collection Type-safe while execution Reusable code for multiple types of data Improve the performance and productivity Boxing and Unboxing are not required Where List<T> collection class is an example for generic class that can be used for add, remove and search an item of any type (T) that passed as parameter to it. Examples :    class SampleClass1<T> : IDisposable     {         public void Dispose()         {             throw new NotImplementedException();    

Multiple foreign keys within same table using code frist enitty framework and navigation property

Multiple foreign keys within same table using code frist enitty framework and navigation property in parent table In this example show you, how to work with multiple foreign key reference to the same table using Code First Entity Framework and Navigation Property First we need to create a two table User and contactNumber, where User table is primary table and contactNumber is a secondary table public class User {      [ DatabaseGenerated ( DatabaseGeneratedOption .Identity)]      public int Id { get ; set ; }      public string Name { get ; set ; }      public virtual ICollection < ContactNumbers > contactNumbers { get ; set ; } } public class ContactNumber {      [ DatabaseGenerated ( DatabaseGeneratedOption .Identity)]      public long Id { get ; set ; }      public int UserId { get ; set ; }   

Match substring surrounded by known prefix and suffix using regex in c#

Add prefix and/or suffix to substring using regex match using c# Example 1: string strline = "This is 123 test string"; strline = Regex.Replace(strline, @"([\d])", "[$1]"); Console.WriteLine(strline); Out Put: is [1][2][3] test string Example 2: string strline = "That's first [good] mark_test"; strline  = Regex.Replace(strline, @"([^A-Za-z0-9 ])", "\\$1"); Console.WriteLine(strline); Out Put: That\'s first \[good\] mark\_test Example 3: string strline = "Hello [123] Frinds"; strline  = Regex.Replace(strline, @"([^\[])(\[\d+\])(.*)", "$1xxxxxxxxx $2 xxxxxxxxx$3"); Console.WriteLine(strline); Out Put:    Hello xxxxxxxxx [123] xxxxxxxxx Frinds Note: In this example $1, $2, $3 means respective value of match group

Send mail using gmail account from C#

            try             {                 //Creating the object of MailMessage                 MailMessage message = new MailMessage();                 //Creating the object of SmtpClient                 SmtpClient smtp = new SmtpClient();                 //To Emial Address                 message.To.Add(new MailAddress("To Email Address"));                 //From Email Address                 message.From = new MailAddress("From Email Address");                 //It is a subject of your email                 message.Subject = "Test";                 //Body of the mail                 message.Body = "Content";                 //Set the true if Html Body , otherwise false                 message.IsBodyHtml = true;                 //set the priority of email                 message.Priority = MailPriority.High;                 smtp.Port = 587;                 smtp.Host = "smtp.gmail.com";