Tuesday, April 30, 2013

Google page rank class C#

Add this Class in project

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

/// <summary>
/// Summary description for GooglePR
/// </summary>
public class GooglePR
{
    public GooglePR()
    {

    }
    private const UInt32 myConst = 0xE6359A60;
    private static void Hashing(ref UInt32 a, ref UInt32 b, ref UInt32 c)
    {
        a -= b; a -= c; a ^= c >> 13;
        b -= c; b -= a; b ^= a << 8;
        c -= a; c -= b; c ^= b >> 13;
        a -= b; a -= c; a ^= c >> 12;
        b -= c; b -= a; b ^= a << 16;
        c -= a; c -= b; c ^= b >> 5;
        a -= b; a -= c; a ^= c >> 3;
        b -= c; b -= a; b ^= a << 10;
        c -= a; c -= b; c ^= b >> 15;
    }
    public static string PerfectHash(string theURL)
    {
        string url = string.Format("info:{0}", theURL);
        int length = url.Length;
        UInt32 a, b;
        UInt32 c = myConst;
        int k = 0;
        int len = length;
        a = b = 0x9E3779B9;
        while (len >= 12)
        {
            a += (UInt32)(url[k + 0] + (url[k + 1] << 8) +
                 (url[k + 2] << 16) + (url[k + 3] << 24));
            b += (UInt32)(url[k + 4] + (url[k + 5] << 8) +
                 (url[k + 6] << 16) + (url[k + 7] << 24));
            c += (UInt32)(url[k + 8] + (url[k + 9] << 8) +
                 (url[k + 10] << 16) + (url[k + 11] << 24));
            Hashing(ref a, ref b, ref c);
            k += 12;
            len -= 12;
        }
        c += (UInt32)length;
        switch (len)
        {
            case 11:
                c += (UInt32)(url[k + 10] << 24);
                goto case 10;
            case 10:
                c += (UInt32)(url[k + 9] << 16);
                goto case 9;
            case 9:
                c += (UInt32)(url[k + 8] << 8);
                goto case 8;
            case 8:
                b += (UInt32)(url[k + 7] << 24);
                goto case 7;
            case 7:
                b += (UInt32)(url[k + 6] << 16);
                goto case 6;
            case 6:
                b += (UInt32)(url[k + 5] << 8);
                goto case 5;
            case 5:
                b += (UInt32)(url[k + 4]);
                goto case 4;
            case 4:
                a += (UInt32)(url[k + 3] << 24);
                goto case 3;
            case 3:
                a += (UInt32)(url[k + 2] << 16);
                goto case 2;
            case 2:
                a += (UInt32)(url[k + 1] << 8);
                goto case 1;
            case 1:
                a += (UInt32)(url[k + 0]);
                break;
            default:
                break;
        }
        Hashing(ref a, ref b, ref c);
        return string.Format("6{0}", c);
    }
    public int MyPageRank(string MyUrl)
    {
        string HashDomain = PerfectHash(MyUrl);
        string RequestedURL = string.Format("http://toolbarqueries.google.com/" +
               "tbr?client=navclient-auto&ch={0}&features=Rank&q=info:{1}",
               HashDomain, MyUrl);
        try
        {
            HttpWebRequest HttpRequest = (HttpWebRequest)WebRequest.Create(RequestedURL);
            string GetResponse = new StreamReader(
                   HttpRequest.GetResponse().GetResponseStream()).ReadToEnd();
            if (GetResponse.Length == 0)
                return 0;
            else
                return int.Parse(Regex.Match(GetResponse,
                       "Rank_1:[0-9]:([0-9]+)").Groups[1].Value);
        }
        catch (Exception)
        {
            return -1;
        }
    }
}



and on code behind page
 protected void Page_Load(object sender, EventArgs e)
    {
GooglePR Gpr = new GooglePR();
Response.write(Gpr.MyPageRank(val));
}

Count GetAlexa Rank

Count GetAlexa Rank

Storing  Alexa value in viewstate and using that viestate as per requirement.

private int GetAlexaRank(string domain)
    {
        var alexaRank = 0;
        try
        {
            var url = string.Format("http://data.alexa.com/data?cli=10&dat=snbamz&url={0}", domain);
            var doc = XDocument.Load(url);
            var rank = doc.Descendants("POPULARITY")
            .Select(node => node.Attribute("TEXT").Value)
            .FirstOrDefault();

            if (!int.TryParse(rank, out alexaRank))
                alexaRank = -1;

        }
        catch (Exception e)
        {
            return -1;
        }
        ViewState["alaska"] = Convert.ToString(alexaRank);
        return alexaRank;

    }

Count google indexed page C#

To perform this task we need htmlagility pack click here and download

Count Google indexed page : -

public void getindexedcount(string url)
    {

        HtmlWeb hw = new HtmlWeb();
        HtmlDocument doc = hw.Load("https://www.google.co.in/search?hl=en&noj=1&q=site:" + url + "");
        foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//div[@id='resultStats']"))
        {
            getindex.Text = "Indexes pages Count is :" + link.OuterHtml;
        }
    }

Monday, April 15, 2013

errors : The name ... does not exist in the current

Make sure inherits and class same name

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


public partial class TEST_office : System.Web.UI.Page
{
    
}

Hide Null value hyperlink Gridview

Visible ='<%# Eval("a") == System.DbNull.Value ? False : True %>'
 
 
Here On visible we check if the value is null then visible false otherwise it is true means visible.

Wednesday, April 10, 2013

Gallery Image Zoom

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Zoom Image on Mouse Hover using jQuery Image Zoom Plugin</title>
<link href="jQuery.fancybox.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="js/jquery.elevatezoom.min.js" type="text/javascript"></script>
<script src="js/jquery.fancybox.pack.js" type="text/javascript"></script>
<style type="text/css">
#gallery_01 img{border:2px solid white;width: 96px;}
.active img{border:2px solid #333 !important;}
</style>
</head>
<body>
<div>
<img id="img1" src="images/small/image1.png" data-zoom-image="images/large/image1.jpg" />
<div id="gallery_01">
<a  href="#" class="active" data-image="images/small/image1.png"
data-zoom-image="images/large/image1.jpg">
<img src="images/small/image1.png"  /></a>
<a  href="#" data-image="images/small/image2.png" data-zoom-image="images/large/image2.jpg">
<img src="images/small/image2.png" /></a>
<a href="#" data-image="images/small/image3.png" data-zoom-image="images/large/image3.jpg">
<img src="images/small/image3.png" /></a>
<a href="#" data-image="images/small/image4.png" data-zoom-image="images/large/image4.jpg">
<img src="images/small/image4.png" /></a>
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
$("#img1").elevateZoom({ gallery: 'gallery_01', cursor: 'pointer', galleryActiveClass: "active" });
$("#img1").bind("click", function(e) {
var ez = $('#img1').data('elevateZoom');
ez.closeAll();
$.fancybox(ez.getGalleryList());
return false;
});
});
</script>
</body>
</html>

Sql Injection

Sql Injection just like a solving the puzzle for this we need little bit understanding of sql.

Basically when we use Select Clause to retrieve record from database or while insert record sql injection applied.

Example For Login Query

Select * from Empdetail where  EmailID='perfect.chourasia@gmail.com' and pwd='1a2s3d'

Above query return the result where email id and password match by result set.

By Few User can access without any Emailid and Pwd
Use 'OR''=' ,'OR''='--, 'OR''=', 'OR'1'='1 in place of Perfect.chourasia@gmail.com.

Query will break -


Select * from Empdetail where  EmailID=''OR''='--' and pwd='1a2s3d'


Say you have a stored procedure that executes dynamic SQL from a user input. A very simple example:
CREATE PROCEDURE dbo.sp_testproc
    @query    varchar(1000)
AS

DECLARE @sql varchar(1000);
SET @sql='SELECT * FROM dbo.Employees WHERE '+@query;
EXEC(@sql);
Needless to say, it is really dangerous to allow people to insert ad-hoc SQL code. Here’s what could happen:
EXECUTE dbo.sp_testproc
    @query='currentlyEmployed=1; DELETE FROM dbo.Employees';
In the example above, the user can wipe the dbo.Employees table because he uses a semicolon to separate a statement into two. Actually, a line break would do just as well.

Replacing semicolons, etc

As a step in validating user-input, you can use the REPLACE() function to eliminate characters or strings in the user input variable.
SET @query=REPLACE(@query, ';', '');
SET @query=REPLACE(@query, CHAR(10), '');
SET @query=REPLACE(@query, CHAR(13), '');

Escaping apostrophes

Apostrophees can be used for a similar purpose when generating SQL injections. You can provide apostrophes in regular text strings in T-SQL, but they are escaped with another apostrophe.
SET @query=REPLACE(@query, '''', '''''');
This may look tricky at first, but remember that an escaped apostrophe is actually two apostrophes when in a string. So to set a variable to the value “Life’s good”, you’d use the following T-SQL
SET @status='Life''s good';


Due to or condition query check the condition ''='' it return true. So hacker succeeded in there aim.

Zoom Image

 Jquery Image Zoom On Mousehover

Here you need to place three dimension of single image
a. Thumbnail - 100 x 67 pixels
b. Small - 411 x 274 pixels
c. Large - 1280 x 854 pixels


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

<!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 inner Zoom Image on Mouse Hover</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="js/jquery.elevatezoom.min.js" type="text/javascript"></script>
</head>
<body>
    <div>
    <img id="img1" src="images/small/image1.png" data-zoom-image="images/large/image1.jpg"/>
   
     <img id="img2" src="images/small/image2.png" data-zoom-image="images/large/image2.jpg"/>
    </div>
     <script type="text/javascript">
         $(function() {
         $("#img1").elevateZoom({
             zoomType:"inner",
             cursor:"crosshair"
             });
            
         });
</script>
</body>
</html>










Download the Code





Tuesday, April 9, 2013

Resizable Gridview

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head id="Head1" runat="server">
    <title></title>
    <script src="Scripts/jquery.js" type="text/javascript"></script>
<script src="Scripts/colResizable-1.3.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.cookie.js" type="text/javascript"></script>
 <script type="text/javascript">
     $(document).ready(function () {
         if ($.cookie('colWidth') != null) {
             var columns = $.cookie('colWidth').split(',');
             var i = 0;
             $('.GridViewStyle th').each(function () {
                 $(this).width(columns[i++]);
             });
         }

         $(".GridViewStyle").colResizable({
             liveDrag: true,
             gripInnerHtml: "<div class='grip'></div>",
             draggingClass: "dragging",
             onResize: onSampleResized
         });

     });

     var onSampleResized = function (e) {
         var columns = $(e.currentTarget).find("th");
         var msg = "";
         columns.each(function () { msg += $(this).width() + ","; })
         $.cookie("colWidth", msg);
     };
</script>
</head>
<body>
    <form id="Form1" runat="server">
    <asp:Label ID="Label1" runat="server"></asp:Label>
    <br /> <br />
    <asp:GridView ID="GridView1" runat="server" CssClass="GridViewStyle">
    </asp:GridView>
    <br /><br />
     <asp:Button ID="Button1" runat="server" Text="Test Postback" />

     </form>
</body>
</html>

Code behind page

 protected void Page_Load(object sender, System.EventArgs e)
    {
        Dictionary<string, int> col = new Dictionary<string, int>();
        col.Add("key 1", 1);
        col.Add("key 2", 2);
        col.Add("key 3", 3);
        col.Add("key 4", 4);
        col.Add("key 5", 5);
        GridView1.DataSource = col;
        GridView1.DataBind();
    }
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        Label1.Text = string.Format("Current Time is {0}", DateTime.Now.ToString());
    }

Download the Source Code