Wednesday, June 25, 2008

GridView ObjectDataSource Paging

Took me a lot of time to figure it out...

First, paging doesn't seem to work when ObjectDataSource Select function returns SqlDataReader so it returns DataView now.

The code looks like this:

   1: [DataObjectMethod(DataObjectMethodType.Select)]
   2:     public static DataView GetRows(int statusid, int experienceid, string submitter, string idsid, string SortExpression)
   3:     {
   4:  
   5:         StringBuilder strSql = MainSelectSql(idsid, statusid, experienceid, submitter, SortExpression);
   6:  
   7:         SqlConnection connection = Connection.GetConnection();
   8:         SqlCommand command = new SqlCommand(strSql.ToString(), connection);
   9:         command.CommandType = CommandType.Text;
  10:         SqlDataAdapter adapter = new SqlDataAdapter(command);
  11:  
  12:         //connection.Open();
  13:         DataTable dt = new DataTable();
  14:         adapter.Fill(dt);
  15:         connection.Close();
  16:         int totalRecords = dt.Rows.Count;
  17:  
  18:         DataView dw = new DataView(dt);
  19:  
  20:         return dw;
  21:     }

 

The GridView has to have its "AllowPaging" set to true and PageIndexChanging method looks like this:

   1: protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
   2: {
   3:     GridView gv = (GridView)sender;
   4:     gv.PageIndex = e.NewPageIndex;
   5:     gv.DataBind();
   6: }

 

Also can change the page size in properties...

Saturday, June 21, 2008

SQL Server 2005- OVER() partition

This will be useful for GridView pagination:

An aggregate functions can be added to any SELECT (even without a GROUP BY clause) by specifying an OVER() partition for each function.

This means that this code:

   1: select 
   2:     o.customerID, o.productID, o.orderDate, o.orderAmount, t.Total
   3: from 
   4:     Orders o
   5: inner join 
   6:    (
   7:     select customerID, sum(orderAmount) as Total from Orders group by customerID
   8:    ) 
   9:   t on t.customerID = o.customerID
can be replaced with:
   1: select customerID,  productID, orderDate, orderAmount, 
   2:       sum(orderAmount) OVER (Partition by CustomerID) as Total
   3: from Orders

Source

Monday, June 9, 2008

jQuery Selectors - How to Get Anything You Want

Great articles from Learning  jQuery blog:

How to Get Anything You Want - part 1

How to Get Anything You Want - part 2

Firefox popup blocker blocks ctrl-V from Google Reader

Found the solution on Google Groups (Google Reader Help):

1. In a Firefox window, enter "about:config" in the address bar.
2. In the "Filter" field, type "popup_maximum".
3. One preference, called "popup_maximum" appears; double-click the
"value" number.
4. Enter a new, appropriately large value. I used 2000, just to be on
the safe side.

Sunday, June 1, 2008

SQL - substring and charindex

 

   1: SELECT
   2:     substring(IdeaText,1,
   3:         charindex('//', IdeaText)
   4:     ) as part1
   5: FROM tblIdeas

 

substring

charindex