Thursday, 23 April 2009

rounding errors between SQL and .NET - why helps to use decimal type instead of float

I came across a situation where when the database datatype is float, and we read the data into a .NET datatable, the automatic conversion is from float to double. For some reason when this happens there are occasionally cases of rounding errors. The way to overcome this (other than changing the float columns to decimal(18, 6) or some other decimal spec, is to access the float data using some CONVERT(decimal(18, 6) col_name_here) so that the .NET data table has decimal type in the column in question.

Tuesday, 21 April 2009

cursor to fetch database views


   1:  declare @fetchStatus as int;
   2:  set @fetchStatus = 0
   3:  declare @str as varchar(100), @views as varchar(100)
   4:  declare tmp cursor for
   5:  select    [name] from    sys.views
   6:  open tmp
   7:   
   8:  while @fetchStatus = 0 begin   
   9:  fetch tmp into @views   
  10:  set @fetchStatus = @@fetch_status   
  11:  if @fetchStatus = 0 begin       
  12:  print @views       
  13:  set @str = 'select * from ' + @views + ' where 0 = 1'       
  14:  exec (@str)   
  15:  end
  16:  end
  17:   
  18:  close tmp
  19:  deallocate tmp

Monday, 16 March 2009

Implement datetime (and other) sorting in ASP.NET gridview

We need some code to load data into the table, then we use the following functions (also add the gridView_Sorting function to handle the OnSorting event of the grid, in the gridview definition in the .aspx file (OnSorting="gridView_Sorting"), same for OnPageIndexChanging event: OnPageIndexChanging="gridView_PageIndexChanging").


   1:  static SortDirection lastSortDirection = SortDirection.Ascending;
   2:  static DataTable roadmapTable;
   3:   
   4:  static string ConvertSortDirectionToSql(SortDirection sortDirection)
   5:  {
   6:      string m_SortDirection = String.Empty;
   7:   
   8:      switch (sortDirection)
   9:      {
  10:      case SortDirection.Ascending:
  11:          m_SortDirection = "ASC";
  12:          break;
  13:   
  14:      case SortDirection.Descending:
  15:          m_SortDirection = "DESC";
  16:          break;
  17:      }
  18:   
  19:      return m_SortDirection;
  20:  }
  21:   
  22:  protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
  23:  {
  24:      roadmapGridView.PageIndex = e.NewPageIndex;
  25:      roadmapGridView.DataBind();
  26:  }
  27:   
  28:  protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
  29:  {
  30:      if (roadmapTable == null)
  31:      {
  32:          return;
  33:      }
  34:   
  35:      lastSortDirection = lastSortDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
  36:   
  37:      DataView m_DataView = new DataView(roadmapTable);
  38:      m_DataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(lastSortDirection);
  39:      roadmapGridView.DataSource = m_DataView;
  40:      roadmapGridView.DataBind();
  41:  }