Wednesday, January 17, 2007

Firebird and the boolean data type

We know that Firebid hasn't a boolean data type. But with VB.net this isn't a problem.
Now we'll make an example of how manage a checkbox column in VB.net with Firebird.
First we create a field smallint called for example "P" not null with field default=0 in the table.
Next we create a new boolean column in a datgridview and assign to it the properties of visualization a resizing mode, proceeding in this way:

Dim checkboxColumn As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn
With checkboxColumn
.HeaderText = "P"
.Name = "P"
.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
.FlatStyle = FlatStyle.Standard
.CellTemplate = New DataGridViewCheckBoxCell()
.CellTemplate.Style.BackColor = Color.Beige
.DataPropertyName = "P"
'There we assign the numeric value corresponding to true and false value
.TrueValue = 1
.FalseValue = 0
End With
'There we assign the numeric value corresponding to true and false value 
'Next we use the DataGridView1_CellContentClick event of the datagridview to edit the boolean field:
Private Sub DataGridView1_CellContentClick(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellContentClick
Dim ObjConnection As New FbConnection()
Dim checkboxColumn As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn
ObjConnection.ConnectionString = "servertype=0;username=sysdba;dialect=3;password=masterkey;database=" & My.Application.Info.DirectoryPath & "\Biblioteca.gdb"
ObjConnection.Open()

Try
Dim ObjCommand As New FbCommand()
ObjCommand.Connection = ObjConnection
ObjCommand.CommandType = CommandType.TableDirect
If DataGridView1.CurrentRow.Cells("P").Value = 0 Then

ObjCommand.CommandText = "update Biblioteca set P=1 where counter='" & DataGridView1.CurrentRow.Cells("Counter").Value & "' and P=0"

Else
ObjCommand.CommandText = "update Biblioteca set P=0 where counter='" & DataGridView1.CurrentRow.Cells("Counter").Value & "'"

End If
ObjCommand.ExecuteNonQuery()

Catch ex As FbException
MsgBox(ex.Message)
Finally
ObjConnection.Close()
End Try
End Sub

No comments:

Post a Comment