using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using eInfoDesigns.dbProvider.MySqlClient;

namespace typesproject
{
	/// <summary>
	/// Summary description for register.
	/// </summary>
	public class register : System.Web.UI.Page
	{
		protected System.Data.DataSet ds = new DataSet();
		protected System.Web.UI.WebControls.DropDownList lstHeroes;
		protected System.Web.UI.WebControls.TextBox txtJob;
		protected System.Web.UI.WebControls.TextBox txtRanking;
		protected System.Web.UI.WebControls.Button btnPrevious;
		protected System.Web.UI.WebControls.Button btnNext;
		protected System.Web.UI.WebControls.Button btnUpdate;
		protected System.Web.UI.WebControls.HyperLink lnkMainMenu;
		protected System.Web.UI.WebControls.Label lblRanking;
		protected System.Web.UI.WebControls.Label lblSelect;
		protected System.Web.UI.WebControls.Label lblSelectJob;
		protected System.Web.UI.WebControls.Label lblTitle;
		protected System.Web.UI.WebControls.Label lblHelp;
		protected const string ConnectionString = "server=localhost;uid=;pwd=;database=csharpvqs;";

		void RefreshJob()
		{
			int index = (int)ViewState["index"];
			System.Data.DataRow row =
				ds.Tables[0].Rows[index];
			txtJob.Text = (string)row["Job"];
			txtRanking.Text =
				(row["Ranking"] is DBNull) ? "0" : row["Ranking"].ToString() ;
		}

		void SelectRecords()
		{
			string name =
				lstHeroes.SelectedItem.Text;
			string sql = string.Format(
				"select * from herojobs where name='{0}'",name);
			MySqlConnection conn = new
				MySqlConnection(ConnectionString);
			conn.Open();
			MySqlCommand cmd = new
				MySqlCommand(sql,conn);
			MySqlDataAdapter da = new
				MySqlDataAdapter();
			da.SelectCommand = cmd;
			da.Fill(ds);
			conn.Close();
			RefreshJob();
		}

		private void Page_Load(object sender, System.EventArgs e)
		{
			if (IsPostBack == false)
			{
				string sql =
					"select * from superheroes";
				MySqlConnection conn = new MySqlConnection(ConnectionString);
				conn.Open();
				MySqlCommand cmd = new MySqlCommand(sql,conn);
				IDataReader reader = cmd.ExecuteReader();
				lstHeroes.DataSource = reader;
				lstHeroes.DataTextField = "Name";
				lstHeroes.DataBind();
				ViewState["index"]=0;
				SelectRecords();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.lstHeroes.SelectedIndexChanged += new System.EventHandler(this.lstHeroes_SelectedIndexChanged);
			this.btnPrevious.Click += new System.EventHandler(this.btnPrevious_Click);
			this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
			this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void
			lstHeroes_SelectedIndexChanged(object sender,
			System.EventArgs e)
		{
			ViewState["index"]=0;
			SelectRecords();
		}

		private void btnNext_Click(object sender,
			System.EventArgs e)
		{
			SelectRecords();
			int index = (int)ViewState["index"];
			if (index == ds.Tables[0].Rows.Count-1)
				index = 0;
			else
				index ++;
			ViewState["index"] = index;
			RefreshJob();
		}
		private void btnPrevious_Click(object sender,
			System.EventArgs e)
		{
			SelectRecords();
			int index = (int)ViewState["index"];
			if (index == 0)
				index = ds.Tables[0].Rows.Count-1;
			else
				index --;
			ViewState["index"] = index;
			RefreshJob();
		}

		private void btnUpdate_Click(object sender,
			System.EventArgs e)
		{
			string name = lstHeroes.SelectedItem.Text;
			string job = txtJob.Text;
			string ranking = txtRanking.Text;
			string sql = string.Format(@"update herojobs set ranking = {0} where name='{1}' and job = '{2}'", ranking, name, job);
			MySqlConnection conn = new
				MySqlConnection(ConnectionString);
			conn.Open();
			MySqlCommand cmd = new
				MySqlCommand(sql,conn);
			cmd.ExecuteNonQuery();
			conn.Close();
		}	
	}
}
