Skip to main content

Posts

Showing posts from 2010

How to:Create wcf application without using config file

In this post i am going to show you how to create wcf application without using config file. Let's start the tutorial.
Open visual studio and create a new console application Right click on the project and add reference to following assemblyusing System.ServiceModel;
Open Program.cs file and paste following code
[ServiceContract]     interface IService     {         [OperationContract]         string HelloWorld(string strMessage);     }     publicclass ServiceImp : IService     {         [OperationBehavior]         publicstring HelloWorld(string strMessage)         {             return ("Hello " + strMessage + "!");         }     }
Now Add following code inside main method

staticvoid Main(string[] args)         {             //Create a URI to serve as the base address             Uri httpUrl = new Uri("http://localhost:8090/MyService");             //Create ServiceHost             ServiceHost host = new ServiceHost(typeof(ServiceImp), httpUrl);          …

Linq to Sql: Left outer join

In this post i am going to show how to generate left outer join using linq to sql. For this post i am going to use Categories and Product table of northwind. First way to generate left outer join var query=from p in Products join c in Categories on p.CategoryID equals c.CategoryID into pc where p.CategoryID==1 from j in pc.DefaultIfEmpty() select new { CategoryName=j.CategoryName, ProductName=p.ProductName }; ouput generated code SELECT [t1].[CategoryName] AS [CategoryName], [t0].[ProductName] FROM [Products] AS [t0] LEFTOUTERJOIN [Categories] AS [t1] ON [t0].[CategoryID] = ([t1].[CategoryID]) WHERE [t0].[CategoryID] = @p0 second way without using into operator var query=from p in Products from c in Categories.Where(x=>x.CategoryID==p.CategoryID).DefaultIfEmpty() select new { CategoryName=c.CategoryName, ProductName=p.ProductName …

Linq to Sql:Inner join

In this post i am going to show you how to generate inner join using linq to sql.
For this post i am going to use Categories and Product table of northwind.



Let's imagine that you want to fetch CategoryName and ProductName from above table using linq.



var query=from p in Products           join c in Categories           on p.CategoryID equals c.CategoryID           select new           {           CategoryName=c.CategoryName,           ProductName=p.ProductName           };

Jquery and CheckboxList

In this post i am going to show you how use jquery in asp.net to select the checkbox of Checkbox List <%@ Page Language="C#" AutoEventWireup="true" CodeFile="RadioButtonListJquery.aspx.cs" Inherits="RadioButtonListJquery" %><!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.3.2.js"type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { $("#selectAll").click(function () { $("input[type='checkbox'").not("#selectAll").each(function () { $(this).attr("checked", "true"); }); …

How DataContract serialize CLR object into XML

In this post,I am going to show you how DataContract export DataContract into xsd ,and then how an object is converted into xml.





using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Serialization; using System.Xml.Schema; using System.Xml; namespace DataContract_Demo {     [DataContract(Namespace = "http://aspdotnetcodebook.blogspot.com", Name = "Person")]     publicclass Person     {         [DataMember(Name = "FirstName")]         publicstring FirstName { get; set; }         [DataMember(Name = "LastName")]         publicstring LastName { get; set; }     }     class Program     {         staticvoid Main(string[] args)         {             GenerateXsd();             GenerateXML();         }         privatestaticvoid GenerateXsd()         {             XsdDataContractExporter exporter = new XsdDataContractExporter();             exporter.Export(typeof(Person));             foreach (XmlS…

How to add a static item to a databound dropdownlist

In this post i will show how to add static item to dropdownlist like a dropdown that displays one static item such as "select" while the rest of the items are data bound, then check out the code below: 

Set the AppendDataBoundItems property of the dropdownlist to True, and add in the static list item. Your dropdown will look something like this:

 <asp:DropDownList ID="ddlCountry" runat="server" AppendDataBoundItems="true">             <asp:ListItem Text="Select" Value="-1"></asp:ListItem>         </asp:DropDownList>
check out the complete code

<%@ 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="ser…

How to use anonymous and Lambda expression in .net

In this post I will discuss how to use anonymous and lambda expression in .net.Imagine we have a simple example in c# that simply add two numbers.
using System; using System.Collections.Generic; using System.Linq; using System.Text; class Program {     publicdelegateint Fn(int a, int b);     staticvoid Main(string[] args)     {         Fn add = new Fn(Add);         Console.WriteLine(add(1, 2));     }     publicstaticint Add(int a, int b)     {         return (a + b);     } }
Let’s refactor the delegate instance version of the code to use an anonymous method.What is anonymous method? Anonymous methods are a new language feature in C# 2.0. Anonymous methods allow us to define a code block where a delegate object is acceptable. Let’s convert the above code using anonymous code. Remove the following method from the delegate instance version
publicstaticint Add(int a, int b)         {             return (a + b);         }
And replace following code with
Fn add = new Fn(Add); With this on…

3 Tier Architecture in ASP.NET

In this post i will show how to create 3 -tier architecture in asp.net.Before going to technical details,I will describe some details about what is 3 tier architecture and what are the main components of 3 tier architecture.

The three-tier architecture was comes into existence to improve management of code and contents and to improve the performance of the web based applications.There are mainly three layers in three-tier architecture.These are define as follows…




1.  UI(InsertCustomer.aspx): First layer Presentation contains mainly the interface code, and this is shown to user. This code could contain any technology that can be used on the client side like HTML, JavaScript or VBScript etc.

2. Business Logic(CustomerBL.cs) : Second layer is Business Logic which contains all the code of the server-side .This layer have code to interact with database database and to query, manipulate, pass data to user interface and handle any input from the UI as well.

3. DataAccess Layer(CustomerDAL.c…

CASE Statement Equivalent in LINQ

In this post i will show you how how to generate "Case statement" from linq.Let's say that you want to select rows from [Order Details] table in Northwind database something like this: If the product UnitPrice is >10 then you want to display it as "High"If the product UnitPrice is <5 then you want to display it as "Low" otherwise "Average"
Our query should include the switch case  clause. Something like this:

SELECT
(
CASE
WHEN UnitPrice >10 THEN 'High'
WHEN UnitPrice <5 THEN 'Low'
ELSE 'Average'
END ) AS ProductPrice
FROM [Order Details]



using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace Linq_Tips { class Program { staticvoid Main(string[] args) { NorthwindDataContext dc = new NorthwindDataContext(); var query = from o in dc.Order_Details select new …

Linq and Sql "where in" Operator

In this post i will  show you how to generate "where in " clause in Linq to Sql. Imagine that we want to select rows from Products table in Northwind database where ProductId matches 3,4, 10. Our query should include the Where IN clause. Something like this:

SELECT *FROM Products WHERE ProductID in (3,4,10)

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace Linq_Tips { class Program { staticvoid Main(string[] args) { //Create a list List<int> list = new List<int>(); //Add items in list list.AddRange(newint[] {3, 4, 10 }); NorthwindDataContext dc = new NorthwindDataContext(); //Query database var query = from product in dc.Products where list.Contains(product.ProductID) select product; Console.WriteLine(query); …

How to use Jquery fullcalender in asp.net

This snippet will show how how to use Jquery fullcalender in asp.net Open Microsoft Visual Studio .NET. In Visual C# .NET, create a new website  named Fullcalender.Add  following code to Default.aspx page.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FullCalender.aspx.cs" Inherits="FullCalender" %><!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 src="Scripts/fullcalendar.js" type="text/javascript"></script>     <script src="Scripts/gcal.js" type="text/javascript"></script>     <link href="Styles/fullcalendar.css" rel="styleshe…

Linq and Reflection

This snippet will show how LINQ can be used with reflection to retrieve specific metadata about type that match a specified search criterion using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace Linq_Tips { class Program { staticvoid Main(string[] args) { Assembly assembly = Assembly.Load("System.Data, Version=3.5.0.0, Culture=neutral, " + "PublicKeyToken= b77a5c561934e089"); var Query = from type in assembly.GetTypes() where type.IsClass select type; foreach (var item in Query) { Console.WriteLine(item.Name); } Console.WriteLine("Press any key to exit ... "); Console.ReadKey(); } } } 

How to extract number from string using Linq

Imagine you have a string("ABCDE99F-J74-12-89A") and you want to extract only number from the string. This snippet will show how to extract number from string. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Linq_Tips { class Program { staticvoid Main(string[] args) { string aString = "ABCDE99F-J74-12-89A"; // Select only those characters that are numbers IEnumerable<char> stringQuery = from ch in aString where Char.IsDigit(ch) select ch; // Execute the query foreach (char c in stringQuery) Console.Write(c + " "); } } }

How to apply Left Outer join in Linq to Sql

In this post i am going to show you how to apply Left outer join in Linq to sql. Imagine that we want to apply left outer join in Products and Categories table in Northwind database Something like this:

SELECT [t1].[CategoryName] AS [CategoryId] FROM [dbo].[Products] AS [t0] LEFTOUTERJOIN [dbo].[Categories] AS [t1] ON [t0].[CategoryID] = ([t1].[Categor yID])
As an example i will take northwind Products and Categories table.Both these tables contain CategoryId as common field. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Linq_Tips { class Program { staticvoid Main(string[] args) { NorthwindDataContext dc = new NorthwindDataContext(); var filteredQuery = from p in dc.Products join c in dc.Categories on p.CategoryID equals c.CategoryID into joinData from data in joinData.DefaultIfEmpty() …

How to use flag enuemeration in c#/asp.net

Imagine we have a User class, and one of the user properties is Permission. The Permission enum can be defined as follows:enum Permission { None = 0, Read = 2, Write = 4, ReadWrite = 8, All = Int16.MaxValue } As you can see -these options could be combined in several ways like an user can have Read and Write permission or user can have only Read permission.Most of us will create seprate properties in user class but efficient way to resolve this problem is use flag enumeration.flag enumeration is an instance of the enumeration type to store any combination of the values that are defined in the enumerator list.You create a bit flags enum by applying the System.FlagsAttribute attribute and defining the values appropriately so that AND, OR, NOT and XOR bitwise operations can be performed on them.[Flag] enum Permission { None = 0, Read = 2, Write = 4, ReadWrite = 8, All = Int16.MaxValue }…

How to add tool tip for Dropdownlist Item in asp.net

In this post i am going to show you how to add tool tip for dropdownlist item in asp.net.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ToolTip_DropDownList.aspx.cs" Inherits="ToolTip_DropDownList" %><!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><asp:DropDownListID="ddlDemo"runat="server"><asp:ListItemText="Asp.net"Value="ASP.NET, the next version of ASP, is a programming framework used to create enterprise-class Web Applications"/><asp:ListItemText="C#"Value="C# (pronounced C-sharp) is an object-oriented programming language from Microsoft"/><…