Sunday, January 30, 2011

Insert, Update, Delete Operation in LINQ to SQL????

Here I give you simple example to understand basic database functionality using LINQ

Table structure :

Id(Primary key, Auto Increment)    Int
Name                                             nvarchar(50)
Age                                                nvarchar(50)


Insert operation:

protected void btn_Insert_Click(object sender, EventArgs e)
    {
        DataClassesDataContext db_Context = new DataClassesDataContext();

        Customer cus = new Customer()
        {
            Name = TextBox1.Text,
            Age = TextBox2.Text
        };

        db_Context.Customers.InsertOnSubmit(cus);
        db_Context.SubmitChanges();
    }


Update operation:

protected void btn_Update_Click(object sender, EventArgs e)
    {
        DataClassesDataContext db_context = new DataClassesDataContext();

        Customer cus = db_context.Customers.Single(c => c.ID == 1);
        cus.Name = TextBox1.Text;
        cus.Age = TextBox2.Text;

        db_context.SubmitChanges();
    }


Delete operation:


 protected void btn_Delete_Click(object sender, EventArgs e)
    {
        DataClassesDataContext db_context = new DataClassesDataContext();       
        var cusdata = (from cus in db_context.Customers where cus.Age == "30" select cus).First();
        db_context.Customers.DeleteOnSubmit(cusdata);
        db_context.SubmitChanges();
    }

Tuesday, January 25, 2011

Convert Image to base64 string and base64 to image..

 private void btnBase64_Click(object sender, EventArgs e)
        {
            // Open the image file for reading.
            FileStream fs = File.OpenRead("test.gif");

            // Get the bytes for the image
            byte[] data = new byte[fs.Length];
            fs.Read(data, 0, data.Length);

            // Convert the bytes into a base64 string.
            string base64 = Convert.ToBase64String(data);

            // Display the string into the textbox. 
            textBox1.Text = base64;

            // Use the string in the textbox to convert back into bytes.
            byte[] byteImage = Convert.FromBase64String(base64);

            // Create a memorystream using the bytes
            MemoryStream m = new MemoryStream(byteImage);

            //Create a bitmap image using the memorystream.
            Bitmap img = new Bitmap(m);

            // Change the forms background image to the bitmap image.
            this.BackgroundImage = img;
        }

Friday, January 7, 2011

Getting Alexa Rank Using JavaScript..

 Here I am making simple java script function to find out alexa rank : 


function getAlexaRank(URL)
{     
      var currentURLArray = URL.split('/');
       var alexaURL = "http://data.alexa.com/data?cli=10&dat=s&url=" + currentURLArray[2];
       var xmlhttp = new XMLHttpRequest();
   
       xmlhttp.open("GET", alexaURL, true); 
  
       xmlhttp.onreadystatechange = function()
       {
          if (xmlhttp.readyState == 4)
          {           
              var response = xmlhttp.responseText;           
              //alert("Alexa Rank " + response); 
              var firstindex = response.indexOf("REACH RANK");
              var lastindex = response.indexOf("RANK DELTA");
              //alert("First Index=" + firstindex + "Last Index" + lastindex);
              response = response.substring(firstindex,lastindex-4);             
              response = response.split('"');
              if(firstindex == -1 && lastindex == -1)
                alert("Alexa Rank not found");
              else
                 alert("Alexa Rank=" + response[1]);
           
          }
       }   
       xmlhttp.send(null);     
}

Hope this will be helpful for you.

Tuesday, January 4, 2011

Page Rank Using Javascript

function getPageRank (URL)
{
           
            currentURLArray = URL.split('/');   
           
            var pageRankURL = currentURLArray[0] + "//" + currentURLArray[2] + "/";           
            var hashCode = CheckHash(HashURL(pageRankURL));   
            var xmlhttp = new XMLHttpRequest();               
            var baseURL = "http://toolbarqueries.google.com/search?client=navclient-auto&ie=UTF-8&oe=UTF-8&features=Rank&ch=" + hashCode + "&q=info:" + pageRankURL + "&num=100&filter=0";   
           
           xmlhttp.open("GET", baseURL, true);     
           xmlhttp.onreadystatechange = function()
           {
              if (xmlhttp.readyState == 4)
              {                      
                  var response = xmlhttp.responseText;   
                  alert("Page Rank=" + response);               
              }
           }
       
         xmlhttp.send(null); 
       
}

function StrToNum(str, check, magic)
{
  int32Unit = 4294967296; // 2^32

  for (i = 0; i < str.length; i++)
  {
    check *= magic;
    if (check >= int32Unit)
    {
      check = (check - int32Unit * parseInt (check / int32Unit));
      check = (check < -2147483648) ? (check + int32Unit) : check;
    }
    check += str.charCodeAt(i);
  }

  return check;
}


function bitwise_or (op1, op2)
{
  op1 = op1.toString(16);
  op2 = op2.toString(16);

  res = '';
  for (i1 = op1.length - 1, i2 = op2.length-1; i1 >= 0 || i2 >= 0; i1--,i2--) {
    var o1 = 0, o2 = 0;
    if (i1 >= 0) o1 = op1.charAt(i1);
    if (i2 >= 0) o2 = op2.charAt(i2);
    res = (parseInt(o1, 16) | parseInt (o2, 16)).toString(16) + res;
  }

  return parseInt (res, 16);
}


function HashURL(str)
{
  check1 = StrToNum (str, 0x1505, 0x21);
  check2 = StrToNum (str, 0, 0x1003F);

  check1 >>= 2;
  check1 = ((check1 >> 4) & 0x3FFFFC0 ) | (check1 & 0x3F);
  check1 = ((check1 >> 4) & 0x3FFC00 ) | (check1 & 0x3FF);
  check1 = ((check1 >> 4) & 0x3C000 ) | (check1 & 0x3FFF);


  t1 = bitwise_or (bitwise_or((check1 & 0x3C0) << 4, (check1 & 0x3C)) << 2, check2 & 0xF0F);


  t2_1 = parseInt ((((((check1 & 0xFFFFC000) << 4) | (check1 & 0x3C00)) ) * 4).toString (16) + '00', 16);
  t2_2 = (check2 & 0xF0F0000);
  t2   = bitwise_or (t2_1, t2_2);
 
  return bitwise_or (t1, t2);
}


function CheckHash (hashnum)
{
  checkByte = 0;
  flag      = 0;

  hashstr = ""+hashnum; // tostring

  for (i = hashstr.length - 1; i >= 0; i--)
  {
    re = parseInt (hashstr.charAt (i));
    if (1 === (flag % 2))
    {
      re += re;
      re = parseInt(re / 10) + (re % 10);
    }
    checkByte += re;
    flag ++;
  }

  checkByte %= 10;
  if (0 != checkByte)
  {
    checkByte = 10 - checkByte;
    if (1 === (flag % 2) )
    {
      if (1 === (checkByte % 2))
      {
        checkByte += 9;
      }
      checkByte >>= 1;
    }
  }

  return '7' + checkByte + hashstr;
}


Note - Getting page rank using Google is might be illegal as per Google policy. Go with privacy policy of Google before using it.