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 System.Data.SqlClient;
namespace Chapter30
{
	/// <summary>
	///  StockMaster的描述说明.
	/// </summary>
	public class StockMaster : System.Web.UI.Page
	{
		protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
		protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
		protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
		protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
		protected System.Data.SqlClient.SqlConnection sqlConnection1;
		protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
		protected Chapter30.dsStock dsStock1;
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.Label messages;
		protected System.Web.UI.WebControls.LinkButton Add;
		protected System.Web.UI.WebControls.Panel AddPanel;
		protected System.Web.UI.WebControls.TextBox TextBox1;
		protected System.Web.UI.WebControls.Label label2;
		protected System.Web.UI.WebControls.Label Label3;
		protected System.Web.UI.WebControls.Label Label4;
		protected System.Web.UI.WebControls.Label Label5;
		protected System.Web.UI.WebControls.TextBox AddCode_Display;
		protected System.Web.UI.WebControls.TextBox AddRate;
		protected System.Web.UI.WebControls.TextBox AddUom;
		protected System.Web.UI.WebControls.TextBox AddClosing;
		protected System.Web.UI.WebControls.Label Label6;
		protected System.Web.UI.WebControls.Button SaveNew;
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
	
		public StockMaster()
		{
			Page.Init += new System.EventHandler(Page_Init);
		}

		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置页面初始化代码
			if (! IsPostBack)
			{
				Bind();
			}

		}

		private void Page_Init(object sender, EventArgs e)
		{
			//
			// CODEGEN:  该调用是 ASP.NET Web 服务设计器所必需的
			//
			InitializeComponent();
		}

