RSS

Monthly Archives: February 2012

Banker’s rounding in .NET

Here is a table to help clarify common rounding methods, including round half away from zero and round half to even, also known as Gaussian rounding or Banker’s rounding:

Numbers Round half away from zero Banker’s rounding
-1.9 -2 -2
-1.6 -2 -2
-1.5 -2 -2
-1.4 -1 -1
-1.1 -1 -1
-0.9 -1 -1
-0.6 -1 -1
-0.5 -1 0
-0.4 0 0
-0.1 0 0
0.1 0 0
0.4 0 0
0.5 1 0
0.6 1 1
0.9 1 1
1.1 1 1
1.4 1 1
1.5 2 2
1.6 2 2
1.9 2 2

Read the rest of this entry »

 
Leave a comment

Posted by on February 23, 2012 in .NET, C#

 

Ora-1036

The Ora-1036 exception pops out frequently when one learning ADO.NET with Oracle database. Here are a couple reasons of the exception which I myself have experienced:

  1. The parameters’ name, type or amount doesn’t match the ones in the CommandText.
  2. When using the same SqlCommand/OraCommand more than once, forget to clear the ParameterCollection in the command.
 
Leave a comment

Posted by on February 14, 2012 in .NET, SQL

 

Check if a DataGridView is empty

Sometimes a DataGridView is used for user input and here is a simple way to check if the DataGridView is empty or not:

Public Function IsDataGridViewEmpty(ByRef dataGridView As DataGridView) As Boolean
    Dim isEmpty As Boolean
    isEmpty = True
    For Each row As DataGridViewRow In dataGridView.Rows
        For Each cell As DataGridViewCell In row.Cells
            If Not String.IsNullOrEmpty(cell.Value) Then
                ' Check if the string only consists of spaces
                If Not String.IsNullOrEmpty(Trim(cell.Value.ToString())) Then
                    isEmpty = False
                    Exit For
                End If
            End If
        Next
    Next
    Return isEmpty
End Function

Please feel free to make any suggestion.

 
4 Comments

Posted by on February 14, 2012 in .NET, VB

 

Tags:

Compare class objects with IEquatable

Last week I got a problem when using List.Contains() method to see if a specific object is in the list. For example:
The class definition:

Public Class Book

    Private _title As String
    Public Property Title() As String
        Get
            Return _title
        End Get
        Set(ByVal value As String)
            _title = value
        End Set
    End Property

    Private _edition As String
    Public Property Edition() As String
        Get
            Return _edition
        End Get
        Set(ByVal value As String)
            _edition = value
        End Set
    End Property

End Class

Read the rest of this entry »

 
Leave a comment

Posted by on February 13, 2012 in .NET, VB

 

Tags:

Auto numbering with simple SQL query

If the field is unique and starts from 0 or 1 in the table, simply do…

SELECT COUNT(ColumnName) FROM TableName

…and we get the row count and the field value.

To start auto numbering from 1 in Visual Basic:

Dim count As Integer = cmd.ExecuteSalar()
If IsDBNull(count) Then
   ' Insert 1
Else
   ' Insert count + 1
End If
 
1 Comment

Posted by on February 13, 2012 in .NET, SQL, VB

 

Tags:

Multiple group fields support of DataSet Helper (Visual Basic)

When applying DataSet Helper to my project, it failed to group multiple fields.
The fix is simple, just comment the line in the InsertGroupByInto method as follows:

If Not SameRow Then
   For J = m_FieldInfo.Count To I + 1 Step -1
      DestRows(J) = DestTable.NewRow()
      RowCount(J) = 0
      'If Rollup = False Then Exit For ' Comment this line for group by multiple fields
   Next J
End If
 
Leave a comment

Posted by on February 13, 2012 in .NET, VB

 

Tags: