Wednesday 16 April 2014

Some Linq queries for beginners

Following are some of the basic Linq command/samples for those who just want to start learning Linq. So this will help a lot to all the beginners to easily understand Linq queries.

// Basic Query
// To achieve  Select * From ProductMst  you need to write following linq Query
  var Result1 = from p in db.ProductMst
                 select p;
// To achieve  Select ProductID, ProductName, Price From ProductMst you need to write following linq Query
    var Result2 = from p in db.ProductMst
                 select new {
                     p.ProductID,
                     p.ProductName,
                     p.Price
                 };

// Where Clause Query
// To achieve  Select * From ProductMst Where ProductID = 1 you need to write following linq Query
    var Result3 = from p in db.ProductMst
                 where p.ProductID == 1
                 select p;

// To achieve Select * From ProductMst Where SupplierId =2 and Price > 10 you need to write following linq Query
    var Result4 = from p in db.ProductMst
                 where p.SupplierID == 2 && p.Price > 10
                 select p;


// To achieve Select * From ProductMst Where SupplierId =2 Or SupplierId=5 you need to write following linq Query
    var Result5 = from p in db.ProductMst
                 where p.SupplierID == 2 || p.SupplierID == 5
                 select p;

// Order By Query
//To achieve   Select * From ProductMst Order By ProductId you need to write following linq Query
    var Result6 = from p in db.ProductMst
                 orderby p.ProductID
                 select p;

// To achieve  Select * From ProductMst Order By ProductId Desc you need to write following linq Query
    var Result7 = from p in db.ProductMst
                 orderby p.ProductID descending
                 select p;

// To achieve  Select * From ProductMst Order By CategoryId, Price Desc you need to write following linq Query
    var Result8 = from p in db.ProductMst
                 orderby p.CategoryID, p.Price descending
                 select p;

// Top Query
//To achieve Select Top 5 * From ProductMst you need to write following linq Query
    var Result9 = (from p in db.ProductMst
                 select p).Take(5);

//To achieve Select Top 1 * From ProductMst you need to write following linq Query
    var Result10 = (from p in db.ProductMst
                   select p).Take(1);
     or

    var Result11 = (from p in db.ProductMst
                   select p).First();

// Distinct Query
//To achieve Select Distinct CategoryId From ProductMst you need to write following linq Query

    var Result13 = (from p in db.ProductMst
                   select p.CategoryID).Distinct();

// Group By Query
//To achieve Select CategoryId, Count(CategoryID) As FieldName From ProductMst Group By CategoryId you need to write following linq Query
    var Result14 = from p in db.ProductMst
                  group p by p.CategoryID into g
                  select new {
                      CategoryId = g.Key,
                      FieldName = g.Count()
                  };

//To achieve Select CategoryId, Avg(UnitPrice) As NewField From ProductMst Group By CategoryId you need to write following linq Query
    var Result15 = from p in db.ProductMst
                  group p by p.CategoryID into g
                  select new {
                      CategoryId = g.Key,
                      FieldName = g.Average(S => S.Price)
                  };

// Union Query
//To achieve Select * From ProductMst Where CategoryId =1 union Select *  From ProductMst Where 
CategoryId = 2 you need to write following linq Query
    var Result17 = (from p in db.ProductMst
                   where p.CategoryID == 1
                   select p).Union(
                       from m in db.ProductMst
                       where m.CategoryID == 2
                       select m
                   );

// Inner Join
// To achieve select p.ProductName, c.categoryName from ProductMst p inner join CategoryMst c 
on p.CategoryID=c.Category  you need to write following linq Query

   var Result18=from n in db.ProductMst
                        join c in db.CategoryMst
                        on n.CategoryID equals c.CategoryID
                        select new
                        {
                           n.ProductName, c.categoryName 
                        };

Thursday 3 April 2014

Detect / Find Client IP Address using JavaScript & jQuery


In this short article I will explain how to detect / find the IP Address of the client / user machine using JavaScript & jQuery.


I have added an HTML <span> tag which will display the IP Address of the client machine. To get the IP Address I am making a JSON call to the Free Web Servicehttp://jsonip.appspot.com/ and I am passing the name of the callback function which will be called on completion of the request.
When the request is completed the IP address present in the response object is displayed in the HTML <span> tag


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
    window.onload = function () {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://www.telize.com/jsonip?callback=DisplayIP";
        document.getElementsByTagName("head")[0].appendChild(script);
    };
    function DisplayIP(response) {
        document.getElementById("ipaddress").innerHTML = "Your IP Address is " + response.ip;
    }
</script>
</head>
<body>
    <form>
        <span id = "ipaddress"></span>
    </form>
</body>
</html>


Tuesday 1 April 2014

jQuery Crop Image in Asp.net using Jcrop jQuery Plugin and Upload to Folder

Here I will explain how to crop image in asp.net using jcrop jQuery plugin or jQuery image crop and upload in asp.net using jQuery jcrop plugin. Jcrop is a powerful image cropping engine for jQuery by using this jcrop plugin we can add cropping functionality easily to your web application.

Designer Source Code:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>jQuery Crop Image using crop plugin</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
    <script src="jquery.Jcrop.js" type="text/javascript"></script>
    <link href="jquery.Jcrop.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
        $(function() {
        $('#imgcrop').Jcrop({
        onSelect: getcroparea
            });
        })

        function getcroparea(c) {
            $('#hdnx').val(c.x);
            $('#hdny').val(c.y);
            $('#hdnw').val(c.w);
            $('#hdnh').val(c.h);
        };
    </script>
   
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <img src="images/pool.jpg" id="imgcrop" alt="sample image"/>
        <input type="hidden" id="hdnx" runat="server" />
        <input type="hidden" id="hdny" runat="server"/>
        <input type="hidden" id="hdnw" runat="server"/>
        <input type="hidden" id="hdnh" runat="server" />
        <asp:Button ID="btncrop" runat="server" OnClick="btncrop_Click" Text="Crop Images" />
        <img id="imgcropped" runat="server" visible="false" />
    </div>
    </form>
</body>
</html>


 Code Behind :

using System;
using System.Drawing;
using System.IO;
using Image = System.Drawing.Image;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btncrop_Click(object sender, EventArgs e)
    {
        try
        {
                string fname = "pool.jpg";
                string fpath = Path.Combine(Server.MapPath("~/images"), fname);
                Image oimg = Image.FromFile(fpath);
                Rectangle cropcords = new Rectangle(
                Convert.ToInt32(hdnx.Value),
                Convert.ToInt32(hdny.Value),
                Convert.ToInt32(hdnw.Value),
                Convert.ToInt32(hdnh.Value));
                string cfname, cfpath;
                Bitmap bitMap = new Bitmap(cropcords.Width, cropcords.Height, oimg.PixelFormat);
                Graphics grph = Graphics.FromImage(bitMap);
                grph.DrawImage(oimg, new Rectangle(0, 0, bitMap.Width, bitMap.Height), cropcords, GraphicsUnit.Pixel);
                cfname = "crop_" + fname;
                cfpath = Path.Combine(Server.MapPath("~/cropimages"), cfname);
                bitMap.Save(cfpath);
                imgcropped.Visible = true;
                imgcropped.Src = "~/cropimages/" + cfname;
            }
            catch (Exception ex)
            {
                throw ex;
            }
    }
}


Download Here