using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class FormDrugSelling : Form
    {
        private const string _CommandText ="SELECT DrugSeller,AccessCodeDrug,DrugTotalBuy,DrugFactorNum,DrugFactorDate,AccessCodeSell,DrugBuyAmount FROM DrugSelling";
        private const string _ConnectionString = "server=(local);database=Drug;Integrated Security=True";
        // Declare global objects...
        SqlConnection objConnect;
        SqlDataAdapter objDataAdapt;
        DataSet objDataSeting;
        DataView objDataViewing;
        CurrencyManager objCurrencyManagering;
        private void FillDataSetAndViewing()
        {
        // Initialize a new instance of the DataSet object...
        objDataSeting = new DataSet();
        // Fill the DataSet object with data...
        objDataAdapt.Fill(objDataSeting, "DrugSelling");
        // Set the DataView object to the DataSet object...
        objDataViewing = new DataView(
        objDataSeting.Tables["DrugSelling"]);
        // Set our CurrencyManager object
        // to the DataView object...
        objCurrencyManagering = (CurrencyManager)(
        this.BindingContext[objDataViewing]);
}  
        private void BindFielding()
{
        // Clear any previous bindings...
        cmbSeller.DataBindings.Clear();
        txtAccessCodeDrug.DataBindings.Clear();
        txtAmountBuy.DataBindings.Clear();
        txtFactorNum.DataBindings.Clear();
        txtDate.DataBindings.Clear();
        txtTotalbuy.DataBindings.Clear();
        txtAccessCodeSell.DataBindings.Clear();
        // Add new bindings to the DataView object...
        cmbSeller.DataBindings.Add("Text",
         objDataViewing, "DrugSeller");
        txtAccessCodeDrug.DataBindings.Add("Text",
        objDataViewing, "AccessCodeDrug");
        txtTotalbuy.DataBindings.Add("Text",
        objDataViewing, "DrugTotalBuy");
        txtFactorNum.DataBindings.Add("Text",
        objDataViewing, "DrugFactorNum");
        txtDate.DataBindings.Add("Text",
        objDataViewing,"DrugFactorDate");
        txtAmountBuy.DataBindings.Add("Text",
        objDataViewing,"DrugBuyAmount");
        txtAccessCodeSell  .DataBindings.Add("Text",
        objDataViewing,"AccessCodeSell");
            // Display a ready status...
        //ToolStripStatusLabel1.Text = "Ready";
            
        }
         public FormDrugSelling()
        {
            objConnect = new SqlConnection(_ConnectionString);
            objDataAdapt = new SqlDataAdapter(_CommandText,objConnect);
            objConnection = new SqlConnection("server=(local);database=Drug;Integrated Security=True");
            objDataAdapter = new SqlDataAdapter("SELECT LatinDrug,ShortDrug,PersionDrug,AccessCodeDrug,ExchangeUnit,Price,Drugform,Stock FROM DrugDefinition", objConnection);
            
            InitializeComponent();
        }

        DrugSell sell = new DrugSell();
        private void button1_Click(object sender, EventArgs e)
        {
            Cleantxt();
            btnsaveSell.Enabled = true;
                    
        }
        public void UpdateDrugDefinition()
        {
            SqlCommand objCommand = new SqlCommand();
            // Set the SqlCommand object properties...
            objCommand.Connection = objConnection;
            objCommand.CommandText = "UPDATE DrugDefinition SET AccessCodeDrug=@AccessCodeDrug , Stock=@Stock WHERE AccessCodeDrug = @AccessCodeDrug ";
            objCommand.CommandType = CommandType.Text;
            // Add parameters for the placeholders in the SQL in
            // the CommandText property...
            // Parameter for the title field...
            string TempStock1 = txtAmountBuy.Text;
            txtAmountBuy.Text = "";
            FillDataSetAndView();
            txtAmountBuy.DataBindings.Clear();
            txtAmountBuy.DataBindings.Add("Text", objDataView, "Stock");
            Int64 TempStock2 = int.Parse(TempStock1) + int.Parse(txtAmountBuy.Text);
            objCommand.Parameters.AddWithValue("@Stock", TempStock2);
            // Parameter for the title_id field...
            objCommand.Parameters.AddWithValue("@AccesscodeDrug", txtAccessCodeDrug.Text);
            // Open the connection...
            objConnection.Open();
            // Execute the SqlCommand object to update the data...
            objCommand.ExecuteNonQuery();
            // Close the connection...
            objConnection.Close();

        
        
        }
        private void FillDataSetAndView()
        {
            // Initialize a new instance of the dataset object 
            objDataSet = new DataSet();
            // fill the dataset objectwith data 
            objDataAdapter.Fill(objDataSet, "DrugDefinition");
            // Set the DataView object to the DataSet object...
            objDataView = new DataView(objDataSet.Tables["DrugDefinition"]);
            // Set our CurrencyManager object
            // to the DataView object...
            objCurrencyManager = (CurrencyManager)(this.BindingContext[objDataView]);

        }
        public void Cleantxt()
        {
            cmbSeller.Text = "";
            txtAccessCodeDrug.Text = "";
            txtTotalbuy.Text = "";
            txtFactorNum.Text = "";
            txtDate.Text = "";
            txtAmountBuy.Text = "";
            txtAccessCodeSell.Text = "";
             }
        private void FormDrugSelling_Load(object sender, EventArgs e)
        {
            btnsaveSell.Enabled = false;
            FillDataSetAndViewing();
            BindFielding();
            btnUpdateSell.Enabled = false;
                    }

        private void btnsaveSell_Click(object sender, EventArgs e)
        {
            string DrugSellerV, AccessCodeDrugV, DrugTotalBuyV, DrugFactorNumV, DrugFactorDateV, AccessCodeSellV, DrugBuyAmountV;
            DrugSellerV = cmbSeller.Text;
            AccessCodeDrugV = txtAccessCodeDrug.Text;
            DrugTotalBuyV = txtTotalbuy.Text;
            DrugFactorNumV = txtFactorNum.Text;
            DrugFactorDateV = txtDate.Text;
            AccessCodeSellV = txtAccessCodeSell.Text;
            DrugBuyAmountV = txtAmountBuy.Text;
            sell.AddSell(DrugSellerV, AccessCodeDrugV, DrugTotalBuyV, DrugFactorNumV, DrugFactorDateV, AccessCodeSellV, DrugBuyAmountV);
            UpdateDrugDefinition();
        }

        private void btnSearchSell_Click(object sender, EventArgs e)
        {
            // Declare local variables...
            int intPosition;
            // Determine the appropriate item selected and set the
            // Sort property of the DataView object...
            switch (cmbSearchSell.SelectedIndex)
            {
                case 0://LatinDrug
                    objDataViewing.Sort = "AccessCodeSell";
                     break;
                case 1://ShortDrug
                    objDataViewing.Sort = "DrugSeller";
                     break;
                case 2://PersionDrug
                    objDataViewing.Sort = "AccessCodeDrug";
                    break;
                case 3://AccessCodedrug
                    objDataViewing.Sort = "DrugBuyAmount";
                    break;
                case 4://ExchangeUnit
                    objDataViewing.Sort = "DrugTotalBuy";
                    break;
                case 5://price
                    objDataViewing.Sort = "DrugFactorNum";
                    break;
                case 6://DrugForm
                    objDataViewing.Sort = "DrugFactorDate";
                    break;
            }
            // Find the search...
            intPosition = objDataViewing.Find(txtSearchSell.Text);
            if (intPosition == -1)
            {
             // Display a message
            // that the record was not found...
            MessageBox.Show("رکوردی با مشخصات فوق پیدا نشد","جستجو");
            }
                else
            {
            // Otherwise display a message that the record
            // was found and reposition the CurrencyManager
            // to that record...
            MessageBox.Show("رکورد مورد نطر پیدا شد","جستجو");
            objCurrencyManagering.Position = intPosition;
            cmbSearchSell.Text = "";
            txtSearchSell.Text = "";
            btnUpdateSell.Enabled = true;
            }
            }

        private void btnUpdateSell_Click(object sender, EventArgs e)
        {
            
        // Declare local variables and objects...
        int intPosition;
        SqlCommand objCommanding = new SqlCommand();
        // Save the current record position...
        intPosition = objCurrencyManagering.Position;
        // Set the SqlCommand object properties...
        objCommanding.Connection = objConnection;
        objCommanding.CommandText = "UPDATE DrugSelling SET DrugSeller=@DrugSeller,AccessCodeDrug=@AccessCodeDrug,DrugTotalBuy=@DrugTotalBuy,DrugFactorNum=@DrugFactorNum,DrugFactorDate=@DrugFactorDate,AccessCodeSell=@AccessCodeSell,DrugBuyAmount=@DrugBuyAmount WHERE AccessCodeSell=@AccessCodeSell";
        objCommanding.CommandType = CommandType.Text;
        // Add parameters for the placeholders in the SQL in
        // the CommandText property...
        // Parameter for the title field...
        objCommanding.Parameters.AddWithValue("@DrugSeller",
       cmbSeller.Text);
        // Parameter for the price field...
        objCommanding.Parameters.AddWithValue("@AccessCodeDrug",
       txtAccessCodeDrug.Text);
        // Parameter for the title_id field...
        objCommanding.Parameters.AddWithValue("@AccessCodeSell",
        this.BindingContext[objDataViewing, "AccessCodeSell"].Current);
        //
        objCommanding.Parameters.AddWithValue("@DrugTotalBuy", 
        txtTotalbuy.Text);
        //      
        objCommanding.Parameters.AddWithValue("@DrugFactorNum",
        txtFactorNum.Text);
        //
        objCommanding.Parameters.AddWithValue("@DrugFactorDate",
        txtDate.Text);    
        //
        objCommanding.Parameters.AddWithValue("@DrugBuyAmount",
        txtAmountBuy.Text);
            
            // Open the connection...
        objConnection.Open();
        // Execute the SqlCommand object to update the data...
        objCommanding.ExecuteNonQuery();
        // Close the connection...
        objConnection.Close();
        // Fill the DataSet and bind the fields...
        FillDataSetAndViewing();
        BindFielding();
        // Set the record position
        // to the one that you saved...
        objCurrencyManagering.Position = intPosition;
        // Showthe current record position...
        //ShowPosition();
        // Display a message that the record was updated...
        //ToolStripStatusLabel1.Text = "Record Updated";
        MessageBox.Show("مشخصات  رکورد به هنگام سازی شد", "ویرایش");
        }

        private void cmbSeller_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        }
    }

