using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace WindowsApplication1
{
	/// <summary>
	/// Summary description for Form4.
	/// </summary>
	public class Form4 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox textBox8;
		private System.Windows.Forms.TextBox textBox7;
		private System.Windows.Forms.TextBox textBox6;
		private System.Windows.Forms.TextBox textBox5;
		private System.Windows.Forms.TextBox textBox4;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Button button1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		public Form4()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form4));
			this.panel1 = new System.Windows.Forms.Panel();
			this.button2 = new System.Windows.Forms.Button();
			this.label8 = new System.Windows.Forms.Label();
			this.label7 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.textBox8 = new System.Windows.Forms.TextBox();
			this.textBox7 = new System.Windows.Forms.TextBox();
			this.textBox6 = new System.Windows.Forms.TextBox();
			this.textBox5 = new System.Windows.Forms.TextBox();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel1.BackgroundImage")));
			this.panel1.Controls.Add(this.button2);
			this.panel1.Controls.Add(this.label8);
			this.panel1.Controls.Add(this.label7);
			this.panel1.Controls.Add(this.label6);
			this.panel1.Controls.Add(this.label5);
			this.panel1.Controls.Add(this.label4);
			this.panel1.Controls.Add(this.label3);
			this.panel1.Controls.Add(this.label2);
			this.panel1.Controls.Add(this.label1);
			this.panel1.Controls.Add(this.textBox8);
			this.panel1.Controls.Add(this.textBox7);
			this.panel1.Controls.Add(this.textBox6);
			this.panel1.Controls.Add(this.textBox5);
			this.panel1.Controls.Add(this.textBox4);
			this.panel1.Controls.Add(this.textBox3);
			this.panel1.Controls.Add(this.textBox2);
			this.panel1.Controls.Add(this.textBox1);
			this.panel1.Controls.Add(this.button1);
			this.panel1.ForeColor = System.Drawing.SystemColors.ControlText;
			this.panel1.Location = new System.Drawing.Point(0, 0);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(272, 312);
			this.panel1.TabIndex = 2;
			this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
			// 
			// button2
			// 
			this.button2.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.button2.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button2.Location = new System.Drawing.Point(40, 272);
			this.button2.Name = "button2";
			this.button2.TabIndex = 18;
			this.button2.Text = "درج";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// label8
			// 
			this.label8.Font = new System.Drawing.Font("Tahoma", 13F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label8.Image = ((System.Drawing.Image)(resources.GetObject("label8.Image")));
			this.label8.Location = new System.Drawing.Point(136, 240);
			this.label8.Name = "label8";
			this.label8.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.label8.Size = new System.Drawing.Size(120, 23);
			this.label8.TabIndex = 17;
			this.label8.Text = "شماره کارمند";
			this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// label7
			// 
			this.label7.Font = new System.Drawing.Font("Tahoma", 13F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label7.Image = ((System.Drawing.Image)(resources.GetObject("label7.Image")));
			this.label7.Location = new System.Drawing.Point(136, 208);
			this.label7.Name = "label7";
			this.label7.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.label7.Size = new System.Drawing.Size(120, 23);
			this.label7.TabIndex = 16;
			this.label7.Text = "قیمت فروش";
			this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// label6
			// 
			this.label6.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.label6.Font = new System.Drawing.Font("Tahoma", 13F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label6.Image = ((System.Drawing.Image)(resources.GetObject("label6.Image")));
			this.label6.Location = new System.Drawing.Point(136, 176);
			this.label6.Name = "label6";
			this.label6.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.label6.Size = new System.Drawing.Size(120, 23);
			this.label6.TabIndex = 15;
			this.label6.Text = "قیمت خرید";
			this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// label5
			// 
			this.label5.Font = new System.Drawing.Font("Tahoma", 13F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label5.Image = ((System.Drawing.Image)(resources.GetObject("label5.Image")));
			this.label5.Location = new System.Drawing.Point(136, 144);
			this.label5.Name = "label5";
			this.label5.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.label5.Size = new System.Drawing.Size(120, 23);
			this.label5.TabIndex = 14;
			this.label5.Text = "تاریخ خرید";
			this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// label4
			// 
			this.label4.Font = new System.Drawing.Font("Tahoma", 13F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label4.Image = ((System.Drawing.Image)(resources.GetObject("label4.Image")));
			this.label4.Location = new System.Drawing.Point(136, 112);
			this.label4.Name = "label4";
			this.label4.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.label4.Size = new System.Drawing.Size(120, 23);
			this.label4.TabIndex = 13;
			this.label4.Text = "کارخانه سازنده";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// label3
			// 
			this.label3.Font = new System.Drawing.Font("Tahoma", 13F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label3.Image = ((System.Drawing.Image)(resources.GetObject("label3.Image")));
			this.label3.Location = new System.Drawing.Point(136, 80);
			this.label3.Name = "label3";
			this.label3.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.label3.Size = new System.Drawing.Size(120, 23);
			this.label3.TabIndex = 12;
			this.label3.Text = "تعداد کالا";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("Tahoma", 13F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label2.Image = ((System.Drawing.Image)(resources.GetObject("label2.Image")));
			this.label2.Location = new System.Drawing.Point(136, 48);
			this.label2.Name = "label2";
			this.label2.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.label2.Size = new System.Drawing.Size(120, 23);
			this.label2.TabIndex = 11;
			this.label2.Text = "کدکالا";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Tahoma", 13F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Image = ((System.Drawing.Image)(resources.GetObject("label1.Image")));
			this.label1.Location = new System.Drawing.Point(136, 16);
			this.label1.Name = "label1";
			this.label1.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.label1.Size = new System.Drawing.Size(120, 23);
			this.label1.TabIndex = 10;
			this.label1.Text = "نام کالا";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// textBox8
			// 
			this.textBox8.Location = new System.Drawing.Point(16, 240);
			this.textBox8.Name = "textBox8";
			this.textBox8.TabIndex = 9;
			this.textBox8.Text = "";
			this.textBox8.Leave += new System.EventHandler(this.textBox8_Leave);
			// 
			// textBox7
			// 
			this.textBox7.Location = new System.Drawing.Point(16, 208);
			this.textBox7.Name = "textBox7";
			this.textBox7.TabIndex = 8;
			this.textBox7.Text = "";
			this.textBox7.Leave += new System.EventHandler(this.textBox7_Leave);
			// 
			// textBox6
			// 
			this.textBox6.Location = new System.Drawing.Point(16, 176);
			this.textBox6.Name = "textBox6";
			this.textBox6.TabIndex = 7;
			this.textBox6.Text = "";
			this.textBox6.Leave += new System.EventHandler(this.textBox6_Leave);
			// 
			// textBox5
			// 
			this.textBox5.Location = new System.Drawing.Point(16, 144);
			this.textBox5.Name = "textBox5";
			this.textBox5.TabIndex = 6;
			this.textBox5.Text = "";
			this.textBox5.Leave += new System.EventHandler(this.textBox5_Leave);
			// 
			// textBox4
			// 
			this.textBox4.Location = new System.Drawing.Point(16, 112);
			this.textBox4.Name = "textBox4";
			this.textBox4.TabIndex = 5;
			this.textBox4.Text = "";
			this.textBox4.TextChanged += new System.EventHandler(this.textBox4_TextChanged);
			this.textBox4.Leave += new System.EventHandler(this.textBox4_Leave);
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(16, 80);
			this.textBox3.Name = "textBox3";
			this.textBox3.TabIndex = 4;
			this.textBox3.Text = "";
			this.textBox3.Leave += new System.EventHandler(this.textBox3_Leave);
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(16, 48);
			this.textBox2.Name = "textBox2";
			this.textBox2.TabIndex = 3;
			this.textBox2.Text = "";
			this.textBox2.Leave += new System.EventHandler(this.textBox2_Leave);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(16, 16);
			this.textBox1.Name = "textBox1";
			this.textBox1.TabIndex = 2;
			this.textBox1.Text = "";
			this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
			this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
			// 
			// button1
			// 
			this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.button1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button1.Location = new System.Drawing.Point(152, 272);
			this.button1.Name = "button1";
			this.button1.TabIndex = 1;
			this.button1.Text = "خروج";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// Form4
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(272, 310);
			this.Controls.Add(this.panel1);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "Form4";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "درج کالا ";
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void button1_Click(object sender, System.EventArgs e)
		{
			Close();
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			SqlConnection con=new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=abzar;Data Source=MAHDI-59CA33515\\WINCC");
			con.Open();
			SqlDataAdapter adp1=new SqlDataAdapter("select codekala from kala",con);
			SqlDataAdapter adp2=new SqlDataAdapter("select workerid from worker",con);
			SqlDataAdapter adp3=new SqlDataAdapter("select factor from kala",con);
			DataSet dst1=new DataSet();
			DataSet dst2=new DataSet();
			DataSet dst3=new DataSet();
		    DataView dv1=new DataView();
			DataView dv2=new DataView();
			DataView dv3=new DataView();
			adp1.Fill(dst1,"kala1");
			adp2.Fill(dst2,"worker1");
			adp3.Fill(dst3,"kala2");
		    dv1=dst1.Tables["kala1"].DefaultView;
			dv2=dst2.Tables["worker1"].DefaultView;
			dv3=dst3.Tables["kala2"].DefaultView;
			dv2.Sort="workerid";
			dv1.Sort="codekala";
			dv3.Sort="factor";
			if (dv2.Find(textBox8.Text)<0)
			{
				DialogResult msg1;
				msg1=MessageBox.Show(".شماره کارمندی وارد شده اشتباه است","اشکال",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
			else
			{
				if (dv1.Find(textBox2.Text)<0)
				{
					SqlCommand com=new SqlCommand("insert into kala (name,codekala,number,factor,date1,pricebuy,pricesell,karmandid)values(@a1,@a2,@a3,@a4,@a5,@a6,@a7,@a8)",con);
					com.Parameters.Add("@a1",textBox1.Text);
					com.Parameters.Add("@a2",textBox2.Text);
					com.Parameters.Add("@a3",Convert.ToInt32(textBox3.Text));
					com.Parameters.Add("@a4",textBox4.Text);
					com.Parameters.Add("@a5",Convert.ToDateTime(textBox5.Text));
					com.Parameters.Add("@a6",Convert.ToInt32(textBox6.Text));
					com.Parameters.Add("@a7",Convert.ToInt32(textBox7.Text));
					com.Parameters.Add("@a8",textBox8.Text);
					com.ExecuteNonQuery();
					DialogResult msg1;
					msg1=MessageBox.Show(".کالای جدید به درستی در بانک ذخیره گردید","درج",MessageBoxButtons.OK,MessageBoxIcon.Information);
			    }
				else
				{
					SqlCommand com=new SqlCommand("update kala set name=@a1,codekala=@a2,number=@a3+number,factor=@a4,date1=@a5,pricebuy=@a6,pricesell=@a7,karmandid=@a8 where codekala=@a2",con);
					com.Parameters.Add("@a1",textBox1.Text);
					com.Parameters.Add("@a2",textBox2.Text);
					com.Parameters.Add("@a3",Convert.ToInt32(textBox3.Text));
					com.Parameters.Add("@a4",textBox4.Text);
					com.Parameters.Add("@a5",Convert.ToDateTime(textBox5.Text));
					com.Parameters.Add("@a6",Convert.ToInt32(textBox6.Text));
					com.Parameters.Add("@a7",Convert.ToInt32(textBox7.Text));
					com.Parameters.Add("@a8",textBox8.Text);
					com.ExecuteNonQuery();
					DialogResult msg1;
					msg1=MessageBox.Show(".کالا از قبل در بانک وجود داشته وبه روز رسانی گردید","به روز رسانی",MessageBoxButtons.OK,MessageBoxIcon.Information);
				}
			con.Close();
			}
		}

		private void textBox1_TextChanged(object sender, System.EventArgs e)
		{
			
		}

		private void textBox1_Leave(object sender, System.EventArgs e)
		{
			if (textBox1.Text=="")
			{
			DialogResult msg1;
			msg1=MessageBox.Show(".مقدار را باید قبل از خروج وارد کنید","اشکال",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}

		private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
		
		}

		private void textBox2_Leave(object sender, System.EventArgs e)
		{
			if (textBox2.Text=="")
			{
				DialogResult msg1;
				msg1=MessageBox.Show(".مقدار را باید قبل از خروج وارد کنید","اشکال",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}

		private void textBox3_Leave(object sender, System.EventArgs e)
		{
			if (textBox3.Text=="")
			{
				DialogResult msg1;
				msg1=MessageBox.Show(".مقدار را باید قبل از خروج وارد کنید","اشکال",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}

		private void textBox4_Leave(object sender, System.EventArgs e)
		{
			if (textBox4.Text=="")
			{
				DialogResult msg1;
				msg1=MessageBox.Show(".مقدار را باید قبل از خروج وارد کنید","اشکال",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}

		private void textBox5_Leave(object sender, System.EventArgs e)
		{
			if (textBox5.Text=="")
			{
				DialogResult msg1;
				msg1=MessageBox.Show(".مقدار را باید قبل از خروج وارد کنید","اشکال",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}

		private void textBox6_Leave(object sender, System.EventArgs e)
		{
			if (textBox6.Text=="")
			{
				DialogResult msg1;
				msg1=MessageBox.Show(".مقدار را باید قبل از خروج وارد کنید","اشکال",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}

		private void textBox7_Leave(object sender, System.EventArgs e)
		{
			if (textBox7.Text=="")
			{
				DialogResult msg1;
				msg1=MessageBox.Show(".مقدار را باید قبل از خروج وارد کنید","اشکال",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}

		private void textBox8_Leave(object sender, System.EventArgs e)
		{
			if (textBox8.Text=="")
			{
				DialogResult msg1;
				msg1=MessageBox.Show(".مقدار را باید قبل از خروج وارد کنید","اشکال",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}

		private void textBox4_TextChanged(object sender, System.EventArgs e)
		{
		
		}
	}
}