		#region Web Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容
		/// </summary>
		private void InitializeComponent()
		{    
			this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
			this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
			this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
			this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
			this.dsStock1 = new Chapter30.dsStock();
			this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
			this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
			((System.ComponentModel.ISupportInitialize)(this.dsStock1)).BeginInit();
			// 
			// sqlInsertCommand1
			// 
			this.sqlInsertCommand1.CommandText = "INSERT INTO stock_master(code_display, rate, uom, closing, opening) VALUES (@code" +
				"_display, @rate, @uom, @closing, @opening); SELECT code_value, code_display, rat" +
				"e, uom, closing, opening FROM stock_master WHERE (code_value = @@IDENTITY)";
			this.sqlInsertCommand1.Connection = this.sqlConnection1;
			this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@code_display", System.Data.SqlDbType.VarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "code_display", System.Data.DataRowVersion.Current, null));
			this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@rate", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "rate", System.Data.DataRowVersion.Current, null));
			this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@uom", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "uom", System.Data.DataRowVersion.Current, null));
			this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@closing", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "closing", System.Data.DataRowVersion.Current, null));
			this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@opening", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "opening", System.Data.DataRowVersion.Current, null));
			// 
			// sqlConnection1
			// 
			this.sqlConnection1.ConnectionString = "data source=BHASIN;initial catalog=ASPNET;integrated security=SSPI;persist securi" +
				"ty info=True;workstation id=BHASIN;packet size=4096";
			// 
			// sqlSelectCommand1
			// 
			this.sqlSelectCommand1.CommandText = "SELECT code_value, code_display, rate, uom, closing, opening FROM stock_master";
			this.sqlSelectCommand1.Connection = this.sqlConnection1;
			// 
			// sqlDeleteCommand1
			// 
			this.sqlDeleteCommand1.CommandText = @"DELETE FROM stock_master WHERE (code_value = @code_value) AND (closing = @closing OR @closing1 IS NULL AND closing IS NULL) AND (code_display = @code_display) AND (opening = @opening OR @opening1 IS NULL AND opening IS NULL) AND (rate = @rate OR @rate1 IS NULL AND rate IS NULL) AND (uom = @uom OR @uom1 IS NULL AND uom IS NULL)";
			this.sqlDeleteCommand1.Connection = this.sqlConnection1;
			this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@code_value", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "code_value", System.Data.DataRowVersion.Original, null));
			this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@closing", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "closing", System.Data.DataRowVersion.Original, null));
			this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@closing1", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "closing", System.Data.DataRowVersion.Original, null));
			this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@code_display", System.Data.SqlDbType.VarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "code_display", System.Data.DataRowVersion.Original, null));
			this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@opening", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "opening", System.Data.DataRowVersion.Original, null));
			this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@opening1", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "opening", System.Data.DataRowVersion.Original, null));
			this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@rate", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "rate", System.Data.DataRowVersion.Original, null));
			this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@rate1", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "rate", System.Data.DataRowVersion.Original, null));
			this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@uom", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "uom", System.Data.DataRowVersion.Original, null));
			this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@uom1", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "uom", System.Data.DataRowVersion.Original, null));
			// 
			// dsStock1
			// 
			this.dsStock1.DataSetName = "dsStock";
			this.dsStock1.Locale = new System.Globalization.CultureInfo("en-US");
			this.dsStock1.Namespace = "http://www.tempuri.org/dsStock.xsd";
			// 
			// sqlDataAdapter1
			// 
			this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
			this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
			this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
			this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
																									  new System.Data.Common.DataTableMapping("Table", "stock_master", new System.Data.Common.DataColumnMapping[] {
																																																					  new System.Data.Common.DataColumnMapping("code_value", "code_value"),
																																																					  new System.Data.Common.DataColumnMapping("code_display", "code_display"),
																																																					  new System.Data.Common.DataColumnMapping("rate", "rate"),
																																																					  new System.Data.Common.DataColumnMapping("uom", "uom"),
																																																					  new System.Data.Common.DataColumnMapping("closing", "closing"),
																																																					  new System.Data.Common.DataColumnMapping("opening", "opening")})});
			this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
			// 
			// sqlUpdateCommand1
			// 
			this.sqlUpdateCommand1.CommandText = @"UPDATE stock_master SET code_display = @code_display, rate = @rate, uom = @uom, closing = @closing, opening = @opening WHERE (code_value = @Original_code_value) AND (closing = @Original_closing OR @Original_closing1 IS NULL AND closing IS NULL) AND (code_display = @Original_code_display) AND (opening = @Original_opening OR @Original_opening1 IS NULL AND opening IS NULL) AND (rate = @Original_rate OR @Original_rate1 IS NULL AND rate IS NULL) AND (uom = @Original_uom OR @Original_uom1 IS NULL AND uom IS NULL); SELECT code_value, code_display, rate, uom, closing, opening FROM stock_master WHERE (code_value = @Select_code_value)";
			this.sqlUpdateCommand1.Connection = this.sqlConnection1;
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@code_display", System.Data.SqlDbType.VarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "code_display", System.Data.DataRowVersion.Current, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@rate", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "rate", System.Data.DataRowVersion.Current, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@uom", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "uom", System.Data.DataRowVersion.Current, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@closing", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "closing", System.Data.DataRowVersion.Current, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@opening", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "opening", System.Data.DataRowVersion.Current, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_code_value", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "code_value", System.Data.DataRowVersion.Original, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_closing", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "closing", System.Data.DataRowVersion.Original, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_closing1", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "closing", System.Data.DataRowVersion.Original, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_code_display", System.Data.SqlDbType.VarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "code_display", System.Data.DataRowVersion.Original, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_opening", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "opening", System.Data.DataRowVersion.Original, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_opening1", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "opening", System.Data.DataRowVersion.Original, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_rate", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "rate", System.Data.DataRowVersion.Original, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_rate1", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "rate", System.Data.DataRowVersion.Original, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_uom", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "uom", System.Data.DataRowVersion.Original, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_uom1", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "uom", System.Data.DataRowVersion.Original, null));
			this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Select_code_value", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "code_value", System.Data.DataRowVersion.Current, null));
			this.Add.Click += new System.EventHandler(this.Add_Click);
			this.SaveNew.Click += new System.EventHandler(this.SaveNew_Click);
			this.Load += new System.EventHandler(this.Page_Load);
			((System.ComponentModel.ISupportInitialize)(this.dsStock1)).EndInit();

		}
		#endregion
		public void Bind()
		{
			sqlDataAdapter1.Fill(dsStock1);
			DataGrid1.DataSource=dsStock1;
			DataGrid1.DataBind(); 
			if ( AddPanel.Visible==true)
			{AddPanel.Visible=false;}
		}
		
		
		public void Grid1_Edit(Object sender, DataGridCommandEventArgs e) 
		{
			DataGrid1.EditItemIndex = e.Item.ItemIndex;
			Bind();
		}

		public void Grid1_Cancel(Object sender, DataGridCommandEventArgs e) 
		{
			DataGrid1.EditItemIndex = -1;
			Bind();
		}
		
		
		public void Grid1_Update(Object sender, DataGridCommandEventArgs e) 
		{
			TextBox t;
			//主键
			int Key=	(int)DataGrid1.DataKeys[(int)e.Item.ItemIndex];
			String code_value = " code_value =" + Key.ToString()  ;
			
			t = (TextBox) e.Item.FindControl("editCode_Display");
			String code_display = " code_display = '" + t.Text.Trim() + "'," ;

			t = (TextBox) e.Item.FindControl("editRate");
			String rate = " rate = " + t.Text.Trim() + "," ;

			t = (TextBox) e.Item.FindControl("editUom");
			String uom = " uom ='" + t.Text.Trim() + "'," ;

			t = (TextBox) e.Item.FindControl("editOpening");
			String opening = " opening =  " + t.Text.Trim()  ;

			String s = " Update stock_master Set"; 
			s +=   code_display +rate +uom ;
			s +=   opening  ;
			s +=  " Where " + code_value;
			RunSql(s);
		
			DataGrid1.EditItemIndex = -1;
			Bind();
		}
		public void Grid1_Delete(Object sender, DataGridCommandEventArgs e) 
		{
			int Key=	(int)DataGrid1.DataKeys[(int)e.Item.ItemIndex];
			String code_value =  Key.ToString()  ;
			String s = "Delete from stock_master"; 
				   s +=" where code_value = " + code_value;
			RunSql(s);
			DataGrid1.EditItemIndex = -1;
			Bind();
		}
		public String RunSql( string vsql)
		{
			try
			{
				SqlCommand mycommand = new SqlCommand(vsql,sqlConnection1);
				sqlConnection1.Open();
				mycommand.ExecuteNonQuery();
				sqlConnection1.Close();
			}
			catch(Exception e)
			{
				string ret =	"Exception: " + e.ToString() ;
				messages.Text=ret;
			}

			return("OK");

		}

		

		private void Add_Click(object sender, System.EventArgs e)
		{
			AddPanel.Visible=true;
			AddCode_Display.Text= "";
			AddRate.Text= "0";
			AddUom.Text= "";
			AddClosing.Text= "0";
									
			
		}

		private void SaveNew_Click(object sender, System.EventArgs e)
		{
			if (AddCode_Display.Text.Length == 0)
				
			{
				messages.Text="Sorry - Account Name cannot be blank";
				return ;
			}
		
			if (AddRate.Text.Length == 0)
			{AddRate.Text= "0";}
			if (AddUom.Text.Length == 0)
			{AddUom.Text= "";}
			if (AddClosing.Text.Length == 0)
			{AddClosing.Text= "0";}
			
			String s =  "Execute p_stock_master ";
			s += " @code_value=NULL," ;
			s += " @code_display = '" + AddCode_Display.Text + "',";
			s += " @rate = " + AddRate.Text + ",";  
			s += " @uom = '" + AddUom.Text + "',";
			s += " @closing = " + AddClosing.Text ;

			messages.Text= "";
			RunSql(s);
		
			DataGrid1.EditItemIndex = -1;
			Bind();
			
		}
		


	}
}
