Monday, January 19, 2009

Read a single value from a query with VB.Net

Sometimes it's necessary to read single value from a table (or a query). Here's the code:

Imports System.Data.Oledb


Dim ConnString As String = "connection string"
' open connection
Dim Conn As New Data.OleDb.OleDbConnection(ConnString)
Cn.Open()

' request execution
Dim SQL As String = "SELECT Data FROM Table WHERE ID = 'aaaa';"
Dim command As New Data.OleDb.OleDbCommand(Sql, Conn)
Dim ID As Integer = command.ExecuteScalar()

'close connection
Cn.Close()

Sunday, January 18, 2009

Image to byte array conversion

It's very easy to convert an image into a byte array, with VB.NET; using MemoryStream object:

Private Function ConvertImageToByteArray(ByVal inputImage As Image) As Byte()
    Dim ms As New System.IO.MemoryStream
    inputImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
    Return ms.ToArray
End Function


To re-convert the byte array into image, we can use ImageConverter class:

Private Function ConvertByteArrayToBitmap(ByVal array As Byte()) As Bitmap
    Dim ic As New ImageConverter
    Dim img As Image = CType(ic.ConvertFrom(array), Image)
    Dim bmp As New Bitmap(img)
    Return bmp
End Function

Friday, January 16, 2009

Email address validation with Vb.net and Regula Expression

Imports System.Text.RegularExpressions


Dim ValidateMail As String = "^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-z]{2,4}|[0-9]{1,3})(\]?)$"

Dim EmailAddress As String = "$mymail@domain.com$"

If Regex.IsMatch(EmailAddress, ValidateMail) Then
...
Else
...
End If

Thursday, January 15, 2009

Mounting CdRom on SCO/Unix

After the creation of the folder as mount point with mkdir    /mnt/cdrom, we have to find the "phisycal" name of cdrom unit. To do it, go to device folder with

cd   /dev/cdrom

and then type

ls

to view the name. For this example, assume c0b0t6l0 as the device name

So, the mount command is:

mount   -r   -F   cdfs   /dev/cdrom/c0b0t6l0   /mnt/cdrom