2008/10/22

Tips: Closing browser without asking users

In some cases I need to close browser without popping up a window to asking, and here is what I found that really works:
window.open('','_self','');
window.close();
or

window.open('','_parent','');
window.close();

The use of _self or _parent will depend on the situation. When users click the "Exit" button on a page, I will use the _self. When the user select "Yes" to leave on a confirm window pop up, I will use the _parent so that the browser will close correctly.

Tips: Converting DataTable's DataType

Here is the way to convert a DataTable's DataType from whatever types to the String type.
private static DataTable ConvertTableType(DataTable dt)
{
  DataTable newDt = dt.Clone();

  //convert all columns' datatype
  foreach (DataColumn dc in newDt.Columns)
  {
    dc.DataType = Type.GetType("System.String");
  }

  //import data from original table
  foreach (DataRow dr in dt.Rows)
  {
    newDt.ImportRow(dr);
  }
  dt.Dispose();

  return newDt;
}

2008/10/20

Tips: Merge two DataTables (6/16/2009 updated)

How to merge two DataTable into one? Here is what I did:
private static DataTable AppendDataTable(DataTable hostDt, DataTable clientDt)
{
  if (hostDt != null && hostDt.Rows.Count > 0)
  {
    DataRow dr;

    for (int i = 0; i < clientDt.Columns.Count; i++)
    {
      hostDt.Columns.Add(new DataColumn(clientDt.Columns[i].ColumnName));

      if (clientDt.Rows.Count > 0)
        for (int j = 0; j < clientDt.Rows.Count; j++)
        {
          dr = hostDt.Rows[j];
          dr[hostDt.Columns.Count - 1] = clientDt.Rows[j][i];
          dr = null;
        }
    }
  }

  return hostDt;
}
You don't have to return the hostDt because the reference type object is passed by reference. However, if your source tables will be disposed after merging, you have to replace the return statement by this:
return hostDt.Copy();

2008/10/19

A generic method to call Stored Procedures

Scenario:
1.You need to call many Stored Procedures (Sproc), but you don't want to create a method for each of them.
2.You have some Sprocs, and each of them has different numbers of input parameters.

Here is what I did:
private DataSet DBConnectionForGettingData(string sprocName, Dictionary<string, string> paramList)
{
  using (SqlConnection conn = new SqlConnection(str_ConnectionString))
  {
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = sprocName;
    cmd.Connection = conn;

    //loop through the dictionary
    foreach (string key in paramList.Keys)
    {
      cmd.Parameters.AddWithValue(key, paramList[key]);
    }

    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    //release the resource
    cmd = null;
    da = null;

    return ds;
  }
}
The paramList is the collection of parameters. All you need to do is create a Dictionary object and add all the parameters to this object. You can also adjust the arguments of this method to make it more flexible.

2008/10/12

Drawing charts on your ASP.Net page by using OWC

Here is the steps that how I draw and put a chart on my ASP.Net page by using Microsoft Office Web Component (OWC):
My environment: VS 2005, MS Office 2003.
  1. Adding the reference. Right click on your web project -> select add a reference -> select "COM" tab -> find and add the "Microsoft Office Web Component 11".
    PS: The OWC version is depending on the version of MS Office you are using. I am using Office 2003 so the OWC version will be 11.
  2. In your code-behind file, add this
    using Microsoft.Office.Interop.Owc11;

2008/10/07

Refresh the ASP.Net Image control (6/17/2009 updated)

A very simple request: Refresh the Image control on your aspx page when you want to.
Scenario:
1.You want to refresh a specific image when a user clicks a button.
2.You want to refresh a specific image when that image has been changed.
3.You want to refresh a specific image periodically.

This won't work:
//strImagePath is a variable which stores the image path and filename
Image1.ImageUrl = strImagePath;

This will:
Image1.Attributes.Add("src", "strImagePath?ts=" + System.DateTime.Ticks);

In order to avoid the browser sending the old/cached image to client, we can change the value of the image's "src" property by giving a timestamp. We can also change the image file name (like image20090617.jpg -> image20090618.jpg -> image20090619.jpg), but this will require more steps and increase the complexity.