Posts

Showing posts from 2011

Using SQL Profiler

Image
Goto Sql Server Management Studio. Tools - >Sql Server Profiler Click on "OK" and enjoy.

Difference between WCF and Web service

Difference between WCF and Web service Web service is a part of WCF. WCF offers much more flexibility and portability to develop a service when comparing to web service. Still we are having more advantages over Web service, following table provides detailed difference between them. Features Web Service WCF Hosting It can be hosted in IIS It can be hosted in IIS, windows activation service, Self-hosting, Windows service Programming [WebService] attribute has to be added to the class [ServiceContraact] attribute has to be added to the class Model [WebMethod] attribute represents the method exposed to client [OperationContract] attribute represents the method exposed to client Operation One-way, Request- Response are the different operations supported in web service One-Way, Request-Response, Duplex are different type of operations supported in WCF XML System.Xml.serialization name space is used for serialization System.Runtime.Serialization namespac...

Useful SQL shortcuts

Move content to right  CTRL +A & TAB Move content to left  CTRL +A & SHIFT + TAB Convert text to UPPER CASE in sql editor select the content and press CTRL+SHIFT+U Convert text to LOWER CASE in sql editor select the content and press  CTRL+SHIFT+L . . . . . . . . . . .

Creating Custom HTML Helpers

Creating custom html code blocks which can be re-used in the entire project. Step1: Add a folder "App_Code" in the root directory. Step2: Add a .cshtml file in the App_Code folder "CustomHelpers.cshml"    @helper   HomeHelper(){         <h1>Home Page Helper</h1>     } Step3: Call the newly added helper code in our existing .cshtml page @{     ViewBag.Title = "Home Page";     Layout = "~/Views/Shared/_Layout.cshtml"; } <h2> Home Page</h2> <div>     @CustomHelper.HomeHelper(); </div>

ASP.NET Routing

Introduction In an ASP.NET application that does not use routing, an incoming request for a URL typically maps to a physical file that handles the request, such as an .aspx file. For example, a request for  http://server/application/Products.aspx?id=4  maps to a file that is named Products.aspx that contains code and markup for rendering a response to the browser. The Web page uses the query string value of  id=4  to determine what type of content to display. In ASP.NET routing, you can define URL patterns that map to request-handler files, but that do not necessarily include the names of those files in the URL. In addition, you can include placeholders in a URL pattern so that variable data can be passed to the request handler without requiring a query string. URL Patterns in MVC Applications URL patterns for routes in MVC applications typically include  {controller}  and  {action}  placeholders. When a request is rec...

Features of the ASP.NET MVC Framework

The ASP.NET MVC framework provides the following features: Separation of application tasks (input logic, business logic, and UI logic), testability, and test-driven development (TDD). All core contracts in the MVC framework are interface-based and can be tested by using mock objects, which are simulated objects that imitate the behavior of actual objects in the application. You can unit-test the application without having to run the controllers in an ASP.NET process, which makes unit testing fast and flexible. You can use any unit-testing framework that is compatible with the .NET Framework. An extensible and pluggable framework. The components of the ASP.NET MVC framework are designed so that they can be easily replaced or customized. You can plug in your own view engine, URL routing policy, action-method parameter serialization, and other components. Extensive support for ASP.NET routing, which is a powerful URL-mapping component that lets you build applications that have compr...

How do I get the application path in an ASP.NET application

Retrieve the application path string appPath = HttpContext.Current.Request.ApplicationPath; Convert virtual application path to a physical path string physicalPath = HttpContext.Current.Request.MapPath(appPath);                                                    OR System.Web.HttpContext.Current.Server.MapPath("~/")

Difference between href="#" and href="javascript:void(0)"

href="" will link to the same page as the one you are currently on, effectively refreshing the page. href="#" will not refresh the page, but using the # will make the screen move to the top of the page (it is the browser effectively looking for an anchor with no name, ). javascript:void(0) will prevent anything happening on the link at all.

Returning Multiple Values in JAVASCRIPT

function myFunction() {   var result = testFunction();    alert(result[0]);//returns "1"    alert(result[1]);//returns "2"    alert(result[2]);//returns "3" } //This function returns multiple values(a,b,c) in the form of array. function  testFunction()  {   var a="1",b="2",c="3";     return [a,b,c] }

Calling Server Side Method Using jQuery/Ajax

With this post I would show how to call server side method from client side. Here we will use jQuery to utilize the Ajax Capabilities which will help us to get/post data to/from server Asynchronously. There are many methods available to perform an async callback to the server. Here I will show a simple example as in how to call a code behind Webmethod. For simplicity I would be calling the code behind method on a Button Click. Here is the code: Aspx markup : 1: <asp:Button ID= "Button1" runat= "server" Text= "Click" /> 2: <br /><br /> 3: <div id= "myDiv" ></div> jQuery : 1: <script src= "http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type= "text/javascript" ></script> 2: <script type = "text/javascript" > 3: $(document).ready(function () { 4: $( '#<%=Button1.ClientID %>' )...

"IN" operator in LINQ

SQL Query using IN operator SELECT Members . Name FROM Members WHERE Members . ID IN ("1,2,3" ) LINQ Query equivalent string [] str = { "1" , "2" }; var list = Members .Where(p=> str.Contains(p.ID)) .Select(X => X);

LINQ To SQL - CASE Statements

Image
Switch functionality can be accomplished by using " Tern ary Operator " The ternary operator takes the "if" statement and turns it into an expression.  Here's an example: The syntax is <condition>  ? <true value> : <false value> Now, let's add a basic case statement.  This will return the text "This is poisonous!" for plants with a 0 in the edible field, and "Okay to eat" otherwise.  By looking at the generated SQL using the debug visualizer, we can see that a CASE statement is in fact being generated. Now the question will come up, "what if I want to have more than just one WHEN and an ELSE".  In other words, how do I add more cases.  Here's the trick: By replacing the "if false" value of the ternary expression with another ternary expression we logically create the same effect as a SQL CASE statement.  Unlike the switch statement, this is an expression, and can be used on the ...

Types of LINQ syntax

There are 2 types of LINQ Syntax: 1.Fluent syntax 2.Query syntax string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" }; IEnumerable<string> query = names.Where (name => name.EndsWith ("y")); query.Dump ("In fluent syntax"); query = from n in names where n.EndsWith ("y") select n; query.Dump ("In query syntax");

Combining interpreted and local queries.

void Main() { // This uses a custom 'Pair' extension method, defined below. IEnumerable<string> q = Customers .Select (c => c.Name.ToUpper()) .Pair() // Local from this point on. .OrderBy (n => n); q.Dump(); } public static class MyExtensions { public static IEnumerable<string> Pair (this IEnumerable<string> source) { string firstHalf = null; foreach (string element in source) if (firstHalf == null) firstHalf = element; else { yield return firstHalf + ", " + element; firstHalf = null; } } }

SQL to LINQ converter

Try this tool :  http://www.linqpad.net/

Using LINQPad to test your LINQ to XML queries

Image
Using LINQPad to test your LINQ to XML queries Sunday, 6 February 2011 12:55 by  mha If you’re a LINQPad user you’re probably used to query your DBML (LINQ to SQL), but did you know that you can also use it for LINQ to Objects, LINQ to XML etc. A simple example of how to do a query against a XML file (make sure language is set to C# Statements): var xml = XElement.Load (@"c:\\inetpub\\GWportal\\src\\wwwBackend\\App_Data\\axCache\\     GWDK\productprice-mha.xml"); var query =    from e in xml.Descendants("unittransaction").Descendants()      where (e.Attribute("name").Value == "ItemRelation" && e.Value.Equals("10020"))    select e.Parent;     query.Dump(); Which gives this result: