Friday, June 15, 2012

Watin Tool


In this blog iam describe the implementation and usage of watin
Watin is the one of the most  powerful  visual studio testing tool .watin tool develop in C# and the main goal  automating   the browser and test the project. watin can get and set values from the elements in a form, and you can fire events of any of the elements 

Download the watin click here
Following is the Hello world example of web test automation: searching Google.

[Test]
public void SearchForWatiNOnGoogle()
{
  using (var browser = new IE("http://www.google.com"))
  {
    browser.TextField(Find.ByName("q")).TypeText("WatiN");
    browser.Button(Find.ByName("btnG")).Click();
  
    Assert.IsTrue(browser.ContainsText("WatiN"));
  }
}

HTMLAgilityPack


In this blog iam describe the implementation and usage of HTMLAgilityPack
HtmlAgilityPack is one of the great open sources projects I ever worked with. It is a HTML parser for .NET applications, works with great performance, supports malformed HTML.
HTMLAgilityPack is used to retrieve the value from browser. The main goal of the HTMLAgilityPack
Website crawling or screen scraping

Download and build the HTMLAgilityPack solution.

The sample code are here


Take the html hidden value

var webGet = new HtmlWeb();
            var document = webGet.Load(url);
            var value = document.DocumentNode.SelectSingleNode("//input[@type='hidden' and @name='mob']")
                .Attributes["value"].Value;


Take the html td value



var webGet = new HtmlWeb();
            var document = webGet.Load(url);

            foreach (HtmlNode li in document.DocumentNode.SelectNodes("//td[@class='textnopad']"))
            {
                if(li.InnerText.Contains("91"))
                {
                    string mob = li.InnerText;
                }
            }
 Take the html anchor  tag value

var webGet = new HtmlWeb();
            var document = webGet.Load(url);
            var linksOnPage = from lnks in document.DocumentNode.Descendants()
                              where lnks.Name == "a" &&
                                    lnks.Attributes["href"] != null &&
                                     lnks.Attributes["href"].Value.Contains(id)
                                   
                              select new
                              {
                                  Url = lnks.Attributes["href"].Value,
                                  Text = lnks.InnerText

                              }.Url;

  Take the html anchor  tag value

var webGet = new HtmlWeb();
            var document = webGet.Load(url);
              foreach (HtmlNode li in document.DocumentNode.SelectNodes("//div[@id='cssBox']"))
            {
                string mob = li.InnerText;
            }



Friday, June 1, 2012

Crop Image asp.net


Just go to this link: http://webcropimage.codeplex.com/
or here is sample code:
or here is sample code:

if (addimage.HasFile)
        {

            filepath = "~/userimages/" + Profile.UserName + "/" + "1" + Convert.ToString(DateTime.Now.GetHashCode()) + addimage.FileName;
            addimage.SaveAs(MapPath(filepath));
            System.Drawing.Image userimage = System.Drawing.Image.FromFile(MapPath(filepath));
            string thumbfilepath = "~/userimages/" + Profile.UserName + "/" + "1small" + Convert.ToString(DateTime.Now.GetHashCode()) + addimage.FileName;
            userimage = crop(userimage,180,180);
            userimage.Save(thumbfilepath, userimage.RawFormat);

            imageclass.InsertImages(ticketid, Profile.userid, filepath, newclass.capcheck(title.Text));

        }

 Bitmap crop(System.Drawing.Image userimage, int Width, int Height)
    {
        Bitmap newimage = null;
      
            newimage = new Bitmap(userimage, Width, Height);

       return newimage;
    }

Better way to store images in DB or as a file ina folder?


Storing Image in a folder and using URL or relative path in the database is the way I would recommend. They are having many advantages and here are some of them.


  • Normally sql server space is more expensive than ordinary disk space in hosting environment.
  • You can name images in a Search Engine optimized way
  • Essay to manage, backup and restore images and database.
  • More suitable for static pages, (if you optimize some pages for performance).

Return multiple values from a single method in C#?


If you mean returning multiple values, you can either return a class/struct containing the values you want to return, or use the "out" keyword on your parameters, like so:
public void Foo(int input, out int output1, out string output2, out string errors) {
    // set out parameters inside function
}