Skip to main content

Posts

Showing posts from 2011

How to create self signed certificate using makecert

In this post,I will show you how to create self signed certificate for wcf application.For this,I will used the utility makecert.MakeCert.exe allows you to (for test/dev purposes) generate both a trusted root certificate and a certificate signed by that trusted root certificate for encryption purposes (also for signature purposes.
Open visual studio command prompt (Admin mode) and type following command 
C:\cert>makecert -pe -n "CN=RootCA" -ss My -sr LocalMachine -a sha1 -sky signatu re -r "RootCA.cer" -pe Marks the generated private key as exportable. This allows the private key to be included in the certificate.-Specifies the subject's certificate name. This name must conform to the X.500 standard. The simplest method is to specify the name in double quotes, preceded by CN=; for example, "CN=myName".-ss Specifies the subject's certificate store name that stores the output certificate.sr >localmachine-a Specifies the signature algorithm…

How to validate asp.net form using Jquery

In this post I will show you how to validate asp.net form using jqery. Create a new website,add a new js file ,and add following code inside it function validateForm(e) { var formIsValid = true; // check that a valid email address has been entered var emailRegExp = /[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/; if (!emailRegExp.test(String($("#txtEmail").val()).toUpperCase())) { addError("txtEmail", "Please enter a valid email address."); formIsValid = false; } else { removeError("txtEmail"); } // check that first name has one or more characters if ($("#txtFirstName").val() == '') { addError("txtFirstName", "This field is required."); formIsValid = false; } else { removeError("txtFirstName"); } // check that last name has one or more characters if ($("#txtLastName").val() == '') { …

How to enable/disable dropdownlist through a checkbox selection

In this post,I will show you how to enable/disable dropdownlist control through checkbox selection in asp.net.

Create a new website Right click on the website,add a new webpage,and add following code into it


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title></title><scriptsrc="Scripts/jquery-1.4.1.js"type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { $("#chkEnable").click(function () { if (this.checked) $('#ddlList').attr('disabled', 'disabled');���������������…

How to change default binding in WCF 4.0

With WCF 3.x, if you try to host a service without any configured endpoints, the ServiceHost instance will throw an exception informing you that you need to configure at least one endpoint. With WCF 4, this is no longer the case because the runtime automatically adds one or more “default endpoints” for you, thereby making the service usable without any configuration.
If you want to change default binding then open web.config and do following entry in <system.serviceModel>

<system.serviceModel>

<protocolMapping>

<add binding="basicHttpBinding" scheme="Http"/>

</protocolMapping>


</system.serviceModel>

How to upload/Download file from wcf service

In this post,I will show you how to stream (upload and download) data through wcfservice.
Create a new websiteRight click on the website and add new wcf service named FileStream.svcOpen the IFileStream.cs and add following codeusing System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using System.IO; [ServiceContract] publicinterface IFileStream { [OperationContract] string Upload(Stream inputStream); [OperationContract] Stream Download(string fileId); [OperationContract] string[] GetAvailableFiles(); }
Open FileStream.cs file and add following code inside it
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using System.IO; using System.Web; // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in cod…

How to implement Adapter pattern in asp.net

In this post,I will show you how to implement Adapter patter in asp.net.Before going to implementation details let's dive into adapter pattern definitions.
What is  an Adapter Pattern?
The adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface

How’s it implemented? 1.You want to use existing class,and its interface doe not match the one you need.
2.You want to create a reusable class that cooperates with unrelated classes with incompatible interface
Problem:-
Many of us use the .netCacheclass for storing objects into memory. Suppose later if we find that some other third party Cache management library that  is better than existing Cache class , then the project would not change just adapter internally would call the newCacheclass.



How to remove duplicates elements from List using IEqualityComparer

In this post,I will show you how to remove duplicate record from List using IEqualityComparer.It has two methods to support the comparison of objects for equality.Let us suppose that you have a Employee class that has four properties EmployeeID,FirstName,LastName and Age.publicclass Employee { public Employee() { } publicstring EmployeeID { get; set; } publicstring FirstName { get; set; } publicstring LastName { get; set; } publicint Age { get; set; } public List&lt;Employee&gt; Employees { get { returnnew List&lt;Employee&gt;() { new Employee(){EmployeeID="E001",FirstName="F001",LastName="L001",Age=21}, new Employee(){EmployeeID="E002",FirstName="F002",LastName="L002",Age=25}, new Employee(){EmployeeID="E003",FirstName="F003",LastName="L003",Age=26}, new Employee(){EmployeeID="E001",Firs…

How to sum the value of gridview column using jquery

In this post I will show how to sum the value of gridview column on selection of checkbox .Let us suppose that you have a gridview which has three column named Name,Quantity and Price.Now your requirement is to sum the value of price filed on selection of checkbox.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><scriptsrc="Scripts/jquery-1.4.1.js"type="text/javascript"></script> <title></title> <script type="text/javascript"> $(document).ready(function () { var total; //Column index value of price field (Column index start from 1)var columnIndexValue = 4; varchecked…

How to compress and decompress data in c#

In this post,I will show you how to compress and decompress data in c# using GzipStream class.Below is the code for the same
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.IO.Compression; namespace CompressDecompress { class Program { conststring CompressFilePath = @"C:\temp\data.zip"; conststring Filename = @"C:\temp\data.txt"; privateconststring DeCompressFilePath = @"C:\temp\data1.txt"; staticvoid Main(string[] args) { Compress(Filename); Decompress(CompressFilePath); } privatestaticvoid Decompress(string compressFilePath) { using (FileStream inputStream = new FileStream(compressFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { using (FileStream outputStream = new FileStream(DeCompressFilePath, FileMode.Create, FileAccess.ReadWrite)) { using (GZipStream g…

How to read pdf document properties in asp.net

In this post, I will show you how to read the pdf document properties in asp.net.As we know that there is no- such  in-built class in .net framework that read the pdf document.So,for this demo,I will used third-party library iTextSharp.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PdfPropertiesReader.aspx.cs" Inherits="PdfPropertiesReader" %><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title></title></head><body><formid="form1"runat="server"><div><divid="pdfProperties"runat="server"></div></div><asp:ButtonID="Button1"runat="server"OnClick="Button1_Click"Text="Read Pdf Properties"/></form>&…

How to convert IEnumerable to Html table in asp.net

In this post I will show how to convert IEnumerable to string (Html table).Assume you have a customer class which has three properties named FirstName,LastName and Age.Now,you want to display customer collection on UI.
For this I have created following method which retrives propertyname and value dynamically (using reflection) and return result as string
public static string ToHtmlTable(IEnumerable list, string tableSyle, string headerStyle, string rowStyle, string alternateRowStyle)<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title></title><styletype="text/css"> .headerStyle { background-color: #abc;…

How to fetch header info from given url

In this post I will show how to fetch header info from given url using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; namespace SNIPPET_CS { publicclass HeaderInfo { publicstring Key { get; set; } publicstring Value { get; set; } publicstatic List<HeaderInfo> GetHttpResponseHeaders(string url) { List<HeaderInfo> headerList = new List<HeaderInfo>(); WebRequest webRequestObject = WebRequest.Create(url); WebResponse responseObject = webRequestObject.GetResponse(); if (responseObject != null) { headerList.AddRange(from string headerKey in responseObject.Headers select new HeaderInfo() { Key = headerKey, Value = responseObj…

How to send output of console application to file

In this example,I will show how to send console output to file in c# using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; namespace SNIPPET_CS { class Program { staticvoid Main(string[] args) { ConsoleOutput(); Console.WriteLine("Console output saved successfully"); Console.ReadLine(); } privatestaticvoid ConsoleOutput() { conststring applicationPath = @"D:\Windows\System32\ipconfig.exe"; //string ApplicationArguments = "-c -x";// Create a new process object Process processObj = new Process(); // StartInfo contains the startup information of// the new process processObj.StartInfo.FileName = applicationPath; //ProcessObj.StartInfo.Arguments = ApplicationArguments;// These two optional flags ensure that no DOS window// appear…

How to create an Enum value from the string

This is a simple code for converting string representation to Enum using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Blog_CS { publicenum State { New, Ready, Stop, Reset }; class Program { privatestaticvoid Main(string[] args) { conststring state = "New"; State currentState = (State)Enum.Parse(typeof(State), state); if (currentState == State.New) { Console.WriteLine("Run state"); } } } }

How to add custom sorting for a class with IComparable interface

Sometimes it necessary to sort your collection before showing it on UI.You can add custom sorting to you class just by inheriting it with  IComparable interface.IComparable interface contains one single method CompareTo.

For this post I have created a class Employee which has four properties FirstName,LastName,Age and Title.This class implements IComparable interface,which means instance of the class can be compared with other instance of this class.

using System; using System.Collections.Generic; using System.Text; using System.IO; namespace Blog_CS { publicclass Employee : IComparable<Employee> { publicstring FirstName { get; set; } publicstring LastName { get; set; } publicint Age { get; set; } publicstring Title { get; set; } publicstatic List<Employee> Employees { get { returnnew List<Employee>() { new Employee(){FirstName = "F004",LastName = &…