ASP.NET Podcast Show #133 - Windows Azure Table Storage - video - More Wally - Wallace B. McClure
in

MoreWally.com

Giving people what they want, More Wally. This is the technical and personal blog site of
Wallace B. (Wally) McClure.

This Blog

Syndication

News

Please goy buy 3-4 copies of my book on MonoTouch titled "Professional iPhone Programming with MonoTouch for .NET/C# Developers." They make great gifts all year round. Plus, I get about $.20 when you buy a copy.

Technical Sites

More Wally - Wallace B. McClure

This blog will have all kinds of posts about Wally McClure. In it, there will be tons of .NET and computer programming posts as well as Wally's views on life in general. As you might guess, this site and blog help you get More Wally in your life. What more could anyone want? iPhone, Android, MonoTouch, MonoDroid, Mobile, HTML5, .NET, ADO.NET, ASP.NET, AJAX, Atlas, Microsoft AJAX Library, ASP.NET AJAX, and Windows Azure............follow me on twitter at Wally

ASP.NET Podcast Show #133 - Windows Azure Table Storage - video

 

Subscribe to All!

Subscribe to WMV.

Subscribe to M4V (iPod).

Subscribe to MP3.

Download WMV.

Download M4V (iPod).

Download MP3.

Win a ticket to the MDC in Detroit.  Enter here. 

Original Url: http://aspnetpodcast.com/CS11/blogs/asp.net_podcast/archive/2008/12/25/asp-net-podcast-show-133-windows-azure-table-storage-video.aspx

Show Notes:

  • CRUD Application
  • CRUD application.
  • Message.
  • Message Data Service Context.
  • This example is taken from Hands On Labs.

Source Code for Default.aspx.cs:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Services.Client;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.Samples.ServiceHosting.StorageClient;
using Microsoft.ServiceHosting.ServiceRuntime;

namespace TableCloudService_WebRole
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string statusMessage = String.Empty;
            if (!Page.IsPostBack)
            {
                DisplayMessages();
            }
        }

        protected void lbDisplayMessages_Click(object sender, EventArgs e)
        {
            DisplayMessages();
        }

        protected void OnDeleteMessage(object sender, CommandEventArgs e)
        {
            LinkButton lb = (LinkButton)sender;
            GridViewRow gvr = (GridViewRow)lb.Parent.Parent;

            if (e.CommandName == "Delete")
            {
                string PartitionKey = ((HiddenField)gvr.FindControl("hdPartitionKey")).Value;
                string RowKey = ((HiddenField)gvr.FindControl("hdRowKey")).Value;
                string TimeStamp = ((HiddenField)gvr.FindControl("hdTimeStamp")).Value;
                string Body = ((TextBox)gvr.FindControl("txtBody")).Text;
                string Name = ((TextBox)gvr.FindControl("txtName")).Text;
                MessageDataServiceContext context = GetContext();

                //var m = (from mess in context.Messages
                //         where
                //             (mess.PartitionKey == PartitionKey) &&
                //             (mess.RowKey == RowKey)
                //         select mess).Single();
                var m = new Message();
                m.PartitionKey = PartitionKey;
                m.RowKey = RowKey;
                m.Timestamp = Convert.ToDateTime(TimeStamp);
                context.AttachTo("Messages", m, "*");
               
                context.DeleteMessage(m);
                RoleManager.WriteToLog("Verbose", "Delete Message.");
                DisplayMessages();
            }
        }
        protected void OnUpdateMessage(Object sender, CommandEventArgs e)
        {
            LinkButton lb = (LinkButton)sender;
            GridViewRow gvr = (GridViewRow)lb.Parent.Parent;

            if (e.CommandName == "Update")
            {
                string PartitionKey = ((HiddenField)gvr.FindControl("hdPartitionKey")).Value;
                string RowKey = ((HiddenField)gvr.FindControl("hdRowKey")).Value; ;
                string TimeStamp = ((HiddenField)gvr.FindControl("hdTimeStamp")).Value; ;
                string Body = ((TextBox)gvr.FindControl("txtBody")).Text; ;
                string Name = ((TextBox)gvr.FindControl("txtName")).Text; ;
                MessageDataServiceContext context = GetContext();

                //var m = (from mess in context.Messages
                //         where
                //             (mess.PartitionKey == PartitionKey) &&
                //             (mess.RowKey == RowKey)
                //         select mess).First();
                var m = new Message();
                m.PartitionKey = PartitionKey;
                m.RowKey = RowKey;
                m.Timestamp = Convert.ToDateTime(TimeStamp);
                m.Body = Body;
                m.Name = Name;
                context.AttachTo("Messages", m, "*");
                context.UpdateMessage(m);
                RoleManager.WriteToLog("Information",  "Update complete.");
                DisplayMessages();
            }
        }

        protected void Timer1_OnTick(Object sender, EventArgs e)
        {
            DisplayMessages();
        }

        private void DisplayMessages()
        {
            MessageDataServiceContext context = GetContext();
            RoleManager.WriteToLog("Verbose", "Display Messages.");
            messageList.DataSource = context.Messages.Take(10);
            messageList.DataBind();
        }

        private MessageDataServiceContext GetContext()
        {
            MessageDataServiceContext context;
            StorageAccountInfo accountInfo = StorageAccountInfo.GetAccountInfoFromConfiguration("TableStorageEndpoint");
            TableStorage.CreateTablesFromModel(typeof(MessageDataServiceContext), accountInfo);
            context = new MessageDataServiceContext(accountInfo);
            RoleManager.WriteToLog("Verbose", "Message Data Service Context created.");
            return context;
        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            StorageAccountInfo accountInfo = StorageAccountInfo.GetAccountInfoFromConfiguration("TableStorageEndpoint");
            MessageDataServiceContext context = new MessageDataServiceContext(accountInfo);
            context.AddMessage(this.nameBox.Text, this.messageBox.Text);
            nameBox.Text = String.Empty;
            messageBox.Text = String.Empty;
            DisplayMessages();
        }
    }
}

Source Code for Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TableCloudService_WebRole._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Azure Table Example Page</title>
        <style type="text/css">
        body { font-family: Verdana; font-size: 9pt; }
        h1 { font-size: 12pt; color: #555555; }
        li { list-style-type: none; }
        form { background-color: #eeeeff; width: 80%; margin: 0 auto; padding: 1em; border: solid 1px #333333; }
        #entryform, #messages { margin: 1em 0 }
        #entryform li span { float: left; width: 15%; color:#333333; margin-top:0.25em; }
        #entryform input[type="text"] { border: solid 1px #999999; }
        #messages { border: solid 1px #999999; }
        #messages li { padding: 0.5em; }
        .error { color: #ff0000; }
        .even { background-color: #ccccff; }
        .odd { background-color: #ffffff; font-style: italic; }       
        .messageBox { width: 73%; }
    </style>

</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="sm" runat="server" />
    <div>
        <h1>Windows Azure Chat</h1>
        <ul id="entryform">
            <li><span>Your name</span><asp:TextBox ID="nameBox" runat="server" Text="Anonymous" /></li>
            <li><span>Message</span><asp:TextBox ID="messageBox" runat="server" CssClass="messageBox" /> <asp:Button ID="submitButton" runat="server" Text="Submit" OnClick="SubmitButton_Click" /></li>
            <li><span><asp:LinkButton ID="lbDisplayMessages" runat="server" OnClick="lbDisplayMessages_Click" Text="Update Messages" /></span></li>
        </ul>
                <div>
                    <asp:Label ID="status" runat="server" CssClass="error" />
                </div>
                <asp:GridView ID="messageList" runat="server" AutoGenerateColumns="false">
                    <AlternatingRowStyle CssClass="even" />
                    <RowStyle CssClass="odd" />
                    <Columns>
                    <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:HiddenField ID="hdPartitionKey" runat="server" Value='<%#Eval("PartitionKey") %>' />
                        <asp:HiddenField ID="hdRowKey" runat="server" Value='<%#Eval("RowKey") %>' />
                        <asp:HiddenField ID="hdTimeStamp" runat="server" Value='<%#Eval("TimeStamp") %>' />
                        <asp:HiddenField ID="hdName" runat="server" Value='<%#Eval("Name") %>' />
                        <asp:HiddenField ID="hdBody" runat="server" Value='<%#Eval("Body") %>' />
                        <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>' />
                        said:
                    </ItemTemplate>
                    </asp:TemplateField>                   
                    <asp:TemplateField HeaderText="Message">
                    <ItemTemplate>
                        <asp:TextBox ID="txtBody" runat="server" Text='<%# Eval("Body") %>' />
                    </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Operations">
                    <ItemTemplate>
                        <asp:LinkButton ID="updateMessage"
                            CommandName="Update"
                            runat="server" Text="Update" OnCommand="OnUpdateMessage" />
                        <asp:LinkButton ID="deleteMessage"
                            OnClientClick="return confirm('Delete Message?');"
                            CommandName="Delete"
                            runat="server" Text="Delete" OnCommand="OnDeleteMessage" />               
                    </ItemTemplate>
                    </asp:TemplateField>
                    </Columns>
                </asp:GridView>
    </div>
    </form>
</body>
</html>

Source for Message.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Samples.ServiceHosting.StorageClient;

namespace TableCloudService_WebRole
{
    public class Message : TableStorageEntity
    {
        //private string PartitionKey;
        //private string RowKey;
        public string Name { get; set; }
        public string Body { get; set; }

        public Message()
        {
            PartitionKey = "a";
            RowKey = String.Format("{0:10},{1}", DateTime.MaxValue.Ticks - DateTime.Now.Ticks,
                Guid.NewGuid().ToString());
        }
    }
}

Source for MessageDataServiceContext.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Samples.ServiceHosting.StorageClient;

namespace TableCloudService_WebRole
{
    public class MessageDataServiceContext : TableStorageDataServiceContext
    {
        public MessageDataServiceContext(StorageAccountInfo accountInfo) : base(accountInfo)
        {
        }

        public IQueryable<Message> Messages
        {
            get
            {
                return this.CreateQuery<Message>("Messages");
            }
        }

        public void AddMessage(string name, string body)
        {
            Message MessageEntity = new Message { Name = name, Body = body };
            this.AddObject("Messages", MessageEntity);
            this.SaveChanges();
        }

        public void DeleteMessage(Object MessageEntity)
        {
            this.DeleteObject(MessageEntity);
            this.SaveChanges();
        }

        public void UpdateMessage(Object MessageEntity)
        {
            this.UpdateObject(MessageEntity);
            this.SaveChanges();
        }
    }
}

Comments

No Comments
2006 - Wallace B. McClure
Powered by Community Server (Non-Commercial Edition), by Telligent Systems