Sunday, November 14, 2010

Sorting dataview on multiple columns

Sorting multiple columns in the dataview we just need to separate the columns with a comma. To define the sort direction we can use ASC or DESC after the column name. Here is an example on how to sort a dataview in multiple columns
// Get the DefaultViewManager of a DataTable.
DataView view = DataTable1.DefaultView;
// By default, the first column sorted ascending.
view.Sort = "LastName ASC, FirstName ASC, Age DESC";

Monday, November 8, 2010

Implementing Facebook Share

Render the Facebook Share button in an anchor (<a>) tag that takes the following attributes:


1. name: You must specify fb_name for this attribute.
2. type: The type of Share button or link to render, with or without a count included. Specify one of box_count, button_count, button, icon_link, or icon. The box_count and button_count options displays a count of the total number of times the page was shared on Facebook, how many comments were added to the story shared on Facebook, and how many times friends Liked the shared story.
3. If you don't specify a type, then this attribute defaults to button_count.
4. share_url: The URL for the page you want to share. If you don't specify a share_url, then this attribute defaults to the current page.


To put a Facebook Share button on your site right now, copy and paste the following code into the page you want to share:

<a name="fb_share"></a>
<script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>


Friday, November 5, 2010

MS SQL column properties of a table

SELECT * FROM sys.columns WHERE OBJECT_NAME(OBJECT_ID)=TableName

Example: SELECT * FROM sys.columns WHERE OBJECT_NAME(OBJECT_ID)='Users'

If you want to check if the field name's datatype,max_length,precision,is_nullable you can use the ff: query above and you can see the result below


Monday, November 1, 2010

PostgreSQL paging

Table script
CREATE TABLE tpe_header
(
  id uuid NOT NULL,
  "year" integer NOT NULL DEFAULT 0,
  season_id uuid,
  refnumber character varying(12) NOT NULL DEFAULT ''::character varying,
  product_type_id uuid,
  gender_id uuid,
  createdby uuid,
  createddate date,
  outsoletype_id uuid,
  upper_id uuid,
  construction_id uuid,
  other_text character varying(160),
  end_client_code uuid,
  deadline_date date,
  CONSTRAINT tpe_header_id PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE tpe_header OWNER TO postgres;
PostgreSql Syntax
select * from tpe_header limit @page_size offset @page_index

with example value : 
select * from tpe_header limit 5 offset 1
Output:




ASP.NET server side paging

Front End
private void RefreshItems()
        {
            int item_count = 0;
            grdList.DataSource = _itemInvoice.GetInvoiceByFilter(branch_code, txtfRefNo.Text, Convert.ToDateTime(txtfRefDate.Text), txtfRemarks.Text, out item_count, curr_pageNumber, page_size, "RefDate", "ASC");
            grdList.DataBind();

            total_page = (int)Math.Ceiling(Convert.ToDecimal(item_count / page_size));

            if (total_page == 0) total_page = 1;

            lbPage.Text = "Page " + curr_pageNumber.ToString() + " of " + total_page.ToString();
            lblItems.Text = "(" + item_count.ToString() + " items)";

        }
Database Access Layer
internal Invoice[] GetInvoiceByFilter(string branchcode, string refno, DateTime refdate, string remarks, out int pageCount, int pageNumber, int pageSize, string sortColumn, string sortOrder)
        {
            base.com.CommandText = "dbo.spInvoiceWithFilter";
            base.com.Parameters.AddWithValue("@branchcode", branchcode);
            base.com.Parameters.AddWithValue("@refno", refno);
            base.com.Parameters.AddWithValue("@refdate", refdate);
            base.com.Parameters.AddWithValue("@remarks", remarks);
            base.com.Parameters.AddWithValue("@pageNumber", pageNumber);
            base.com.Parameters.AddWithValue("@pageSize", pageSize);
            base.com.Parameters.AddWithValue("@sortColumn", sortColumn);
            base.com.Parameters.AddWithValue("@sortOrder", sortOrder);

            SqlParameter param_out = new SqlParameter("@pageCount", SqlDbType.Int, 4);
            param_out.Direction = ParameterDirection.Output;
            base.com.Parameters.Add(param_out);

            _reader = base.GetDataReader();


            List list = new List();
            while (_reader.Read())
            {
                ItemSalesInvoice _is = new ItemSalesInvoice();
                _is.refno = _reader.GetString(1);
                _is.refdate = _reader.GetDateTime(2);
                _is.remarks = _reader.GetString(3);
                _is.RecID = _reader.GetGuid(4);
                list.Add(_is);
            }
            _reader.Close();
            int.TryParse(param_out.Value.ToString(), out pageCount);



            return list.ToArray();
        }
MS SQL stored procedure

CREATE Procedure dbo.[spInvoiceWithFilter] 
(
 @branchcode varchar(2),
 @RefNo varchar(11),
 @RefDate datetime,
 @Remarks varchar(90),
 @pageCount int output,
 @pageNumber int,
 @pageSize int,
 @sortColumn nvarchar(50),
 @sortOrder nvarchar(4)
)
AS 
BEGIN
 
 SET NOCOUNT ON; 

 DECLARE @Sorting NVARCHAR(55),
  @CharBranchCode NVARCHAR(2),
  @CharRefNo NVARCHAR(11),
  @CharRemarks NVARCHAR(50),
  @CharRecordFrom NVARCHAR(18),
  @CharRecordTo NVARCHAR(18),
  @Query NVARCHAR(4000),
  @fullRefNo VARCHAR(20);

 SET @fullRefNo = @branchcode +'-'+ @RefNo;
 
 SELECT @Sorting = @sortColumn +' '+ @sortOrder,
     @CharBranchCode = CAST(@branchcode AS NVARCHAR(2)),
     @CharRefNo =@CharBranchCode +'-'+ CAST(@RefNo AS NVARCHAR(11)),
     @CharRemarks = CAST(@Remarks AS NVARCHAR(50)),
     @CharRecordFrom = CAST(((@pageSize * (@pageNumber-1)) + 1) AS NVARCHAR(18)),
     @CharRecordTo = CAST((@pageSize * @pageNumber) AS NVARCHAR(18));

 SET @pageCount = (SELECT COUNT(RecID)
      FROM Invoice 
      WHERE (BranchCode =@branchcode)
      AND (RefNo LIKE @fullRefNo +'%')
      AND (Remarks LIKE @Remarks +'%'));

 SET NOCOUNT ON;

 SET @Query = 'SELECT * FROM 
 (SELECT ROW_NUMBER() OVER (ORDER BY '+ @Sorting +') AS RowNum,
    Substring(RefNo,4,11) AS RefNo,
    RefDate,
    Remarks,
    RecID
   FROM Invoice
  WHERE (BranchCode='''+  @CharBranchCode +''')
  AND (RefNo LIKE '''+ replace(@CharRefNo,'''','''''')+ '%'')
  AND (Remarks LIKE '''+ replace(@CharRemarks,'''','''''')+ '%''))';

 EXEC(@Query + 'AS tbl WHERE (RowNum BETWEEN ' + @CharRecordFrom + ' AND ' + @CharRecordTo + ') OR ' + @CharRecordTo + ' = 0');
END