Search This Blog

Tuesday, December 22, 2015

What is ADO.NET

ADO.NET provides a set of components for creating distributed, data-sharing applications.

ADO.NET provides consistent access to data sources such as SQL Server and XML, and to data sources exposed through OLE DB and ODBC. Data-sharing consumer applications can use ADO.NET to connect to these data sources and retrieve, handle, and update the data that they contain.









Clear filters RadGrid Telerik

Telerik > RadGrid > Clear filter

foreach (var column in RadGrid1.MasterTableView.Columns)
{
         column.CurrentFilterFunction = GridKnownFunction.NoFilter;
         column.CurrentFilterValue = string.Empty;
}

RadGrid1.MasterTableView.FilterExpression = string.Empty;
RadGrid1.MasterTableView.Rebind();





Monday, December 21, 2015

Print directly RadGridView

Telerik > Windows Forms > RadGridView > Print directly 


radGridView1.Print();







Thursday, December 10, 2015

Clear contents of a file c#

C# > Clear contents of a file

WriteAllText creates a new file, write the contents to the file, and then closes the file. If the target file already exists, it is overwritten.

Example

 System.IO.File.WriteAllText(filename, string.Empty);





Monday, December 7, 2015

Remove unused usings Visual Studio

Visual Studio > Remove Unused Usings

Right mouse click > Organize Usings









Wednesday, December 2, 2015

XDocument C# Example

C# > XML > System.Xml.Linq > XDocument

XDocument is an XML document.

Example


XDocument xDocument = new XDocument(
            new XElement("Hours",
                new XElement("Emp1", "0"),
                new XElement("Emp2", "25"),
                new XElement("Emp3", "40"),
                new XElement("Emp4", "30")
                )
            );

Create a new document by filtering data using Linq

XDocument doc = new XDocument(
           new XElement("Report",
                    from el in xDocument.Element("Hours").Elements()
                    where ((int)el) >=30
                    select el
                )

            );

Result contains two records:







default keyword c#

C# > Keywords > default

This keyword is used in two ways.

  • default label in switch statement
  • generics: the default value
    • null for reference type
    • 0 for value type




Tuesday, November 24, 2015

Autofac Modules C# Tutorial

C# > Patterns > Dependency Injection  > Autofac Modules


Create a project with the following structure:

Project.cs

namespace AutofacModules
{
    public class Project
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

Task.cs

namespace AutofacModules
{
    public class Task
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

IProjectRepository.cs

namespace AutofacModules
{
    public interface IProjectRepository
    {
        void Create(Project project);
    }
}

ITaskRepository.cs

namespace AutofacModules
{
    public interface ITaskRepository
    {
        void Create(Task task);
    }
}

ProjectModule.cs

Register a component to be created through reflection.

using Autofac;
namespace AutofacModules
{
    public class ProjectModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType<ProjectRepository>().As<IProjectRepository>();
            base.Load(builder);
        }
    }
}







TaskModule.cs

using Autofac;
namespace AutofacModules
{
    public class TaskModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType<TaskRepository>().As<ITaskRepository>();
            base.Load(builder);
        }
    }
}

RegistrationModule.cs

Add a module to the container.

using Autofac;
namespace AutofacModules
{
    public class RegistrationModule
    {
        public static IContainer BuildContainer()
        {
            var builder = new ContainerBuilder();

            builder.RegisterModule<ProjectModule>();
            builder.RegisterModule<TaskModule>();

            return builder.Build();
        }
    }
}

Example:

            Project project = new Project()
            {
                Id = 1,
                Name = "Project 1"
            };

            Task task = new Task()
            {
                Id = 1,
                Name = "Task 1"
            };

            var container = RegistrationModule.BuildContainer();

//Retrieve a service from the context.

            var projectRepository = container.Resolve<IProjectRepository>();
            var taskRepository = container.Resolve<ITaskRepository>();

            projectRepository.Create(project);
            taskRepository.Create(task);









Friday, October 30, 2015

Dependency Injection in C# with Autofac - real world example

C# > Patterns > Dependency Injection  > Autofac

Autofac is an addictive Inversion of Control container for .NET 4.5, Silverlight 5, Windows Store apps, and Windows Phone 8 apps

Source: http://autofac.org/

Dependency Injection in C# with Autofac - real world example

using Autofac;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public class Programmer
    {
        public string Name { get; set; }
        public IList<string> Skills { get; set; }
    }




    public class Project
    {
        public string Name { get; set; }
        readonly IList<Programmer> _programmers;
        readonly IDispatcher _dispatcher;

        public Project(IList<Programmer> programmers, IDispatcher dispatcher)
        {
            _programmers = programmers;
            _dispatcher = dispatcher;
        }

        public void FindProgrammers()
        {
            var programmers = _programmers.Where(e => e.Skills.Contains("VB"));

            foreach (var p in programmers)
                _dispatcher.Notify(p);
        }
    }

    public interface IDispatcher
    {
        void Notify(Programmer programmer);
    }

    public class Email : IDispatcher
    {
        private readonly string _message;

        public Email(string message)
        {
            _message = message;
        }

        public void Notify(Programmer programmer)
        {
            Console.WriteLine("Sent message {0} to {1}", _message,  programmer.Name);
        }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            List<Programmer> lst = new List<Programmer>();
            Programmer p = new Programmer();
            p.Name = "Dan";
            p.Skills = new List<string>();
            p.Skills.Add("VB");
            lst.Add(p);

            Email email = new Email("Notification email message");

            ContainerBuilder autofac = new ContainerBuilder();
            autofac.Register(o => new Project(o.Resolve<IList<Programmer>>(), o.Resolve<IDispatcher>()));

            autofac.RegisterType<Email>().As<IDispatcher>();
            autofac.RegisterInstance(lst).As<IList<Programmer>>();
            autofac.RegisterInstance(email).As<IDispatcher>();
           
            using (var container = autofac.Build())
            {
                container.Resolve<Project>().FindProgrammers();
            }

        }
    }
}