Опубликован: 30.05.2011 | Доступ: свободный | Студентов: 2366 / 213 | Оценка: 4.12 / 4.41 | Длительность: 12:00:00
Самостоятельная работа 5:

Работа с Windows Azure Table

< Самостоятельная работа 4 || Самостоятельная работа 5: 1234567 || Лекция 13 >

Приложение Д. AzureTable.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Services.Client;
using Microsoft.WindowsAzure;

namespace WebRole1
{
    public partial class AzureTable : System.Web.UI.Page
    {
        private CloudStorageAccount account = null;
        private ContactContext context = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            btn_change.Visible = false;

            account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            context = new ContactContext(account.TableEndpoint, account.Credentials);
            contactGV.DataSource = context.ContactData;
            contactGV.DataBind();

            int i = 0;

            foreach (TableCell cell in contactGV.HeaderRow.Cells)
            {

                if (cell.Text == "PartitionKey") { Session["pkindex"] = i; }
                if (cell.Text == "RowKey") { Session["rkindex"] = i; }
                i++;
            }
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            var statusMessage = String.Empty;
            try
            {

                context.Add(new Contact { PartitionKey = "MyContacts", 
                 RowKey = this.tb_lastname.Text + " " + this.tb_firstname.Text, 
                  FirstName = tb_firstname.Text, LastName = tb_lastname.Text, 
                    TelNumber = tb_telnum.Text, Email = tb_email.Text });
               
            }
            catch (DataServiceRequestException ex)
            {
                statusMessage = "Unable to connect to the table storage server. Please check that the service is running.<br>"
                                 + ex.Message;
            }
            lb_status.Text = statusMessage;
            contactGV.DataBind();
        }

        protected void contactGV_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            GridView g = (GridView)sender;
            try
            {
                Contact c = (from contact in context.CreateQuery<Contact>("Contacts")
                             where contact.PartitionKey == g.Rows[e.RowIndex].
                              Cells[Convert.ToInt32(Session["pkindex"].ToString())].Text
                             && contact.RowKey == g.Rows[e.RowIndex].
                              .Cells[Convert.ToInt32(Session["rkindex"].ToString())].Text
                             select contact).FirstOrDefault();

                context.Delete(c);
            }
            catch(DataServiceRequestException ex) 
            {
                lb_status.Text = ex.Message;
            }
          g.DataBind();

        }

        protected void contactGV_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridView g = (GridView)sender;
            int index = g.SelectedIndex;

            Contact c = (from contact in context.CreateQuery<Contact>("Contacts")
                         where contact.PartitionKey == g.Rows[index].Cells[Convert.ToInt32(Session["pkindex"].ToString())].Text
                         && contact.RowKey == g.Rows[index].Cells[Convert.ToInt32(Session["rkindex"].ToString())].Text
                         select contact).FirstOrDefault();
            tb_firstname.Text = c.FirstName;
            tb_lastname.Text = c.LastName;
            tb_email.Text = c.Email;
            tb_telnum.Text = c.TelNumber;

            Session["index"] = index;

            btn_change.Visible = true;
        }

        protected void btn_change_Click(object sender, EventArgs e)
        {
            
            int index = Convert.ToInt32(Session["index"].ToString());
            try
            {
            Contact c = (from contact in context.CreateQuery<Contact>("Contacts")
                         where contact.PartitionKey == contactGV.Rows[index].
                             Cells[Convert.ToInt32(Session["pkindex"].ToString())].Text
                         && contact.RowKey == contactGV.Rows[index].
                             Cells[Convert.ToInt32(Session["rkindex"].ToString())].Text
                         select contact).FirstOrDefault();

            c.FirstName = tb_firstname.Text;
            c.LastName = tb_lastname.Text;
            c.Email = tb_email.Text;
            c.TelNumber = tb_telnum.Text;

           
                context.Update(c);
                
            }
            catch (DataServiceRequestException a)
            {
                lb_status.Text = a.Message;
            }
            contactGV.DataBind();

        }
    }
}
< Самостоятельная работа 4 || Самостоятельная работа 5: 1234567 || Лекция 13 >
Роза Мальцева
Роза Мальцева
Игнат Гринько
Игнат Гринько

Примерно месяц назад получил на сайте код Дримспарк, сегодня вводил его на сайте Дримспарк, пишет: Недействительный код проверки. Проверьте правильность введенного кода. Код вводил методом: скопировать-вставить.