October 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Working with Binary Large Objects (BLOBs) Using SQL Server and ADO.NET

  • July 28, 2008
  • By Bipin Joshi
  • Send Email »
  • More Articles »

Listing 10: Inserting a photo

protected void DetailsView1_ItemInserting
(object sender, DetailsViewInsertEventArgs e)
{
   Photo p = new Photo();

   TextBox t1=((TextBox)DetailsView1.Rows[0].Cells[1].Controls[0]);
   TextBox t2=((TextBox)DetailsView1.Rows[1].Cells[1].Controls[1]);
   FileUpload fu=
      ((FileUpload)DetailsView1.Rows[2].Cells[1].Controls[1]);

   p.Title = t1.Text;
   p.Description = t2.Text;

   Stream imgdatastream = fu.PostedFile.InputStream;
   int imgdatalen = fu.PostedFile.ContentLength;
   byte[] imgdata = new byte[imgdatalen];
   int n = imgdatastream.Read(imgdata, 0, imgdatalen);

   p.PhotoData = imgdata;

   PhotoHelper.Insert(p);
   BindData();
}

Here, you grab the new values entered in the DetailsView and fill them into a Photo object. See how the photo uploaded by the user is converted into a byte array. The InputStream of the FileUpload control gives you access to the incoming data. You then read this stream into a byte array. Finally, the Insert() method of PhotoHelper class is called to add a record into the Photos table.

The update operation is similar to insert, with a few changes. Listing 11 gives you the complete event handler for the ItemUpdating event.

Listing 11: Updating a photo

protected void DetailsView1_ItemUpdating(object sender,
   DetailsViewUpdateEventArgs e)
{
   Photo p = new Photo();

   TextBox t1 =
      ((TextBox)DetailsView1.Rows[0].Cells[1].Controls[0]);
   TextBox t2 =
      ((TextBox)DetailsView1.Rows[1].Cells[1].Controls[1]);
   FileUpload fu =
      ((FileUpload)DetailsView1.Rows[2].Cells[1].Controls[1]);

   p.PhotoID = Convert.ToInt32(DetailsView1.DataKey[0]);
   p.Title = t1.Text;
   p.Description = t2.Text;

   Stream imgdatastream = fu.PostedFile.InputStream;
   int imgdatalen = fu.PostedFile.ContentLength;
   byte[] imgdata = new byte[imgdatalen];
   int n = imgdatastream.Read(imgdata, 0, imgdatalen);

   p.PhotoData = imgdata;

   PhotoHelper.Update(p);
   BindData();
}

Notice the line marked in bold letters. You get the PhotoID of the row being updated by using the DataKey collection and then calling the Update() method of rthe PhotoHelper class.

The delete operation is simple enough. It is shown in Listing 12:

Listing 12: Deleting a photo

protected void DetailsView1_ItemDeleting(object sender,
   DetailsViewDeleteEventArgs e)
{
   int photoid = Convert.ToInt32(DetailsView1.DataKey[0]);
   PhotoHelper.Delete(photoid);
   BindData();
}

That completes your default web form. You also need to develop ShowPhoto.aspx that supplies the photo image to the image control. Add another web form to your web site and name it ShowPhoto.aspx. This web form doesn't have any controls on it. The Page_Load event handler of ShowPhoto.aspx looks like what you see in Listing 13:

Listing 13: Showing a photo from database in an Image control

protected void Page_Load(object sender, EventArgs e)
{
   int photoid = Convert.ToInt32(Request.QueryString["photoid"]);
   Photo p = PhotoHelper.GetByID(photoid);

   Response.Clear();
   Response.ContentType = "image/pjpeg";
   Response.BinaryWrite(p.PhotoData);
   Response.End();
   }

Here, you retrieve a single photo by using the GetByID() method of the PhotoHelper class. Recollect that ShowPhoto.aspx receives a query string parameter specifying the photo ID to be retrieved. The byte array containing photo data in raw form then is written to the response stream using the BinaryWrite() method of the Response object. Notice that you have cleared the response and set its content type to image/pjpeg before writing the response.

This completes your web site. You now can run the default web form and try adding, updating, and deleting records. Figure 2 shows you a sample run of the web site:

Figure 2: Default web form showing photos from the database





Page 5 of 7



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel