[C#] LINQ – Convert LINQ to DataTable

Function to Convert LINQ to Datatable that I found from google:

public static DataTable LINQToDataTable(IEnumerable varlist)
        {
            DataTable dtReturn = new DataTable();
            // column names
            PropertyInfo[] oProps = null;
            if (varlist == null) return dtReturn;
            foreach (T rec in varlist)
            {
                // Use reflection to get property names, to create table, Only first time, others will follow
                if (oProps == null)
                {
                    oProps = ((Type)rec.GetType()).GetProperties();
                    foreach (PropertyInfo pi in oProps)
                    {
                        Type colType = pi.PropertyType;
                        if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
                        == typeof(Nullable<>)))
                        {
                            colType = colType.GetGenericArguments()[0];
                        }
                        dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                    }
                }
                DataRow dr = dtReturn.NewRow();
                foreach (PropertyInfo pi in oProps)
                {
                    dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
                    (rec, null);
                }
                dtReturn.Rows.Add(dr);
            }
            return dtReturn;
        }

How to use it:
Read the rest of this entry »

Share

[C#] Full Outer Join 2 datatable using LINQ

datatable 1 = dt1 contains data:

A1
datatable 2 = dt2 contains data:

A2
Expected result:

A3
Solution:

Read the rest of this entry »

Share

[C#] Join 2 datatable and group sum the value using LINQ

Here is the example on how to join 2 datatable and group with sum the value using LINQ in C#

datatable 1 = dt1 contains data:

B1

datatable 2 = dt2 contains data:

B2

Expected result:

B3

Solution:

Read the rest of this entry »

Share

[ASP.NET] Could not load file or assembly ‘Microsoft.ReportViewer.XXX’ in VS2012

So Visual Studio .NET have ReportViewer that looks very useful for reporting, but there is a problem when we want to deploy in production web server: “Could not load file or assembly…” for reportviewer dll or
Could not load file or assembly ‘Microsoft.ReportViewer.Common or
Could not load file or assembly ‘Microsoft.ReportViewer.WebForms or
Could not load file or assembly ‘Microsoft.ReportViewer.WebForms, Version=xx.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxx’ or one of its dependencies. The system cannot find the file specified. or other similar error.

Here are the step to enable reportviewer run in production server without have to install the reportviewer:

taken from : http://msm2020-sc.blogspot.sg/2013/05/could-not-load-file-or-assembly.html

Need to copy the assembly to local bin:
1. from command prompt enter cd C:\WINDOWS\assembly\GAC_MSIL\Microsoft.ReportViewer.ProcessingObjectModel
2. Type dir then press enter. You will see list of version available such as 10.0.0.0__b03f5f7f11d50a3a or 11.0.0.0__89845dcd8080cc91. Choose the version depend on your project used.

3. Type cd the version that you want, for example cd 10.0.0.0__b03f5f7f11d50a3a
4. Then copy all dll to your bin project. copy *.dll d:\RedsouljazProject\bin

5. After add dll to bin, in project add reference Microsoft.ReportViewer.ProcessingObjectModel by browse from our own local bin
6. If we already add Microsoft.ReportViewer.Common and Microsoft.ReportViewer.WebForms, make sure in properties choose Copy Local = True.
This will enable these 3 dll in our local (Include Microsoft.ReportViewer.ProcessingObjectModel dll).

For my case, my rdlc file will not copy when I publish the project, so I need to copy manually to server.

Good luck.

Share

[C#] Truncate decimal instead of rounding

Here is the code to truncate the 2 decimal point from decimal value instead round up or down.

For example:
Value 1: 2.156
Result 1: 2.15

Value 2: 1234.167
Result 2: 1,234.16

private static double TruncateDecimal(double value, int digits)
 {
    double mult = System.Math.Pow(10.0, digits);
    return System.Math.Truncate(value * mult) / mult;
 }

public static string Tuncate2DecimalToString(double value)
{
     if (value.ToString("#,0.0000#", System.Globalization.CultureInfo.InvariantCulture) == "0.0000")
         return "0.00";
     else
         return TruncateDecimal(double.Parse(value.ToString()), 2).ToString("#,0.00#", System.Globalization.CultureInfo.InvariantCulture);
 }

To call this function, simply use the following code:
string result = Tuncate2DecimalToString(2.156)
result = 2.15

Share

[Asp.net] Convert datatable to Json

This code is used to convert datatable to json format. It also handle double quote and backslash issue.

public static string Convert(DataTable dt)
        {           
            string sb = "";
            sb += "[";
            foreach (DataRow dr in dt.Rows)
            {
                sb += "{";
                foreach (DataColumn dc in dt.Columns)
                {
                    //Column Name
                    sb += "\"";
                    sb += dc.ColumnName;
                    sb += "\":";
                    //Column Value
                    sb += "\"";
                    //Value contain " must be replace with \"
                    sb += dr[dc].ToString().Replace(@"\", @"\\").Replace("\"", "\\\"");
                    sb += "\",";
                }
                if (sb.EndsWith(","))
                {
                    sb = sb.Substring(0, sb.Length - 1);
                }
                sb += "},";
            }
            if (sb.EndsWith(","))
            {
                sb = sb.Substring(0, sb.Length - 1);
            }
            sb += "]";

            return sb;
        }

Read the rest of this entry »

Share

Collapsible Drag & Drop Panels like WordPress Dashboard using Jquery and ASP.NET

I found a great article that teach us how to create drag and drop panel like dashboard in wordpress panel.
In this sites: http://webdeveloperplus.com/jquery/saving-state-for-collapsible-drag-drop-panels/ shows us how to create the drag and drop panel and save the state in PHP and mysql. In this article, I will show the code in ASP.NET using SQL Server 2005.

4-25-2011 4-15-33 PM
Read the rest of this entry »

Share

VB.NET – ASP.NET communication between user control and web page using event handler

In this post, we want to show how button in user control communicate with web page in asp.net

First create user control called button.ascx.

Add 3 button, name it btnSave, btnEdit and btnCancel.

in button.ascx back code, add this code:

Public Partial Class button
    Inherits System.Web.UI.UserControl
    Public Event btnSaveHandler As System.EventHandler
    Public Event btnEditHandler As System.EventHandler
    Public Event btnCancelHandler As System.EventHandler

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
        RaiseEvent btnSaveHandler(Me, New EventArgs())
    End Sub

    Protected Sub btnEdit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnEdit.Click
        RaiseEvent btnEditHandler(Me, New EventArgs())
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click
        RaiseEvent btnCancelHandler(Me, New EventArgs())
    End Sub
End Class

In Default.aspx, add 1 label called label1, then drag the button.ascx to the default.aspx.
Add code in default.aspx as below:

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Label1.Text = Request.QueryString("label")
        AddHandler button1.btnCancelHandler, AddressOf cancelClick
        AddHandler button1.btnEditHandler, AddressOf editClick
        AddHandler button1.btnSaveHandler, AddressOf saveClick
    End Sub

    Private Sub cancelClick(ByVal sender As Object, ByVal e As EventArgs)
        Response.Redirect("default.aspx?label=Cancel")
    End Sub

    Private Sub editClick(ByVal sender As Object, ByVal e As EventArgs)
        Response.Redirect("default.aspx?label=Edit")
    End Sub

    Private Sub saveClick(ByVal sender As Object, ByVal e As EventArgs)
        Response.Redirect("default.aspx?label=Save")
    End Sub
End Class

Run it, you will see that button that we create in user control can be manipulate and used in default.aspx
See other example for communication between user control and page in:

http://www.codeproject.com/KB/user-controls/Page_UserControl.aspx

Share

ILight Marina Bay Singapore

Lumenocity Singapore

Lumenocity Singapore

Read the rest of this entry »

Share

Star Cruise Superstar Virgo Trip to Redang

redang_Panorama1
star_cruise_virgo_deck3

Read the rest of this entry »

Share