Click here to Skip to main content
15,884,353 members
Articles / Web Development / ASP.NET

Azure Blob and Entity Table Integration, Extending the Thumbnail Sample

Rate me:
Please Sign up or sign in to vote.
4.54/5 (5 votes)
19 May 2010CPOL7 min read 31.8K   330   11  
This article describes the concepts for doing CRUD (Create, Read, Update, Delete) operations on Windows Azure Tables and how table data can interact with blobs.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Configuration;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.ServiceRuntime;


namespace Thumbnails_WebRole
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        BlobClasses blobclass1 = new BlobClasses();
        PhotoTableDataSource PTDS = new PhotoTableDataSource();
       

        protected void submitButton_Click(object sender, EventArgs e)
        {
            if (upload.HasFile)
            {
                DateTime DTN = DateTime.Now;
                long DTNTicks  = DateTime.Now.Ticks;
                var name = string.Format("{0:10}_{1}", DTNTicks, Guid.NewGuid());
                blobclass1.GetPhotoGalleryContainer().GetBlockBlobReference(name).UploadFromStream(upload.FileContent);
                blobclass1.GetThumbnailMakerQueue().AddMessage(new CloudQueueMessage(System.Text.Encoding.UTF8.GetBytes(name)));
                System.Diagnostics.Trace.WriteLine(String.Format("Enqueued '{0}'", name));
                AddPhotoTableRow(name,DTN);
            }
            //Delay to Make sure the picture has been stored in the blob before the Photos are updated
            System.Threading.Thread.Sleep(1000);
            Display_Thumbnails();
            
        }
     

        private void AddPhotoTableRow(string blobname, DateTime DTN)
        {
            PhotoTableDataModel PTDM = new PhotoTableDataModel();
            PTDM.PartitionKey = "Album1";
            PTDM.RowKey = blobname;
           
            if (TextBoxName.Text!="")
            {
                PTDM.PhotoName = TextBoxName.Text;
            }
            else
            {
                PTDM.PhotoName = "Name";
            }
            if (TextBoxComment.Text != "")
            {
                PTDM.PhotoComment = TextBoxComment.Text;
            }
            else
            {
                PTDM.PhotoComment = "Comment";
            }
            if (TextBoxDate.Text != "")
            {
                PTDM.PhotoTakenDate = Convert.ToDateTime(TextBoxDate.Text);
            }
            else
            {
                PTDM.PhotoTakenDate = DTN;
            }
           
            PTDM.PhotoUploadDate = DTN;
            PTDS.Insert(PTDM);
        
        }

     
        protected void Page_PreRender(object sender, EventArgs e)
        {
            try
            {
               Display_Thumbnails();
            }
            catch (Exception)
            {
            }
        }

        protected void DisplayData_Button_Click(object sender, EventArgs e)
        {
            //Not necessary, because the photos panel is updated by user actions, but left it here for debugging purposes. 
            Display_Thumbnails();
        }
         private void Display_Thumbnails()
         {
             var account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
             var context = new PhotoTableDataServiceContext(account.TableEndpoint.ToString(), account.Credentials);
             List<PhotoTableDataModelwithUrl> PTDMUrl = new List<PhotoTableDataModelwithUrl>();
             var bloblist = from o in blobclass1.GetPhotoGalleryContainer().GetDirectoryReference("thumbnails").ListBlobs()
                            select new { Url = o.Uri };
             foreach (PhotoTableDataModel item in context.PhotoTableData)
             {
                 PhotoTableDataModelwithUrl PTDMUrlItem = new PhotoTableDataModelwithUrl();
                 PTDMUrlItem.PartitionKey = item.PartitionKey ;
                 PTDMUrlItem.RowKey = item.RowKey;
                 PTDMUrlItem.PhotoName = item.PhotoName;
                 PTDMUrlItem.PhotoTakenDate =  item.PhotoTakenDate;
                 PTDMUrlItem.PhotoUploadDate = item.PhotoUploadDate;
                 PTDMUrlItem.PhotoComment = item.PhotoComment;
                 
                 foreach (var s in bloblist)
                 {
                     if (s.Url.ToString().Contains(PTDMUrlItem.RowKey))
                     {
                         PTDMUrlItem.Url = s.Url.ToString();
                     }
                 }
                 PTDMUrl.Add(PTDMUrlItem);
                 
             }
             thumbnails.DataSource = PTDMUrl;
             thumbnails.DataBind();
             up1.Update();
         }
         protected void Delete_Data_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e)
         {
             var blobname = e.CommandArgument.ToString();
             var blobcontainer = blobclass1.GetPhotoGalleryContainer();
             var blob = blobcontainer.GetBlobReference(blobname);
             blob.DeleteIfExists();
             var blobt = blobcontainer.GetBlobReference("thumbnails/" + blobname);
             blobt.DeleteIfExists();
             PhotoTableDataModel PTDM = (from PTDM1 in PTDS.Select()
                                         where PTDM1.RowKey == e.CommandArgument.ToString()
                                         select PTDM1).FirstOrDefault();
        
             PTDS.Delete(PTDM);
             Display_Thumbnails(); 
         }
         protected void Select_Image_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e)
         {
             
             var blobname = e.CommandArgument.ToString();
             var blobcontainer = blobclass1.GetPhotoGalleryContainer().GetBlobReference("photogallery");
             var blob = blobcontainer.Container.GetBlobReference(blobname);
             SelectedImage.ImageUrl = blob.Uri.ToString();
             UpdateSelectedImagePanel1.Update();
             Display_Thumbnails();
         }

         protected void Update_Data_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e)
         {
             PhotoTableDataModel PTDM = (from PTDM1 in PTDS.Select()
                                         where PTDM1.RowKey == e.CommandArgument.ToString()
                                         select PTDM1).FirstOrDefault();

             TextBoxNameEdit.Text = PTDM.PhotoName;
             TextBoxDateEdit.Text = PTDM.PhotoTakenDate.ToShortDateString();
             TextBoxCommentEdit.Text = PTDM.PhotoComment;
             TextBoxRowKeyEdit.Text = PTDM.RowKey;
             UpdateEditPanel1.Update();
         }

         protected void SubmitChangesButton_Click(object sender, EventArgs e)
         {
           
             PhotoTableDataModel PTDM = (from PTDM1 in PTDS.Select()
                                         where PTDM1.RowKey ==  TextBoxRowKeyEdit.Text
                                         select PTDM1).FirstOrDefault();
           
             PTDM.PhotoComment = TextBoxCommentEdit.Text;
             if (TextBoxDateEdit.Text != "")
             {
                 PTDM.PhotoTakenDate = Convert.ToDateTime(TextBoxDateEdit.Text);
             }
                    
             PTDM.PhotoName = TextBoxNameEdit.Text;
             PTDS.Update(PTDM);
             Display_Thumbnails();

         }


        
    }
   
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Vijay Kumar: Architect, Programmer with expertise and interest in Azure, .net, Silverlight, C#, WCF, MVC, databases and mobile development. Concentrating on Windows Phone 7 and Windows Azure development. Lived in California for many years and done many exciting projects in dotnet and Windows platforms. Moved to Raleigh (RTP), North Carolina recently and available for consulting.  Blog http://Silverazure.blogspot.com.

Comments and Discussions