Wednesday, February 1, 2017

What's New Features in C# 5.0

*** C# Evolution Matrix ***
Microsoft just published a new version of C# : 5.0 beta with CLR version 4.5 (Visual Studio 11 beta).
In order to get a big picture of the whole evolution of C# language, I summarized all the key features
into a C# Evolution Matrix for your reference as below diagram shows:



In C# version 5.0, there are two key features: Async Programming and Caller Information.
1. Async Feature
Two new key words are used for Async feature: async modifier and await operator. Method marked
with async modifier is called async method. This new feature will help us a lot in async programming.
For example, in the programming of Winform, the UI thread will be blocked while we use
HttpWebRequest synchronously request any resource in the Internet. From the perspective of user
experience, we cannot interact with the form before the request is done.
Old methodology:
private void btnTest_Click(object sender, EventArgs e)
{
var request = WebRequest.Create(txtUrl.Text.Trim());
var content=new MemoryStream();
using (var response = request.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
responseStream.CopyTo(content);
}
}
txtResult.Text = content.Length.ToString();
}

In the above example, after we clicked the Test button, we cannot not make any change to the form
before the txtResult textbox shows the result.
In the past, we can also use BeginGetResponse method to send async request as this sample in MSDN
shows:
http://msdn.microsoft.com/zh-cn/library/system.net.httpwebrequest.begingetresponse(v=vs.80).aspx. But it
will takes us a lot effort to realize it.
New Logic:

Now, we can simply use below code to do request asynchronously :
private async void btnTest_Click(object sender, EventArgs e)
{
var request = WebRequest.Create(txtUrl.Text.Trim());
var content = new MemoryStream();
Task<WebResponse> responseTask = request.GetResponseAsync();
using (var response = await responseTask)
{
using (var
responseStream = response.GetResponseStream())
{
Task copyTask = responseStream.CopyToAsync(content);
//await operator to supends the excution of the method until the task is completed. In the meantime,
the control is returned the UI thread.
await copyTask;
}
}
txtResult.Text = content.Length.ToString();
}
The await operator is applied to the returned task. The await operator suspends execution of the
method until the task is completed. Meanwhile, control is returned to the caller of the suspended
method.
2.Caller Information
Caller Information can help us in tracing, debugging and creating diagnose tools. It will help us
to avoid duplicate codes which are generally invoked in many methods for same purpose, such
as logging and tracing.
We could get the below information of caller method :

Old Logic:

Below example are a common practice prior to the new feature of Caller Information:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace
ConsoleApplicationTest
{
class Program
{
static void Main(string[] args)
{
InsertLog(“Main”);
MethodB();
Console.ReadLine();
}
static void MethodA()
{
InsertLog(“MethodA”);
MethodB();
}
static void MethodB()
{ }
static void
InsertLog(string methodName)
{
Console.WriteLine(“{0} called method B at {1}”, methodName,
DateTime.Now);
}
}
}
New Logic:
In both Main and MethodA methods, method InsertLog is invoked for logging. Now we can change the
codes to be as per below lines:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace
ConsoleApplicationTest
{
class Program
{
static void Main(string[] args)
{
//InsertLog(“Main”);
MethodB();
Console.ReadLine();
}
static void MethodA()
{
//InsertLog(“MethodA”);
MethodB();
}
static void MethodB(
[CallerMemberName] string memberName = “”,
[CallerFilePath] string sourceFilePath = “”,
[CallerLineNumber] int sourceLineNumber = 0)
{
InsertLog(memberName);
}
static void
InsertLog(string methodName)
{
Console.WriteLine(“{0} called method B at {1}”, methodName,
DateTime.Now);
}
}
}

New Feature:
Binding Operators:

Binding Operators may be C# 5.0. The support for Binding on .NET objects is awesome. I came to know this from Chris on his post where he speaks how Binding operators looked like long ago. The Delphi assignment operator := is now being used for this purpose. :=: would be used for two way binding. Thus:
comboBox1.Text :=: textBox1.Text; 
means there would be two way binding between the properties so that when ComboBox1.Text changes its Text, it will automatically change the value of Textbox1.Text. Binding is actually not a new thing to the system. In WPF, we already have Binding in place, but for that we need the properties to have implemented from INotifyPropertyChanged. So it is not in the language, the WPF objects are doing this internally by handling the PropertyChanged event on the object. But if that is included as an operator, it would be a cool feature to add on.

Summary:
The new features in C# 5.0 will help us to code more easily and improve the productivity. 

References:
https://blogs.msdn.microsoft.com/mvpawardprogram/2012/03/26/an-introduction-to-new-features-in-c-5-0/

Wednesday, December 28, 2016

How to Compare Two Dates using JQuery.

How to Compare Two Dates using JQuery ?


function compareTwoDates(Date1, Date2) {
    var D1 = Date1.split(".");
    var bool = false;
    D1 = new Date(D1.pop(), D1.pop() - 1, D1.pop());
    var D2 = Date2.split(".");
    D2 = new Date(D2.pop(), D2.pop() - 1, D2.pop());
    if (D1 >= D2)
        bool = true;
    return bool;
}

How to get Current Date and Increment and Decrement date using JQuery

