April, 2009

  • Wriju's BLOG

    Learn IIS 7

    • 1 Comments

    Learning IIS7 is so organized. Thanks to the team http://learn.iis.net/ 

    Namoskar!!!

  • Wriju's BLOG

    Windows 7 RC will be available from May 5

    • 1 Comments

    You will be able to download from http://www.microsoft.com/windows/windows-7/default.aspx

    Namoskar!!!

  • Wriju's BLOG

    ADO.NET Entity Framework : Editing a detached Object

    • 3 Comments

    In Layered scenario, you might need to pass an object to another method while updating. Carrying open Context is not good however you may try the below approach by using “CreateEntityKey”. But this will not work if you have configured your edmx with Stored Procedure, reason behind that is

    static void Main(string[] args)

    {

        //Create Entity Key Object

        EntityKey key;

        object originalItem;

     

    //This object can come through method parameter

        Emp updatedItem = new Emp() { Id = 190 };

        updatedItem.FirstName = "Changed Last Name";

     

        using (TestDBEntities ctx = new TestDBEntities())

        {

            key = ctx.CreateEntityKey("Emp", updatedItem);

     

            if(ctx.TryGetObjectByKey(key, out originalItem))

            {

                ctx.ApplyPropertyChanges(key.EntitySetName, updatedItem);

            }

     

            ctx.SaveChanges();

        }                               

    }

    Namoskar!!!

  • Wriju's BLOG

    ASP.NET 3.5 ListView and DataPager : Making RSS Reader

    • 1 Comments

    ASP.NET 3.5 comes with bundle of new data bound controls. Among them ListView and DataPager are the ones I am going talk here. To me ListView is the enhanced from of DataRepeater. It has got everything you might need to create an app with your HTML/CSS skill set. Today you often visit blogs and you subscribe to RSS feed for your favourite ones. Here we are going to create the sample on “How to create RSS Reader”.

    Ø  Create a simple ASP.NET Project/Website.

    Ø  Add “XmlDataSource” to your page.

    Ø  Add the below configuration to your “XmlDataSource” for this blog,

    <asp:XmlDataSource

        ID="xmlWrijuBlog"

        DataFile="http://blogs.msdn.com/wriju/rss.xml"

        XPath="rss/channel/item"

        runat="server">

    </asp:XmlDataSource>

    Note that the XPath is unique for any RSS feed and that is the standard.

    Ø  Now you need to add the ListView Control with below configuration

    <asp:ListView

        ID="listWrijuBlog"

        DataSourceID="xmlWrijuBlog"

        ItemPlaceholderID="itemPlaceholder"   

        runat="server">

    This “ItemPlaceholderID” is the key here. You must need to have “asp:PlaceHolder” control under “LayoutTemplate” to be able to bind the data properly.

     

    Ø  Now let us have a look how the “LayoutTemplate” looks like

     

    <LayoutTemplate>

        <br /><br /><h1> Wriju's Recent Blog</h1><br /><br />

        <asp:PlaceHolder ID="itemPlaceholder"

            runat="server"></asp:PlaceHolder>

    </LayoutTemplate>

     

    Here we have exact same ID for PlaceHolder

     

    Ø  Now how the individual records would look like, it is by ItemTemplate

     

    <ItemTemplate>

        <h2><asp:HyperLink ID="hpLink"

                runat="server" Target="_blank" 

                NavigateUrl='<%# XPath("link") %>'

            ><%# XPath("title") %></asp:HyperLink>

        </h2>

        <p><em>Published <%# XPath("pubDate") %></em></p>

        <p><%# XPath("description") %></p>

    </ItemTemplate>

     

    Ø  Last one is optional if you need i.e.,  ItemSeparatorTemplate

     

    <ItemSeparatorTemplate>

        <hr />

    </ItemSeparatorTemplate>

     

    Ø  And now if you need the paging just go ahead and add DataPager control to it.

    <asp:DataPager ID="DataPager1"

        PagedControlID="listWrijuBlog"

        PageSize="2"           

        runat="server">

       

        <Fields>

            <asp:NextPreviousPagerField ButtonType="Button"

                ShowFirstPageButton="true" ShowLastPageButton="true"

                ShowNextPageButton="true" ShowPreviousPageButton="true" />                

             <asp:NumericPagerField     />                   

        </Fields>

    </asp:DataPager>

    Only thing you need to have here for a DataPager to say, “this is your paged control” though the attribute/property “listWrijuBlog”. This is nothing but your ListView control’s ID.

    The source file is attached here.

    For more visit http://asp.net

    Namoskar!!!

  • Wriju's BLOG

    Improving LINQ to SQL Application Performance

    • 1 Comments

    Must read http://www.sidarok.com/web/blog/content/2008/05/02/10-tips-to-improve-your-linq-to-sql-application-performance.html  

     

    Namoskar!!!

  • Wriju's BLOG

    Data Services (aka Astoria) : CRUD (Select / Insert / Update / Delete )

    • 2 Comments

    Creating Astoria is very easy and many of you might have tried that out. What important for us is to work with that data. If you are developing Visual Studio Solution then things are quite obvious as mentioned below,

    Either you will have Astoria or you will create that service. I am not going in details how you can create that. Assume that you have a service running anywhere.

    You create your client apps (Windows / Web) and the reference an assembly “System.Data.Services.Client”.

    After that you need to create “Service Reference” like WCF / Web Service from your visual studio project. Add the required namespace on top of your code behind file where you are writing your code.

    C : Create / Inserting Data

    static void Main(string[] args)

    {

        //Initialize the consumed Astoria Service

        DataServiceContext ctx = new DataServiceContext(new Uri(@"http://localhost:1116/TestDataService.svc/"));

     

        //This option is very important for Inserting

        ctx.MergeOption = MergeOption.AppendOnly;

     

        //Create a new object to insert

        Emp emp = new Emp() { Name = "Astoria Employee" };

     

        //Add to the collection

        ctx.AddObject("Emp", emp);

     

        //Commit the changes to Database

        ctx.SaveChanges();

    }

    U : Updating Data

    static void Main(string[] args)

    {

        //Initialize the consumed Astoria Service

        TestDataEntities ctx = new TestDataEntities(new Uri(@"http://localhost:1116/TestDataService.svc/"));

           

        //Create a new object to insert

        Emp emp = (from e in ctx.Emp where e.Id == 81 select e).First();

     

        //Change the Employee Name

        emp.Name = DateTime.Now.ToString();

     

        //Update the collection

        ctx.UpdateObject(emp);

     

        //Commit the changes to Database

        ctx.SaveChanges();

    }

     

    D : Deleting Data

    static void Main(string[] args)

    {

        //Initialize the consumed Astoria Service

        TestDataEntities ctx = new TestDataEntities(new Uri(@"http://localhost:1116/TestDataService.svc/"));

     

       

        //Create a new object to insert

        Emp emp = (from e in ctx.Emp where e.Id == 80 select e).First();

     

        //Delete the entity

        ctx.DeleteObject(emp);

     

        //Commit the changes to Database

        ctx.SaveChanges();

    }

    R : Read / Querying Astoria Service

    static void Main(string[] args)

    {

        //Initialize the consumed Astoria Service

        TestDataEntities ctx = new TestDataEntities(new Uri(@"http://localhost:1116/TestDataService.svc/"));

           

        //Create a new object to insert

        IEnumerable<Emp> emps = from e in ctx.Emp select e;

     

        foreach (Emp emp in emps)

        {

            Console.WriteLine("Employee Id : [{0,2}] Name : {1}", emp.Id, emp.Name );

        }

    }

     

    Namoskar!!!

  • Wriju's BLOG

    Azure Training Kit – April 2009 Update

    • 1 Comments

    Is available at http://www.microsoft.com/downloads/details.aspx?FamilyID=413E88F8-5966-4A83-B309-53B7B77EDF78&displaylang=en

    Namoskar!!!

  • Wriju's BLOG

    LINQ to SQL : Returning Scalar Value from Stored Procedure

    • 10 Comments

    In LINQ to SQL it is not that easy thing to achieve as compared to other features. Let us assume you have a Stored Procedure like,

    Case 1: With Output Parameter

    CREATE PROCEDURE [dbo].[GetEmployeeCount]

          @OutVal DateTime OUTPUT

    AS

    BEGIN

          SELECT @OutVal = GetDate()

    END

    You need to write code which will look like,

    using (TestDBDataContext db = new TestDBDataContext())

    {

        //Need a Nullable type here

        //and you need to have some value to it

        DateTime? dt = null;

        var q = db.GetEmployeeCount(ref dt);

     

        Console.WriteLine(dt);               

    }

    Case 2: With Return (only for Integers)

    CREATE PROCEDURE [dbo].[GetEmployeeCountRet]         

    AS

    BEGIN

          DECLARE @Ret INT

          SELECT @Ret = COUNT(*) FROM Emp

          RETURN @Ret

    END

    Your code may look like,

    using (TestDBDataContext db = new TestDBDataContext())

    {

        //For Stored Procedure with Return value (for Integer)

        //returns Int

        var q = db.GetEmployeeCountRet();

     

        Console.WriteLine(q);               

    }

    You cannot simply say

    SELECT COUNT(*) FROM Emp and capture the value in a variable. Because in LINQ to SQL a Stored Procedure either returns ISingleResult<T> or IMultipleResults<T>, so capturing single value becomes very tricky.

    So when you have to do it go for Scalar-Valued function

    Case 3: Using Scalar-Values Functions

    ALTER FUNCTION [dbo].[fn_GetEmployeeCount]()

    RETURNS int

    AS

    BEGIN

          DECLARE @ResultVar int

         

          SELECT @ResultVar = Count(*) FROM Emp

         

          RETURN @ResultVar

    END

    You code,

    using (TestDBDataContext db = new TestDBDataContext())

    {

        var q = db.fn_GetEmployeeCount();

     

        Console.WriteLine(q);               

    }

    Namoskar!!!

  • Wriju's BLOG

    LINQ to SQL : Using ChangeSet for Audit Trail

    • 4 Comments

    Most of the transactional scenario, you may need to add the transaction to some table for ref. many apps does it for admin perspective.

     

    Problem

     

    You can do it in LINQ to SQL with ChangeSet but the problem is with Inserts. It does not give you the Identity field value until and unless you call SubmitChanges. But after SubmitChages() you cannot get the objects. So the trick is as below,

     

     

    //Add new Emp

    Emp emp1 = new Emp() { Name = "E : "+DateTime.Now.ToString() };

     

    //Add new Dept

    Dept dep1 = new Dept() { DeptName = "D : " + DateTime.Now.ToString() };

     

    db.Emps.InsertOnSubmit(emp1);

    db.Depts.InsertOnSubmit(dep1);

     

    //Update Employee

    Emp eUpd = (from em in db.Emps

                where em.Id == 170

                select em).First();

     

    eUpd.Name = DateTime.Now.ToString();

     

    //Track the changed objects

    ChangeSet changedOnes = db.GetChangeSet();

     

     

    //Insert

    /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

     * Since we do not get the Identity column value before SubmitChanges()

     * We need to store them in offline List<T> and then find the Id column after commit

     * This is tricky as we cannot get the Object after calling SubmitChanges()

     * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

     */

    List<Emp> lstEmp = new List<Emp>();

    List<Dept> lstDept = new List<Dept>();

     

    foreach (var obj in changedOnes.Inserts)

    {

        Type ty = obj.GetType();

        if (ty.Name == "Emp")

        {

            lstEmp.Add((Emp)obj);

        }

        if (ty.Name == "Dept")

        {

            lstDept.Add((Dept)obj);

        }

    }

     

    string sData = "Summary : " + changedOnes.ToString() + "\r\n";

     

    //Track Updates

    foreach (var obj in changedOnes.Updates)

    {

        Type ty = obj.GetType();

        sData += "Entity Updated : " + ty.Name + "\r\n";

    }

     

    //Save the changes

    db.SubmitChanges();

     

    //Now find out the Identity Column value

    foreach (var sEmp in lstEmp)

    {

        sData += String.Format("New Emp Id {0} for {1} ", sEmp.Id.ToString(), "Emp") + "\r\n";

    }

     

    foreach (var sDept in lstDept)

    {

        sData += String.Format("New Dept Id {0} for {1} ", sDept.DeptId.ToString(), "Dept") + "\r\n";

    }

     

    MessageBox.Show(sData);

     

     

    Namoskar!!!

     

     

  • Wriju's BLOG

    ADO.NET Entity Framework and Data Services Case Study

    • 1 Comments

    Story of Misys Healthcare Systems is very interesting. They have increased 60% of their development process while migrating their apps to ASP.NET Ajax using Entity Framework and Data Services.

     

    Full story at http://www.microsoft.com/casestudies/casestudy.aspx?casestudyid=4000002427

     

    Namoskar!!!

Page 1 of 1 (10 items)