November 2008 - Posts

Versioning ASP.NET 2.0 WebSite Assembly / Web Deployment Project

The web project model changed in number of ways from Visual Studio 2003 to Visual Studio 2005, But one major part which is missing is the ability to version the assembly using AssemblyInfo.cs file.

This is because the model of VS 2005 dynamically creates multiple assemblies for each class file. Resulting in which we cannot have single named assembly to set version number.

We cannot change the default behaviour of compilation but we can change the way we can deploy our project files and assemblies, by using Web Deployment Project.

Microsoft Web Deployment Project adds numerous features which nicely integrates with Visual Studio 2005, out of which the most useful I found is:

More control over number of assemblies generated by a pre-complied web application as well as control over the naming of the output assemblies. Which means you can generate either single assembly for you entire project, and select the name and version you want for e.g Foo.dll, etc or generate the assemblies per directory/ pages or control.

The ability to customize and modify the web.config file during deployment, that means you need not to change you web.config file every time before making release or deploying the project, instead you can specify the web.config keys on Web Deployment project, that you use for deployment, like this you can customize any other web.config settings also.

These are the few things which I named here, if you want more information on how to install, and use this you can visit the Scott's Blog from the below link:

http://weblogs.asp.net/scottgu/archive/2005/11/06/429723.aspx

Or you can Download the VS 2005 Web Deployment Project from

WebDeploymentSetup.msi

Other Useful Links which may help you to further explore this subject

http://msdn.microsoft.com/en-us/library/aa479568.aspx

Web Deployment Projects Forum

I hope this will help you, if you have something to share on this topic, then please leave a comment.

Thanks

~Brij

SQL Server Coding Standards

ASP.NET, C# or VB.NET often goes hands in hands, In my previous post I have given the C# Coding Standards, but without the correct SQL Server Coding standards, my previous post was almost incomplete, so in this post I am giving you the Link to SQL Server Coding Standards.

You can get more details in Pinal Dave SQLAuthority.com, http://blog.sqlauthority.com/2007/06/06/sql-server-database-coding-standards-and-guidelines-complete-list-download/

This post by Pinal Dave consists of Series of Database Coding Standards and Guidelines.

SQL SERVER Database Coding Standards and Guidelines - Introduction
SQL SERVER - Database Coding Standards and Guidelines - Part 1
SQL SERVER - Database Coding Standards and Guidelines - Part 2


SQL SERVER Database Coding Standards and Guidelines Complete List Download

C# Coding Standards by Lance's

Following the right Coding standars comes with Practice and proper Guidance if you are a starter, and also lots of companies define or customize their own coding standards.

When I came across this challange to define the coding standard for my team, I thought instead of reinventing the wheel and recollecting everything from my experience, better if I get something which is already defined and documented. So I started googling and came across a very good document which covers almost all the aspects of C# Coding standards which I was looking for. This Document covers the following topics*

  • Cover all major C# Language features.
  • Provide guidelines on coding style & language usage (but not syntax)
  • Demonstrate all rules in code where applicable.
  • Describe rules in structure that is easy to read & use.
  • Define clear & concise rules.
  • Use consistent terminology & rule patterns.
  • Only provide rules where there is a clear cut best practice.
  • Lead developers to a "pit of success" and avoid common "pits of failure"

    You can download the PDF from here http://weblogs.asp.net/lhunt/attachment/591275.ashx

    Original Post you can find here : http://weblogs.asp.net/lhunt/pages/CSharp-Coding-Standards-document.aspx

    Thanks

    ~Brij

    * All the Copyrights in this Post are the property of the Respective Owner.

     

     

  • LINQ to XML and LINQ to Objects Basic Sample

    In this post I will show how to use LINQ to XML and LINQ to Objects, very basic example with sample code.

    image  image

    First I have created a XML file which contains the Customer Details, as given below

    <?xml version="1.0" encoding="utf-8" ?>
    <
    customers>
        <
    customer>
            <
    customerid>ALFKI</customerid>
            <
    city>Berlin</city>
            <
    age>20</age>
        </
    customer>
        <
    customer>
            <
    customerid>BONAP</customerid>
            <
    city>Marseille</city>
            <
    age>21</age>
        </
    customer>
        <
    customer>
            <
    customerid>CONSH</customerid>
            <
    city>London</city>
            <
    age>30</age>
        </
    customer>
        <
    customer>
            <
    customerid>EASTC</customerid>
            <
    city>London</city>
            <
    age>34</age>
        </
    customer>
        <
    customer>
            <
    customerid>FRANS</customerid>
            <
    city>Torino</city>
            <
    age>35</age>
        </
    customer>
        <
    customer>
            <
    customerid>LONEP</customerid>
            <
    city>Portland</city>
            <
    age>40</age>
        </
    customer>
        <
    customer>
            <
    customerid>NORTS</customerid>
            <
    city>London</city>
            <
    age>25</age>
        </
    customer>
        <
    customer>
            <
    customerid>THEBI</customerid>
            <
    city>Portland</city>
            <
    age>36</age>
        </
    customer>
    </
    customers>

     

    I have also created a class with the same properties which I have given in the XML document above, and populating the same data,

    public class Customer
    {
    public string CustomerID { get; set; }
    public string City { get; set; }
    public int Age { get; set; }
    public static IEnumerable<Customer> CreateCustomers()
    {
       return new List<Customer>
       {
          new Customer { CustomerID = "ALFKI", City = "Berlin", Age=20   },
          new Customer { CustomerID = "BONAP", City = "Marseille" , Age=21},
          new Customer { CustomerID = "CONSH", City = "London", Age=30    },
          new Customer { CustomerID = "EASTC", City = "London", Age=34    },
          new Customer { CustomerID = "FRANS", City = "Torino", Age=35    },
          new Customer { CustomerID = "LONEP", City = "Portland", Age=40  },
          new Customer { CustomerID = "NORTS", City = "London" , Age=25   },
          new Customer { CustomerID = "THEBI", City = "Portland", Age=26  }    
       };
     }
    }

    Now I have got the Object ready to query using LINQ with the Same data which I have in my XML Document.

    Note above how I'm using the new "Automatic Properties and Object Initializers" feature of C# to define the properties (and avoid having to define a field for them and initialize the Objects). And In all the functions below I am reading the XML Document, using XDocument Class within System.Xml.Linq namespace to open and query document.

    First I have to populate the DropDownList in the UI, to display all the Cities, I am reading the Cities from the object alternatively you can also read the same from XML document, both will return the same result. This DropDown will allow the users to select the customers located in the selected City.

    public static List<Customer> GetCities()
    {
         var customers = from customer in Customer.CreateCustomers()
                         orderby customer.City
                         select new Customer { City = customer.City };
         return customers.ToList();
    }

    I have also added one more DropDown to let the user to select the DataSource (XML or Object), once both the selection is made, users can click on Get Customers to Get the Customer details. Depending on the DataSource and City, the function below will return the List of Customers wither from XML or the Object.

     

    public static List<Customer> GetCustomerFromXML(string city)
    {
        XDocument xmlDoc = XDocument.Load(HttpContext.Current.Server.MapPath("CustomerXML.xml"));
        var customers = from customer in xmlDoc.Descendants("customer")
                        where customer.Element("city").Value == city
                        select new Customer
                        {
                            CustomerID = customer.Element("customerid").Value,
                            City = customer.Element("city").Value,
                            Age = Convert.ToInt32(customer.Element("age").Value)
                        };
        return customers.ToList();
    }

    Similarly I am writing the function below to read the object, and returning the List<Customer> which we can bind directly to the GridView.

    public static List<Customer> GetCustomersFromObject(string city)
    {
         var customers = from customer in Customer.CreateCustomers()
                         where customer.City == city
                         select new Customer
                         {
                             CustomerID = customer.CustomerID,
                             City = customer.City,
                             Age = customer.Age
                         };
         return customers.ToList();
     }

    Note: In the functions above the only trick I've made to the LINQ to XML query is to use the "select new Customer" instead of only "select" clause from "select new" (with no type-name).  With this trick I'm returning a sequence of Customer objects that I can pass from class to class, assembly to assembly, and across web-services.

    In the code behind of ASPX Page first I have populated the Cities DropDown to List all the cities on Page_Load, then OnClick of the Get Customers  I have written the code to Get the City, and the DataSource from the DropDownList and Call the appropriate method depending on the DataSource.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //get all the cities
            ddlCity.DataSource = Customer.GetCities();
            ddlCity.DataTextField = "city";
            ddlCity.DataValueField = "city";
            ddlCity.DataBind();
        }
    }
    protected void btnGetCustomers_Click(object sender, EventArgs e)
    {
        //binding the Customer from Object
        if (ddlSource.SelectedValue == "Object")
        {
            gvCustomerDetails.Visible = true;
            lblMessage.Text = "Displaying data from Object";
            gvCustomerDetails.DataSource = Customer.GetCustomersFromObject(ddlCity.Text);
            gvCustomerDetails.DataBind();
        }
        //binding the Customer from XML
        else if (ddlSource.SelectedValue == "XML")
        {
            gvCustomerDetails.Visible = true;
            lblMessage.Text = "Displaying data from XML";
            gvCustomerDetails.DataSource = Customer.GetCustomerFromXML(ddlCity.Text);
            gvCustomerDetails.DataBind();
        }
        else
        {
            gvCustomerDetails.Visible = false;
            lblMessage.Text = "Please select the Data Source";
        }
    }

    Below is my ASPX code which is very much self explanatory, so I have not given much explanation for that, this just for the control name reference.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <h3>
            LINQ to Object and LINQ to XML Demo</h3>
        <div>
            <table>
                <tr>
                    <td>
                        <label for="source" id="lblSource">
                            Data Source :</label>
                    </td>
                    <td>
                        <asp:DropDownList ID="ddlSource" runat="server">
                            <asp:ListItem Text="Select Source" Value="0" Selected="True"></asp:ListItem>
                            <asp:ListItem Text="XML" Value="XML"></asp:ListItem>
                            <asp:ListItem Text="Object" Value="Object"></asp:ListItem>
                        </asp:DropDownList>
                    </td>
                    <td></td>
                </tr>
                <tr>
                    <td>
                        <label for="empid" id="lblEmp">
                            Select City :</label>
                    </td>
                    <td>
                        <asp:DropDownList runat="server" ID="ddlCity"></asp:DropDownList>
                    </td>
                    <td><asp:Button runat="server" ID="btnGetCustomers" Text="Get Customers" 
                            onclick="btnGetCustomers_Click" /></td>
                </tr>
            </table>
            <br />
            <asp:Label runat="server" ID="lblMessage"></asp:Label>
            <br />
            <asp:GridView ID="gvCustomerDetails" runat="server">
            </asp:GridView>
        </div>
        </form>
    </body>
    </html>
     

    You can download the sample code from here.

    You can refer my previous posts, to see the Example of LINQ To SQL.

     

    For more knowledge you can refer the post below.

    http://weblogs.asp.net/scottgu/archive/2007/08/07/using-linq-to-xml-and-how-to-build-a-custom-rss-feed-reader-with-it.aspx

    http://www.c-sharpcorner.com/UploadFile/scottlysle/L2OinCS05242008233051PM/L2OinCS.aspx