How to get Current Date using JQuery ?

 function getCurrentDate() {
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth() + 1; //January is 0!
    var yyyy = today.getFullYear();

    if (dd < 10) {
        dd = '0' + dd
    }

    if (mm < 10) {
        mm = '0' + mm
    }

    today = dd + '.' + mm + '.' + yyyy;
    return today;
}


Increment Date:
This function will give increment date by no. of days

Ex: current date 02.02.2017 increment by 2 days date will be 04.02.2017.

inputs are: incrementVal= 2 , and dateParam= 02.02.2017

function incrementDate(incrementVal, dateParam) {
    if (!isEmptyString(dateParam)) {
        var D1 = dateParam.split(".");
        dateParam = new Date(D1.pop(), D1.pop() - 1, D1.pop());
        dateParam.setDate(dateParam.getDate() + parseInt(incrementVal));

        var decrementedDate = ddmmyyyyFormat(dateParam);
        return decrementedDate;
    }

}

Result:
output:  04.02.2017.

Decrement Date:


This function will give decrement date by no. of days

Ex: current date 02.02.2017 decrement by 2 days date will be 31.01.2017.

inputs are: incrementVal= 2 , and dateParam= 02.02.2017

function decrementDate(decrementVal, dateParam) {
    if (!isEmptyString(dateParam)) {
        var D1 = dateParam.split(".");
        dateParam = new Date(D1.pop(), D1.pop() - 1, D1.pop());
        dateParam.setDate(dateParam.getDate() - parseInt(decrementVal));

        var decrementedDate = ddmmyyyyFormat(dateParam);
        return decrementedDate;
    }
}

Result:
output:  31.01.2017.

Format the Date: custom date format

This function will format the date as 02.02.2017.
inputs: date =current date.

function ddmmyyyyFormat(date) {
    var dateFromString = (('' + date.getDate()).length < 2 ? '0' : '') + date.getDate() + '.' +
        (('' + (date.getMonth() + 1)).length < 2 ? '0' : '') + (date.getMonth() + 1) + '.' +
         date.getFullYear();
    return dateFromString;
}

output:02.02.2017.


Sunday, October 23, 2016

How To Send emails from C# Application using SMTP Server.

             

                MailMessage mail = new MailMessage();
                SmtpClient smtpclient = new SmtpClient("smtp.gmail.com");
                mail.From = new MailAddress("k.vijaykumar0436@gmail.com");
                mail.To.Add("vijaynow2@gmail.com");
                mail.Subject = "Test Mail";
                mail.Body = mailBody;
                smtpclient.Port = 587;
                smtpclient.Credentials = new
                System.Net.NetworkCredential("k.vijaykumar0436@gmail.com", "password");
                smtpclient.EnableSsl = true;

                smtpclient.Send(mail);
                Console.Write("succussfullyy......");

Tuesday, February 16, 2016

Important links for .net

http://myview.rahulnivi.net/getting-started-angular-js/

Config-less Hosting

Hi guys,
In this section, we will look at Config-Less Hosting.
WCF does not require config file. It only requires endpoint information. It does not matter which medium it is coming from. You can provide all these information from the code itself. You can get all mandatory information from some source say xml file or database to construct the endpoint. I will give the demo by simply hardcoding the same. Let us go to console hosting. Nevertheless, before making any change in code, let me just comment the config section for WCF in APP.config file. Below is the snippet in the finished state.
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using MovieLib.Contracts;
using MovieLib.Services;
namespace ConsoleHostApplication
{
class Program
{
static void Main(string[] args)
{
ServiceHost hostMovieManager = new ServiceHost(typeof(MovieManager));
string address = "net.tcp://localhost:8010/MovieService";
Binding binding = new NetTcpBinding();
Type contract = typeof (IMovieService);
hostMovieManager.AddServiceEndpoint(contract, binding, address);
hostMovieManager.Open();
Console.WriteLine("Service Launched,Press Enter to Exit!");
Console.ReadLine();
hostMovieManager.Close();
}
}
}
Let me explain the code a bit. As you can see, I have provided endpoint procedurally. One point to note here is that all of these binding classes inherit from common base class binding. Here, I have created an instance of NetTcpBinding. Then I instantiated a type of my contract. In the end, add I have added endpoint with AddServiceEndpoint method. With the above code change in place, when I build and run the same, it produced the below output.
105th
Similar thing can be achieved with web host as well. We will delve further in the coming section. Till then, stay tuned and Happy Coding,
Thanks,
Vijay Kumar Kandala.
Happy Coding

Friday, December 11, 2015

The computer restarted unexpectedly or encountered an unexpected error. windows installtion can not proceed to install windows click ok to restart the computer and then restart the installation

1)      On the same error screen press SHIFT+F10 and it will open up a black Window. (a command prompt window)
2)       In  the black windows type the word "regedit"(without the inverted commas and hit enter. That will open the registry editor.
        In the Registry Editor..
                Browse to....
        HKLocal machine/SYSTEM/SETUP/STATUS/ChildCompletion, and after highliting childcompletion,on the right hand side check for setup.exe. if the value is 1 change it to 3.(by double clicking on setup.exe and changing the value shown in the box).
Then close the registry editor and then also close the black window.
      Then click on OK for the error and the computer will restart and the installation may complete.  just try it and let me know if it worked.