14 May 2015

Copy Data between two different MSSQL Databases on different servers using C#

You can copy data from one SQL table to another using INSERT command with SELECT within same database or databases on same server but things gets little complicated when databases are on two different server. Here is a C# snipplet you can use to copy data between two desperate databases on two different servers. Code is self explanatory with comments.  Your source and destination table fields needs to match.


// Create source connection
SqlConnection source = new SqlConnection(ConfigurationManager.ConnectionStrings["SourceConnectionString"].ConnectionString);
// Create destination connection
SqlConnection destination = new SqlConnection(ConfigurationManager.ConnectionStrings["DestinationConnectionString"].ConnectionString);


// Open source and destination connections.
source.Open();
destination.Open();

// Select data from Products table
SqlCommand cmd = new SqlCommand(@"SELECT customer
         ,name
         ,address1
         ,address2
         ,address3
         ,address4
         ,address6
         ,address5
         ,fax
         ,territory
         ,region
         ,class
        FROM
        [dbo].[slcustm]", source);
// Execute reader
Console.WriteLine("Read data  from [dbo].[slcustm] table");
SqlDataReader reader = cmd.ExecuteReader();

Console.WriteLine("Write data  to DestinationCustomers table");
// Create SqlBulkCopy

SqlBulkCopy bulkData = new SqlBulkCopy(destination);
//If you are copying larger amount of data don't forget to set the timeout flag. Default value is 30 seconds. 0 = No limit
bulkData.BulkCopyTimeout = 0;

// Set destination table name
bulkData.DestinationTableName = "DestinationCustomers";
// Write data
bulkData.WriteToServer(reader);
// Close objects
bulkData.Close();
destination.Close();

03 May 2015

ASP.NET Gridview with Filter in Header using Reflection and LINQ

Introduction

ASP.NET gridview by default provides facility for sorting and paging but no inbuilt facility to filter column. This article looks at possible way to implement filtering function within the Gridview.

Background

I came across this requirement of having a gridview which allows filtering data from within the gridview. I also wanted to preserve the sorting and paging of the gridview.
Rather than creating separate panel above gridview for each of the fields to filter data, wouldn't it be nice to put a textbox along with each header column to filter data.
This leads me to this solution I derived for it. This may not be the best solution to do it but it definitely works. Our goal is to achieve this.

You can download full source code from : ASP.NET Gridview with Filter in Header Source Code


How It All Works ?

Create ASP.NET Web Application project in Visual Studio. First of all, we will create a DTO class to hold some data that we can display in a gridview. For this demo, I have created a DTO class of outstanding orders that contains some properties and some dummy data.

[Serializable]
public class Outstanding
{
 public string Item { get; set; }
 public string Order { get; set; }
 public int Line { get; set; }
 public int Status { get; set; }
 public string ToLocation { get; set; }
 public decimal Qty { get; set; }
 public DateTime RegDate { get; set; }
 public string Location { get; set; }
 public decimal AllocQty { get; set; }

 public List GetOutstanding()
 {
  List lstOrders = new List();

  lstOrders.Add(new Outstanding() { Item = "CocaCola", 
  Order = "000101", Line = 1, Status = 20, 
  ToLocation = "Sydney", 
  Qty = 2000, RegDate = new DateTime(2014, 1, 1), 
  Location = "USA", AllocQty = 100 });
  lstOrders.Add(new Outstanding() { Item = "BubbleGum", 
  Order = "000101", Line = 1, Status = 20, 
  ToLocation = "Sydney", 
  Qty = 2500, RegDate = new DateTime(2014, 1, 11), 
  Location = "USA", AllocQty = 300 });
  lstOrders.Add(new Outstanding() { Item = "Coffee", 
  Order = "000111", Line = 1, Status = 50, 
  ToLocation = "Melbourne", 
  Qty = 2500, RegDate = new DateTime(2014, 1, 10), 
  Location = "USA", AllocQty = 100 });
  lstOrders.Add(new Outstanding() { Item = "Sugar", 
  Order = "000112", Line = 1, Status = 50, 
  ToLocation = "Melbourne", 
  Qty = 2300, RegDate = new DateTime(2014, 1, 10), 
  Location = "NZ", AllocQty = 300 });
  lstOrders.Add(new Outstanding() { Item = "Milk", 
  Order = "000112", Line = 1, Status = 50, 
  ToLocation = "Melbourne", 
  Qty = 2300, RegDate = new DateTime(2014, 1, 10), 
  Location = "NZ", AllocQty = 200 });
  lstOrders.Add(new Outstanding() { Item = "Green Tea", 
  Order = "000112", Line = 1, Status = 20, 
  ToLocation = "Melbourne", 
  Qty = 300, RegDate = new DateTime(2014, 1, 10), 
  Location = "NZ", AllocQty = 220 });
  lstOrders.Add(new Outstanding() { Item = "Biscuit", 
  Order = "000131", Line = 1, Status = 70, 
  ToLocation = "Perth", 
  Qty = 200, RegDate = new DateTime(2014, 1, 12), 
  Location = "IND", AllocQty = 10 });
  lstOrders.Add(new Outstanding() { Item = "Wrap", 
  Order = "000131", Line = 1, Status = 20, 
  ToLocation = "Perth", 
  Qty = 2100, RegDate = new DateTime(2014, 1, 12), 
  Location = "IND", AllocQty = 110 });

  return lstOrders;
 }
}
Now in the Default.aspx page, add a gridview. To preserve sorting, add link button in HeaderTemplate with CommandName as "Sort" and CommandArgument as name of the column. Also, for the purpose of filtering the application will bind all the textboxes to single event (OnTextChanged="txtItem_TextChanged" ) and within the event we will determine which textbox fired it and take action accordingly.  So columns of the gridview will look like this. I have used different filters like =,>,<,>=&<= for numeric data and "contains" filter for string values.
Note : Make sure you name all your textboxes as txtFieldName so when filtering we can remove the txt from the ID of the textbox and then use reflection and LINQ to filter the data.
<asp:TemplateField SortExpression="Item">
    <HeaderTemplate>
      <asp:LinkButton ID="lbItem" runat="server" Text="Item" 
      CommandName="Sort" CommandArgument="Item"></asp:LinkButton>
      <br />
      <asp:TextBox runat="server" ID="txtItem" AutoPostBack="true" 
      OnTextChanged="txtItem_TextChanged"></asp:TextBox>
    </HeaderTemplate>
    <ItemTemplate>
       <%#Eval("Item") %>
    </ItemTemplate>
    </asp:TemplateField><asp:TemplateField SortExpression="Line" 
    ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right">
     <HeaderTemplate>
        <asp:LinkButton ID="lbLine" runat="server" Text="Line" 
        CommandName="Sort" CommandArgument="Line"
        CssClass="RightAlign"></asp:LinkButton>
      <br />
     <table>
      <tr>
       <td>
        <asp:DropDownList runat="server" 
        ID="ddlFilterTypeLine" CssClass="upperCaseText">
        <asp:ListItem Text="=" Value="=" 
        Selected="True"></asp:ListItem>
        <asp:ListItem Text=">" Value=">"></asp:ListItem>
         <asp:ListItem Text=">=" Value=">="></asp:ListItem>
         <asp:ListItem Text="<" Value="<"></asp:ListItem>
        <asp:ListItem Text="<="  Value="<="></asp:ListItem>
         </asp:DropDownList>
      </td>
     <td>
     <asp:TextBox runat="server" ID="txtLine" Width="50" 
     AutoPostBack="true" OnTextChanged="txtItem_TextChanged" 
     CssClass="upperCaseText"></asp:TextBox>
    </td>
</tr>
</table></HeaderTemplate>
<ItemTemplate>
<%#Eval("Line","{0:0}")%>
</ItemTemplate>
</asp:TemplateField> 
Now in the Page_Load event, we will bind gridview to the dummy data. I have kept data in ViewState for this demo.
if (!Page.IsPostBack)
{
 Outstanding objOutstanding = new Outstanding();

 List lstOutstandingOrders = new List();
 lstOutstandingOrders = objOutstanding.GetOutstanding();

 ViewState["columnNameO"] = "RegDate";
 grdViewOutstanding.DataSource = lstOutstandingOrders;
 grdViewOutstanding.DataBind();

 ViewState["lstOutstandingOrders"] = lstOutstandingOrders;
 upnlOutstanding.Update();
}  
In the textbox's text change event, we will find out which textbox fired it by looking at the ID of a sender and take action accordingly. Finally, we will bind the data to gridview. To preserve the values in filter after postback, I created a seperate method which gets called everytime postback occurs and set values in corresponding textboxes and filters after postback.

In here what happens is all the textboxes are bound to single event so when even is fired you will first find out which textbox has fired that event and remove txt from the ID of textbox to get the name of the property to filter. x.GetType().GetProperty(filterName).GetValue(x, new object[] { } ) provides the value of associated property from list of objects. This is using reflection to get the property of Outstanding class based on input "filterName" as string value and then get the value of the property from the object and compare it to what is passed in the textbox.

 
        // For Outstanding Orders - Single Event bound  to all textboxes
        protected void txtItem_TextChanged(object sender, EventArgs e)
        {
            if (ViewState["lstOutstandingOrders"] != null)
            {
                List allOutstanding = (List)ViewState["lstOutstandingOrders"];
                TextBox txtBox = (TextBox)sender;
                string filterName = txtBox.ID.Substring(3); // remove txt from Textbox ID. You need to make sure that all the textboxes for filtering are named as txtFieldName
                //Check if there is a dropdown associated with current filter.
                if (grdViewOutstanding.HeaderRow.FindControl("ddlFilterType" + filterName) != null)
                {
                    //Get value from filter type dropdown
                    string filtrerType = ((DropDownList)grdViewOutstanding.HeaderRow.FindControl("ddlFilterType" + filterName)).SelectedItem.Value;

                    //Special case for DateTime
                    if (filterName == "RegDate")
                    {
                        DateTime filterValue = DateTime.Parse(txtBox.Text.Trim());
                        //Use LINQ reflection to find value for the input filer value
                        //x.GetType().GetProperty(filterName).GetValue(x, new object[] { })-- This is the LINQ reflection to get value 
                        //x.GetType().GetProperty(filterName) -- This gives you the actual property associated with Outstanding class based on input property name as string value. 
                        //Then we call get value to get its acutal value and compare it with what is being passed into textbox
                        if (filtrerType == "=")
                            allOutstanding = allOutstanding.Where(x => DateTime.Parse(x.GetType().GetProperty(filterName).GetValue(x, new object[] { }).ToString()) == filterValue).ToList();
                        else if (filtrerType == ">")
                            allOutstanding = allOutstanding.Where(x => DateTime.Parse(x.GetType().GetProperty(filterName).GetValue(x, new object[] { }).ToString()) > filterValue).ToList();
                        else if (filtrerType == ">=")
                            allOutstanding = allOutstanding.Where(x => DateTime.Parse(x.GetType().GetProperty(filterName).GetValue(x, new object[] { }).ToString()) >= filterValue).ToList();
                        else if (filtrerType == "<")
                            allOutstanding = allOutstanding.Where(x => DateTime.Parse(x.GetType().GetProperty(filterName).GetValue(x, new object[] { }).ToString()) < filterValue).ToList();
                        else if (filtrerType == "<=")
                            allOutstanding = allOutstanding.Where(x => DateTime.Parse(x.GetType().GetProperty(filterName).GetValue(x, new object[] { }).ToString()) <= filterValue).ToList();
                    }
                    else // Parse Numbers as decimal
                    {
                        if (filtrerType == "=")
                            allOutstanding = allOutstanding.Where(x => decimal.Parse(x.GetType().GetProperty(filterName).GetValue(x, new object[] { }).ToString()) == decimal.Parse(txtBox.Text.Trim())).ToList();
                        else if (filtrerType == ">")
                            allOutstanding = allOutstanding.Where(x => decimal.Parse(x.GetType().GetProperty(filterName).GetValue(x, new object[] { }).ToString()) > decimal.Parse(txtBox.Text.Trim())).ToList();
                        else if (filtrerType == ">=")
                            allOutstanding = allOutstanding.Where(x => decimal.Parse(x.GetType().GetProperty(filterName).GetValue(x, new object[] { }).ToString()) >= decimal.Parse(txtBox.Text.Trim())).ToList();
                        else if (filtrerType == "<")
                            allOutstanding = allOutstanding.Where(x => decimal.Parse(x.GetType().GetProperty(filterName).GetValue(x, new object[] { }).ToString()) < decimal.Parse(txtBox.Text.Trim())).ToList();
                        else if (filtrerType == "<=")
                            allOutstanding = allOutstanding.Where(x => decimal.Parse(x.GetType().GetProperty(filterName).GetValue(x, new object[] { }).ToString()) <= decimal.Parse(txtBox.Text.Trim())).ToList();
                    }
                    //Hold Filter Type in ViewState to preserve what is selected for use during postback
                    ViewState["OFilter" + filterName] = filtrerType;
                }
                else // Only string value
                {
                    allOutstanding = allOutstanding.Where(x => x.GetType().GetProperty(filterName).GetValue(x, new object[] { }).ToString().ToUpper().Contains(txtBox.Text.Trim().ToUpper())).ToList();
                }

                //Hold the filter value in ViewState. This will be used in ResetFilterAndValueOutstanidn() during postback
                ViewState["O" + filterName] = txtBox.Text.Trim().ToUpper();

                ViewState["lstOutstandingOrders"] = allOutstanding;
                grdViewOutstanding.DataSource = allOutstanding;
                grdViewOutstanding.DataBind();

                ResetFilterAndValueOutstanding();

            }


        } 
ResetFilterAndValueOutstanding() method restores values in filter textbox and filter type in dropdown after each postback. All the filter values and filter types are stored in ViewState with key value starting with "O". Make sure you don't store any other data in ViewState with key value starting with "O" because when removing the filter we will remove all the ViewState values starting with "O".
        protected void ResetFilterAndValueOutstanding()
        {
            //All the filters and filtervalues are stored in ViewState staring with "O"
            foreach (var k in ViewState.Keys)
            {
                if (k.ToString().StartsWith("O"))
                {
                    //Check if there is a textbox in GridView Header for this ViewState value. 
                    if (grdViewOutstanding.HeaderRow.FindControl("txt" + k.ToString().Substring(1)) != null)
                    {
                        ((TextBox)grdViewOutstanding.HeaderRow.FindControl("txt" + k.ToString().Substring(1))).Text = ViewState[k.ToString()].ToString().ToUpper();
                    }
                    //Check if there is a dropdownlist in GridView for this ViewState value.
                    if (grdViewOutstanding.HeaderRow.FindControl("ddlFilterType" + k.ToString().Substring(1)) != null)
                    {
                        foreach (ListItem li in ((DropDownList)grdViewOutstanding.HeaderRow.FindControl("ddlFilterType" + k.ToString().Substring(1))).Items)
                        {
                            if (li.Text == ViewState["OFilter" + k.ToString().Substring(1)].ToString()) li.Selected = true; else li.Selected = false;
                        }
                    }
                }
            }
          
        }
Add a link button on top of the gridview called "Remove Filter" which will remove all the ViewState with keys starting with "O" and rebind gridview to data and reset all filters to its original values.
protected void lbRemoveFilterOutstanding_Click(object sender, EventArgs e)
        {
            //Find all the ViewState Keys starting with "O". This represents Filters and Filter Values
            List lstKeysToRemove = new List();
            foreach (var k in ViewState.Keys)
            {
                if (k.ToString().StartsWith("O"))
                {
                    lstKeysToRemove.Add(k.ToString());
                }
            }
            foreach (string key in lstKeysToRemove)
            {
                ViewState.Remove(key);
            }
         
            Outstanding objOutstanding = new Outstanding();
            List lstOutstandingOrders = new List();
            lstOutstandingOrders = objOutstanding.GetOutstanding();
          
            grdViewOutstanding.DataSource = lstOutstandingOrders;
            grdViewOutstanding.DataBind();

            ViewState["lstOutstandingOrders"] = lstOutstandingOrders;
        }
There is paging and sorting enabled on the gridview which is easy to understand. This is one of the way to implement filtering on a gridview with paging and sorting.

Happy Coding !!!