Thursday, December 4, 2008

Yield Keyword in C#

One interesting new feature of the C# 2।0 is the "yield" keyword. Basically it is used to iterate through objects returned by a method. It creates a state engine in IL so you can create methods that retain their state and dont have to go through the pain of maintaining state in your code.



public static IEnumerable GetInt()
{
for (int i = 0; i <>
yield return i;
}


Here's the implementation.


class प्रोग्राम
{ static void Main(string[] args)
{
foreach (int i in GetInt())
Console।WriteLine("Got " + i।ToString());
}


public static IEnumerable GetInt()
{
for (int i = 0; i <>
yield return i;
}
}

Thanks,
Nitin Sharma

Tuesday, December 2, 2008

Consuming Web Service by AJAX SlideShow Extender Control

Hi All,
     Here is a way to Consume Web Service in  AJAX SlideShowExtender Control using Context Key..

in .aspx page::

 if (Request.QueryString["Id"] != null)
        {
            uint Id = Convert.ToUInt16(Request.QueryString["Id"].ToString());
                       
          SlideShowExtender1.ContextKey = Id.ToString(); //Passing the ID attribute Dynamically
            
        }
 ------------------------------------------------------------------------------------
   div align="center"
    asp:Label runat="Server" ID="imageTitle" CssClass="slideTitle" 

    asp:Image ID="ImagePlayer" runat="server" Height="300" Style="border: 1px 
solid black; width: auto" 
    asp:Label runat="server" ID="imageDescription" CssClass="slideDescription"
    asp:Label
    
    asp:Button runat="Server" ID="btnBack" Text="Prev" Font-Size="Larger" 
    asp:Button runat="Server" ID="btnPlay" Text="Play" Font-Size="Larger" 
    asp:Button runat="Server" ID="btnNext" Text="Next" Font-Size="Larger" 
    cc1:SlideShowExtender ID="SlideShowExtender1" runat="server" TargetControlID="ImagePlayer"
        AutoPlay="True" Loop="True" NextButtonID="btnNext" PlayButtonID="btnPlay" PlayButtonText="Play"
        PreviousButtonID="btnBack" SlideShowServiceMethod="GetSlides" SlideShowServicePath="~/Test.asmx"
        StopButtonText="Pause" ImageDescriptionLabelID="imageDescription" ImageTitleLabelID="ImgTitle"
    cc1:SlideShowExtender>
    div

-------------------------------------------------------------------------------------

In .asmx(web service).cs page::
 [WebMethod]
    public AjaxControlToolkit.Slide[] GetSlides(string contextKey)
    {
        System.Web.UI.Page ObjUIPage = new System.Web.UI.Page();
        int i = 0;
        int j = 0;

        //'The below line specifies the Directory to be used.Here the images are in images folder of my application
        System.IO.DirectoryInfo DirInfo = new System.IO.DirectoryInfo(ObjUIPage.Server.MapPath("") + "\\PropertyUnitImages\\" + contextKey);
        System.IO.DirectoryInfo[] subDirs = DirInfo.GetDirectories();

        //'The below line gets the list of files in the directory specified above
        System.IO.FileInfo[] Files = DirInfo.GetFiles();

        //The below line gives the file count, which is useful to specify the size of array
        j = Files.Length;

        Slide[] mySlide = new AjaxControlToolkit.Slide[j + 1];
        //System.IO.FileInfo di = default(System.IO.FileInfo);

        //' This loop continues upto the last file in the directory
        foreach (var d in Files)
        {
            mySlide[i] = new AjaxControlToolkit.Slide(d.FullName, d.Name, d.Name.Substring(0, d.Name.IndexOf(".")));
            i = i + 1;
        }
        return mySlide;
        //' this line sends the dynamically added details as AjaxControlToolkit.Slide
    }
      





A word onContext Key  property of SlideShowExtender:



The parameter is contextKey of type String. You can use contextKey in anyway you want, it is meant to pass information from the calling AJAX control to the web service. In this application, the parameter will be used to supply the directory for the slide show to be fed from. The return parameter is an array of Slides. The AJAX SlideShow control looks for this signature and the signature is case sensitive and exact. Don't try to change any of this syntax or your service not be called.

Thanks,
Nitin Sharma

Friday, November 14, 2008

Some LINQ Queries

TestDataContext TDC = new TestDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        var query = from table in TDC.PTables
                    where table.salary >= 10000
                    orderby table.Age descending
                    select new
                    {
                        Name = table.Name.ToUpper(),
                        Age = table.Age,
                       Country = table.Country

                    };

        gv1.DataSource = query;
        gv1.DataBind();
      



      //Showing a List of Process by LINQ
      var process = from p in System.Diagnostics.Process.GetProcesses()
                      orderby p.ProcessName
                    select new
                     {
                        p.ProcessName,
                          p.Id,
                         p.Threads,
                          p.HandleCount,
                          p.WorkingSet64
                          

                     };
      foreach (var test in process)
       {
           Response.Write(test+"
");
       }



      //Showing a List of Files by LINQ
        System.IO.DirectoryInfo DI  = new System.IO.DirectoryInfo("D:\\USBBackUP\\Cards\\Cards");

       var directoryList =
                from f in DI.GetFiles("*.*",
                System.IO.SearchOption.AllDirectories)
              select f;

      foreach( var row in directoryList)
       {
            Response.Write(row +"
");
       }
    }
   
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        
        PTable ptable = new PTable();
        ptable.Age = "35";
        ptable.Name = "Nitin Sharma";
        ptable.Country = "INDIA";

        TDC.PTables.InsertOnSubmit(ptable);
        TDC.SubmitChanges();
        gv1.DataBind();

        
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
       
        try
        {
            PTable todelete = TDC.PTables.Single(p => p.Name.Contains("Nitin"));
            TDC.PTables.DeleteOnSubmit(todelete);
            TDC.SubmitChanges();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        gv1.DataBind();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        var update = (from table in TDC.PTables
                     where table.CountryID == 8 
                     select table).Single();
        update.Name = txtName.Text;
        TDC.SubmitChanges();
        gv1.DataBind();
        txtName.Text = "";
       
       
        
    }

      

Thanks,
Nitin Sharma

Wednesday, November 5, 2008

Joins in LINQ


Using a Simple Join in LINQ:

 RekabuDataContext RKdb = new RekabuDataContext();

protected void Page_Load(object sender, EventArgs e)
    {
     
        BindGridView();
       
    }

public void BindGridView()
    {
        var q = from records in RKdb.Tests
                from recordsnew in RKdb.Images
                where records.id == recordsnew.id
                select recordsnew;
        gvRekabu.DataSource = q;
        gvRekabu.DataBind();
    }


Assigning Custom Columns header in LINQ:

public void Bind()
    {

        var q = from fields in RKdb.Tests
                where (fields.salary >= 20000)

                select new
                {
                    CandidateName = fields.Name,
                    CandidateLocation = fields.Loaction,
                    CandidateSalary = fields.salary,

                }
                ;

        gvRekabu.DataSource = q;
        gvRekabu.DataBind();
    
    }


Thanks,
Nitin Sharma

Tuesday, November 4, 2008

Tulsi Vivah on November 10,2008

Tulsi Vivah

The tulsi plant is held sacred by the Hindus as it is regarded as an incarnation of Mahalaxmi who was born as Vrinda. The festival of Tulsi Vivah is celebrated in each and every household of Goa. The one special feature of the festival is that of preparing various delicious sweet dishes at home. The women folk engage themselves in preparations well in advance. A typical Goan ojhe (load of sweets) is sent to the daughter from her parental home along with jodi (cotton threads used to light lamp while performing aarti). 

Tulsi was married to demon king Jalandhar. But she prayed to Lord Vishnu that her demon husband would be protected, with the result that no god was able to harm him. However on the request of the other gods, Lord Vishnu took the form of Jalandhar and stayed with the unsuspecting Tulsi. When the truth emerged after Jalandhar's death, Vrinda crused Vishnu and turned him to stone (Shaligram) and collapsed. From her body emerged the tulsi plant. That is why Vishnu pooja is considered incomplete without tulsi leaves. Tulsi vivah is celebrated on the next day of Kartiki Akadashi. On this day tulsi is married to Shaligram. 

On this day tulsivrindavan is coloured and decorated as a bride. Sugarcane and branches of tamarind and amla trees are planted along with the tulsi plant. Only vegetarian food is cooked on this day. At midday, a full meal consisting of rice, moongachi gathi, puri , sweet potato kheer , red pumpkin vegetable cooked with pieces of sugarcane, amla and tamarind is offered to Tulsi Vrindavan. Tulsi vivah ceremony takes place in the late evening. Various poha dishes are offered to Lord Vishnu. Then prasad is distributed among family members and friends.


Thanks,
Nitin Sharma