Monday, May 26, 2014

ASP.NET Gridview sorting that bound to a List<> of custom objects as DataSource

In AsP.NET, Gridview with List<> of custom objects as DataSource does not support sorting. Here is a code to achieve it. Here the sort direction and Sort column is stored in a ViewState. So when we click a column name to sort it will check the previous sort direction from the ViewState["SortDirection"] and ViewState["SortColumn"]. If the previous direction is ASC means it will do DESC sort vice versa. I have used LINQ to sort the List. The sorting is done inside the GridView Rowcommand event with the help of CommandName and CommandArgument as parameter. And also you have to change the Gridview header from Label to LinkButton in the Header Template as it will make postbacks. Check out the code you can learn some new C# techniques as i have learned when i tried to implement this functionalities. Design Code:

<asp:GridView ID="grdManageUsers" runat="server" AutoGenerateColumns="False" OnRowCommand="grdManageUsers_RowCommand">
    <Columns>
        <asp:TemplateField HeaderText="First Name">
            <HeaderTemplate>
                <asp:LinkButton ID="lnkFirstname" runat="server" CommandName="Sort" CommandArgument="Firstname">First Name</asp:LinkButton>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblFirstNameEdit" runat="server" Text='<%# Eval("Firstname")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtFirstname" runat="Server" Text='<%# Eval("Firstname") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Last Name">
            <HeaderTemplate>
                <asp:LinkButton ID="lnklastname" runat="server" CommandName="Sort" CommandArgument="Lastname">Last Name</asp:LinkButton>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblLastnameEdit" runat="server" Text='<%# Eval("Lastname")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtLastname" runat="Server" Text='<%# Eval("Lastname") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Email">
            <HeaderTemplate>
                <asp:LinkButton ID="lnkEmail" runat="server" CommandName="Sort" CommandArgument="Email">Email</asp:LinkButton>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblEmailEdit" runat="server" Text='<%# Eval("Email")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtEmail" runat="Server" Text='<%# Eval("Email") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        </Columns>
    </asp:GridView>

Code behind C#:

List userList = new List();

private SortDirection LastSortDirection
{
    get
    {
        return (ViewState["LastSortDirection"] == null) ? SortDirection.Descending : (SortDirection)ViewState["LastSortDirection"];
    }
    set
    {
        ViewState["LastSortDirection"] = value;
    }
}

private string LastSortColumn
{
    get
    {
        return (ViewState["LastSortColumn"] == null) ? "Firstname" : (string)ViewState["LastSortColumn"];
    }
    set
    {
        ViewState["LastSortColumn"] = value;
    }
}
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        try
        {
           userList = GetUsers(); // It will return the List of users from data source.
           ViewState["USERLIST"] = userList; // It can be used for sorting
        }
        catch (Exception ex)
        {
           
        }
    }
}
protected void grdManageUsers_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {
        if (e.CommandName == "Sort" && !string.IsNullOrEmpty(e.CommandArgument.ToString()))
        {
            userList = ViewState["USERLIST"] as List;
            if (userList != null && userList.Count > 0)
            {
                IQueryable query = userList.AsQueryable();
                string newSortColumn = string.Empty;
                SortDirection newSortDirection = SortDirection.Descending;
                BindGridView(Sort(query, e.CommandArgument.ToString(), LastSortColumn, LastSortDirection, out newSortColumn, out newSortDirection));
                LastSortColumn = newSortColumn;
                LastSortDirection = newSortDirection;
            }
        }
    }
    catch (Exception ex)
    {      
    }
}
private void BindGridView(List users)
{
     grdManageUsers.DataSource = users;
     grdManageUsers.DataBind();
}
public List Sort(IQueryable list, string sortColumn, string lastSortColumn, SortDirection lastSortDirection, out string newSortColumn, out SortDirection newSortDirection)
{
    newSortColumn = string.Empty;
    newSortDirection = SortDirection.Descending;
    var result = new List();
    try
    {
        if (sortColumn == lastSortColumn)
        {
            newSortColumn = sortColumn;
            if (lastSortDirection == SortDirection.Ascending)
            {
                newSortDirection = SortDirection.Descending;
                result = SortProcess(list, SortDirection.Descending, sortColumn);
            }
            else
            {
                newSortDirection = SortDirection.Ascending;
                result = SortProcess(list, SortDirection.Ascending, sortColumn);
            }
        }
        else
        {
            newSortColumn = sortColumn;
            newSortDirection = SortDirection.Ascending;
            result = SortProcess(list, SortDirection.Ascending, sortColumn);
        }
    }
    catch
    {
        throw;
    }
    return result;
}
public List SortProcess(IQueryable list, SortDirection sortDirection, string sortColumn)
{
    IQueryable query = list.AsQueryable();
    try
    {
        var pi = typeof(T).GetProperty(sortColumn);

        if (sortDirection == SortDirection.Ascending)
        {
            query = query.OrderBy(x => pi.GetValue(x, null));
        }
        else
        {
            query = query.OrderByDescending(x => pi.GetValue(x, null));
        }
    }
    catch
    {
        throw;
    }
    return query.ToList();
}

Thursday, May 22, 2014

Project Euler Solution using C#: Problem 22: Names Scores

Problem:

Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.

What is the total of all the name scores in the file?
My Solution:

static void Main(string[] args)
{
    StreamReader sr = new StreamReader("names.txt");
    string textNames = sr.ReadToEnd().Replace("/", "").Replace("\"", "");
    char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
    long result = 0;
    string[] arr = textNames.Split(',');
    Array.Sort(arr);
    for (int i = 1; i <= arr.Length; i++)
    {
        int score = 0;
        foreach (var eachChar in arr[i - 1].ToCharArray())
        {
            score += (Array.IndexOf(alpha, eachChar) + 1);
        }
        score *= i;
        result += score;
    }
    Console.WriteLine(result);
    Console.ReadLine();
}

Note: You can simplifies the coding :)

Project Euler Solution using C#: Problem 20: Factorial Digit Sum

Problem:

n! means n × (n − 1) × ... × 3 × 2 × 1 For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. Find the sum of the digits in the number 100! My Solution:

static void Main(string[] args)
{
    BigInteger multi = 1;
    long result = 0;
    int digits = 100;
    for (int i = digits; i >= 1; i--)
    {
        multi *= i;
    }
    foreach (var item in multi.ToString().ToCharArray())
    {
        result += long.Parse(item.ToString());
    }
    Console.WriteLine(result);
    Console.ReadLine();
}

Note: You can simplifies the coding :)

Wednesday, May 14, 2014

Handle Synchronous and Asynchronous Postbacks from the ASP.NET Updatepanel using Javascript

Here is a simple code to handle all the Synchronous and Asynchronous Postbacks from the ASP.NET Updatepanel using Javascript. We can get the control that makes the Postback. So Its very useful to show/hide loading image when asynchronous Postback happens. JavaScript Code:
<script type="text/javascript">
        $(document).ready(function ()
        {
            var prm = Sys.WebForms.PageRequestManager.getInstance();
            prm.add_initializeRequest(prm_InitializeRequest);
            prm.add_endRequest(prm_EndRequest);
            var ctrl;
            function prm_InitializeRequest(sender, args)
            {
                ctrl = args.get_postBackElement().id;
                if (ctrl == "ContentPlaceHolder1_btnAddUser") 
                {
                $('#imgLoading').show();
                }
            }
            function prm_EndRequest(sender, args)
            {
              $('#imgLoading').hide();
            }
        });
</script>

Note: I have used jquery show/hide function to show/hide loading image. so you have to use the Jquery API.

How to search for a text in SQL Server Database using Stored procedure

Here is the stored procedure that will search and display the table name and column name of the text you are passing as a parameter to search from the Database.
SQL Query
CREATE PROC SearchTextFromDatabaseTables
(
    @SearchString nvarchar(100)
)
AS
BEGIN
    CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))
    SET NOCOUNT ON
    DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
    SET  @TableName = ''
    SET @SearchStr2 = QUOTENAME('%' + @SearchString + '%','''')
    WHILE @TableName IS NOT NULL
    BEGIN
        SET @ColumnName = ''
        SET @TableName =
        (
            SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
            FROM     INFORMATION_SCHEMA.TABLES
            WHERE         TABLE_TYPE = 'BASE TABLE'
                AND    QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
                AND    OBJECTPROPERTY(
                        OBJECT_ID(
                            QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
                             ), 'IsMSShipped'
                               ) = 0
        )
        WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)
        BEGIN
            SET @ColumnName =
            (
                SELECT MIN(QUOTENAME(COLUMN_NAME))
                FROM     INFORMATION_SCHEMA.COLUMNS
                WHERE         TABLE_SCHEMA    = PARSENAME(@TableName, 2)
                    AND    TABLE_NAME    = PARSENAME(@TableName, 1)
                    AND    DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'int', 'decimal')
                    AND    QUOTENAME(COLUMN_NAME) > @ColumnName
            )
            IF @ColumnName IS NOT NULL
            BEGIN
                INSERT INTO #Results                 EXEC                 (                     'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630)                     FROM ' + @TableName + ' (NOLOCK) ' +                     ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2                 )             END         END       END     SELECT ColumnName, ColumnValue FROM #Results END

Execute the Procedure using the below query and pass the text to be searched in it.
exec SearchTextFromDatabaseTables 'TEXT TO BE SEARCHED'

and you will get the result as below
    ColumnName                                    ColumnValue
1  [dbo].[TABLENAME].[COLUMNNAME]                SEARCHED TEXT STRING