Quick Objects Documentation Akal Tech Logo
Work With Blob Field To Store Image Data

Glossary Item Box

Business Logic Framework - Tutorial 31:

Business Logic Framework makes it very simple to deal with blob/binary data types. A blob field in the database gets mapped to BinaryField in the Business Logic Framework. When an object with a BinaryField is loaded, the BinaryField's value is automatically filled with the binary array from the underlying record in the database. To save the BinaryField value, simply set the binary array and update the business object just like you would in normal update scenario.

 Even though samples are available both for Web and Windows, the code in this tutorial refers to the windows version. The main difference between the two is that the ASP.NET sample uses a HTTP Handler to serve the image.

 

1 Private   Sub Button_LoadCustomer_Click( ByVal  sender  As ObjectByVal e  As EventArgs) 
2     If Not [String].IsNullOrEmpty( Me.TextBox_CustomerID.Text)  Then 
3          If Me .Customer  Is Nothing Then 
4              Me.Customer =  New Customers() 
5          End If 
6          ' We can use the Parse method of the CustomerID field to parse the value from the TextBox  
7          Me.Customer.CustomerID.Parse( Me.TextBox_CustomerID.Text) 
8          ' Calling the Load method will Load all the fields of the Customer object.   
9          ' By default all fields will be loaded but if you can control what fields are loaded by setting their Visible property value.  
10          If Me .Customer.Load()  Then 
11              Me.Button_Save.Enabled =  True 
12              ' Lets assign the Customer's FirstName and LastName values to the text boxes.  
13              Me.TextBox_FirstName.Text =  Me.Customer.FirstName.Value 
14              Me.TextBox_LastName.Text =  Me.Customer.LastName.Value 
15              ' Every type of field object has a property called IsNull so it makes it relatively very simple  
16              ' to check if the field value is null or not. This provides a consistent way of dealing with Null values in the database.  
17              If Not  Me.Customer.Photo.IsNull  Then 
18                  ' Since the Photo is of type BinaryField and it contains a Byte array.  
19                  ' The Byte array can be translated into an Image Bitmap object as shown below.  
20                  Me.PictureBox_Photo.Image =  New Bitmap( New MemoryStream( Me.Customer.Photo.Value)) 
21              Else 
22                  ' Since there is no image in the database we set the picture box's Image property to null.  
23                  Me.PictureBox_Photo.Image =  Nothing  
24              End If 
25          End If 
26     End If 
27 End  Sub 
28  
1 private   void Button_LoadCustomer_Click( object  sender, EventArgs e) 
2
3     if (!String.IsNullOrEmpty( this.TextBox_CustomerID.Text)) 
4     { 
5          if (this.Customer ==  null
6         { 
7              this.Customer =  new Customers(); 
8         } 
9          // We can use the Parse method of the CustomerID field to parse the value from the TextBox  
10          this.Customer.CustomerID.Parse( this.TextBox_CustomerID.Text); 
11          // Calling the Load method will Load all the fields of the Customer object.   
12          // By default all fields will be loaded but if you can control what fields are loaded by setting their Visible property value.  
13          if (this.Customer.Load()) 
14         { 
15              this.Button_Save.Enabled =  true
16              // Lets assign the Customer's FirstName and LastName values to the text boxes.  
17              this.TextBox_FirstName.Text =  this.Customer.FirstName.Value; 
18              this.TextBox_LastName.Text =  this.Customer.LastName.Value; 
19              // Every type of field object has a property called IsNull so it makes it relatively very simple  
20              // to check if the field value is null or not. This provides a consistent way of dealing with Null values in the database.  
21              if (!this.Customer.Photo.IsNull) 
22             { 
23                  // Since the Photo is of type BinaryField and it contains a Byte array.  
24                  // The Byte array can be translated into an Image Bitmap object as shown below.  
25                  this.PictureBox_Photo.Image =  new Bitmap( new MemoryStream( this.Customer.Photo.Value)); 
26             } 
27              else 
28             { 
29                  // Since there is no image in the database we set the picture box's Image property to null.  
30                  this.PictureBox_Photo.Image =  null
31             } 
32         } 
33     } 
34
35  

1 private  void Button_Save_Click( object  sender, EventArgs e) 
2
3     if (this.Customer !=  null &&  this.Customer.IsLoaded) 
4     { 
5          // Assign the ObjectMode to Save and this will ensure that any field values that are assigned  
6          // from this point onwards, their UseInSave will be set to True.  
7          this.Customer.ObjectMode = Akal.QuickObjects.ObjectBase.ObjectModes.Save; 
8          this.Customer.FirstName.Value =  this.TextBox_FirstName.Text; 
9          this.Customer.LastName.Value =  this.TextBox_LastName.Text; 
10          // Lets check to see if the User actually supplied an image or not.  
11          // Or may be the user wants to remove the photo.  
12          if (this.CheckBox_Remove.Checked ||  this.PictureBox_Photo.Image ==  null
13         { 
14              // Since the user did not supply an image, we can set the Photo to null.  
15              this.Customer.Photo.IsNull =  true
16         } 
17          else 
18         { 
19              // We can convert the Image to a Byte array as shown below.  
20             MemoryStream stream =  new MemoryStream(); 
21              this.PictureBox_Photo.Image.Save(stream,  this.PictureBox_Photo.Image.RawFormat); 
22              // We can simply assign an array of bytes to the BinaryField's Value property.  
23              this.Customer.Photo.Value = stream.ToArray(); 
24         } 
25          // Calling the Update method will update the underlying Customer record.  
26          // The binary fields, large text, or Xml Fields are automatically handled as well.  
27          if (this.Customer.Update()) 
28         { 
29             MessageBox.Show( "Saved!" ); 
30         } 
31          else 
32         { 
33             MessageBox.Show( this.Customer.ErrorString); 
34         } 
35     } 
36     else 
37     { 
38          this.Button_Save.Enabled =  false
39     } 
40
41  
1 Private  Sub Button_Save_Click( ByVal  sender As ObjectByVal e  As EventArgs) 
2     If Me.Customer IsNot  Nothing  AndAlso Me.Customer.IsLoaded  Then 
3          ' Assign the ObjectMode to Save and this will ensure that any field values that are assigned  
4          ' from this point onwards, their UseInSave will be set to True.  
5          Me.Customer.ObjectMode = Akal.QuickObjects.ObjectBase.ObjectModes.Save 
6          Me.Customer.FirstName.Value =  Me.TextBox_FirstName.Text 
7          Me.Customer.LastName.Value =  Me.TextBox_LastName.Text 
8          ' Lets check to see if the User actually supplied an image or not.  
9          ' Or may be the user wants to remove the photo.  
10          If Me .CheckBox_Remove.Checked  OrElse Me.PictureBox_Photo.Image  Is Nothing  Then 
11              ' Since the user did not supply an image, we can set the Photo to null.  
12              Me.Customer.Photo.IsNull =  True 
13          Else 
14              ' We can convert the Image to a Byte array as shown below.  
15              Dim stream  As New MemoryStream() 
16              Me.PictureBox_Photo.Image.Save(stream,  Me.PictureBox_Photo.Image.RawFormat) 
17              ' We can simply assign an array of bytes to the BinaryField's Value property.  
18              Me.Customer.Photo.Value = stream.ToArray() 
19          End If 
20          ' Calling the Update method will update the underlying Customer record.  
21          ' The binary fields, large text, or Xml Fields are automatically handled as well.  
22          If Me .Customer.Update()  Then 
23             MessageBox.Show( "Saved!"
24          Else 
25             MessageBox.Show( Me.Customer.ErrorString) 
26          End If 
27     Else 
28          Me.Button_Save.Enabled =  False  
29     End If 
30 End Sub 
31