using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UpdatePage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack==false)
        {
            Initialize();
        }
    }

    private void Initialize()
    {
        DataSet.UsersDataTable oUserDataTable =
            new DataSet.UsersDataTable();

        DataSetTableAdapters.UsersTableAdapter oUserTableAdapter =
            new DataSetTableAdapters.UsersTableAdapter();

        oUserTableAdapter.FillUserByUserId(oUserDataTable, 1);

        if (oUserDataTable.Count!=1)
        {
            string strErrorMessage =
                "There Is No Such A User For Updating.";
            DisplayErrorMessage(strErrorMessage);
            return;
        }

        DataSet.UsersRow oUserRow=oUserDataTable[0];

        if (oUserRow.IsFullNameNull()==false)
        {
            txtFullName.Text = oUserRow.FullName;
        }

        if (oUserRow.IsDescriptionNull()==false)
        {
            txtDescription.Text = oUserRow.Description;
        }
            
    }

    protected virtual void DisplayErrorMessage(string message)
    {
        divPageMessages.Visible = true;
        litPageMessages.Text =
            string.Format("<div class ='Error'>{0}</div>", message);
    }

    protected virtual void DisplayWarningMessage(string message)
    {
        divPageMessages.Visible = true;
        litPageMessages.Text =
            string.Format("<div class ='Warning'>{0}</div>", message);
    }

    protected virtual void DisplayInformationMessage(string message)
    {
        divPageMessages.Visible = true;
        litPageMessages.Text =
            string.Format("<div class ='Information'>{0}</div>", message);
    }
    protected void btnDone_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
          
            DataSet.UsersDataTable oUsersDataTable =
                new DataSet.UsersDataTable();

            DataSetTableAdapters.UsersTableAdapter oUsersTableAdapter =
                new DataSetTableAdapters.UsersTableAdapter();
           
            oUsersTableAdapter.FillUserByUserId(oUsersDataTable, 1);

          
            if (oUsersDataTable.Count != 1)
            {
                string strErrorMessage =
                    "There is no such a user for updating!";

                DisplayErrorMessage(strErrorMessage);
                return;
            }
         
            DataSet.UsersRow oUsersRow = oUsersDataTable[0];
           
            txtFullName.Text =
                txtFullName.Text.Trim();

            if (txtFullName.Text == string.Empty)
            {
                oUsersRow.SetFullNameNull();
            }
            else
            {
                oUsersRow.FullName = txtFullName.Text;
            }

            txtDescription.Text =
                txtDescription.Text.Trim();

            if (txtDescription.Text == string.Empty)
            {
                oUsersRow.SetDescriptionNull();
            }
            else
            {
                oUsersRow.Description = txtDescription.Text;
            }
         
            oUsersTableAdapter.Update(oUsersDataTable);
         
            string strInformationMessage =
                "Your profile was updated successfully.";

            DisplayInformationMessage(strInformationMessage);
           
        }
    }
  
}