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 FormDrugdefinition : Form
    {
        public FormDrugdefinition()
        {
            objConnection =new SqlConnection("server=(local);database=Drug;Integrated Security=True");
            objDataAdapter=new SqlDataAdapter(_ComandText,objConnection);
            InitializeComponent();
        }
        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]);
               
        }
        private void BindFields()
        {
        // Clear any previous bindings...
            txtAccesscodedrug.DataBindings.Clear();
            txtDrugdefinition.DataBindings.Clear();
            txtPersionDefinition.DataBindings.Clear();
            txtPrice.DataBindings.Clear();
            txtShortDefinition.DataBindings.Clear();
            cmbDrugExchange.DataBindings.Clear();
            cmbDrugform.DataBindings.Clear();
            txtStock.DataBindings.Clear();
       // Add new bindings to the DataView object...
            txtDrugdefinition.DataBindings.Add("Text",objDataView,"LatinDrug");
            txtShortDefinition.DataBindings.Add("Text",objDataView,"ShortDrug");
            txtPersionDefinition.DataBindings.Add("Text",objDataView,"PersionDrug");
            txtAccesscodedrug.DataBindings.Add("Text",objDataView,"AccessCodeDrug");
            cmbDrugExchange.DataBindings.Add("Text",objDataView,"ExchangeUnit");
            txtPrice.DataBindings.Add("Text",objDataView,"Price");
            cmbDrugform.DataBindings.Add("Text",objDataView,"Drugform");
            txtStock.DataBindings.Add("Text",objDataView,"Stock");
//        ToolStripStatusLabel1.Text="Ready";
        }
        private void ShowPosition()
        {
        // Display the current position
        // and the number of records
            txtRecordPosition.Text=(objCurrencyManager.Position+1)+" Of "+objCurrencyManager.Count;
        }
        
        private void FormDrugdefinition_Load(object sender, EventArgs e)
        {
            btnUpdate.Enabled = false;
            txtStock.Enabled = false;
            btnsave.Enabled = false;
            // Fill the DataSet and bind the fields...
            FillDataSetAndView();
            BindFields();
            // Show the current record position...
            ShowPosition();
        }

        DrugStore Drug = new DrugStore();
        private void btnok_Click(object sender, EventArgs e)
        {
            Cleantxt();
            btnsave.Enabled = true;
                    }
        public void Cleantxt()
        {
            txtDrugdefinition.Text = "";
            txtShortDefinition.Text = "";
            txtPersionDefinition.Text = "";
            txtAccesscodedrug.Text = "";
            txtPrice.Text = "";
            cmbDrugExchange.Text = "";
            cmbDrugform.Text = "";
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
             }

        private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
        
        }

        private void btnmovefirst_Click(object sender, EventArgs e)
        {
                // Set the record position to the first record...
                   objCurrencyManager.Position = 0;
                // Show the current record position...
                    ShowPosition();
        }

        private void btnmoveprevious_Click(object sender, EventArgs e)
        {
            // Move to the previous record...
            objCurrencyManager.Position -= 1;
            // Show the current record position...
            ShowPosition();
        }

        private void btnmovenext_Click(object sender, EventArgs e)
        {
            // Move to the next record...
               objCurrencyManager.Position += 1;
            //Show the current record position...
               ShowPosition();
        }

        private void btnmovelast_Click(object sender, EventArgs e)
        {
            // Set the record position to the last record...
                objCurrencyManager.Position =
                objCurrencyManager.Count - 1;
            // Show the current record position...
                ShowPosition();
        }

        private void btnsearch_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 (cmbsearch.SelectedIndex)
            {
                case 0://LatinDrug
                    objDataView.Sort = "LatinDrug";
                    break;
                case 1://ShortDrug
                    objDataView.Sort = "shortdrug";
                    break;
                case 2://PersionDrug
                    objDataView.Sort = "PersionDrug";
                    break;
                case 3://AccessCodedrug
                    objDataView.Sort = "AccessCodeDrug";
                    break;
                case 4://ExchangeUnit
                    objDataView.Sort = "ExchangeUnit";
                    break;
                case 5://price
                    objDataView.Sort = "Price";
                    break;
                case 6://DrugForm
                    objDataView.Sort = "DrugForm";
                    break;
            }
            // Find the search...
            intPosition = objDataView.Find(txtSearch.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("رکورد مورد نطر پیدا شد","جستجو");
            objCurrencyManager.Position = intPosition;
            btnUpdate.Enabled = true;
            }
            // Show the current record position...
            ShowPosition();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            string LatinDrugV, ShortDrugV, PersionDrugV, ExchangeUnitV, DrugFormV, AccessCodeDrugV, PriceV;
            LatinDrugV = txtDrugdefinition.Text;
            ShortDrugV = txtShortDefinition.Text;
            PersionDrugV = txtPersionDefinition.Text;
            AccessCodeDrugV = txtAccesscodedrug.Text;
            ExchangeUnitV = cmbDrugExchange.Text;
            PriceV = txtPrice.Text;
            DrugFormV = cmbDrugform.Text;
            Drug.AddDrug(LatinDrugV, ShortDrugV, PersionDrugV, AccessCodeDrugV, ExchangeUnitV, PriceV, DrugFormV);
            btnok.Enabled = false;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            // Declare local variables and objects...
            int intPosition;
            SqlCommand objCommand = new SqlCommand();
            // Save the current record position...
            intPosition = objCurrencyManager.Position;
            // Set the SqlCommand object properties...
            objCommand.Connection = objConnection;
            objCommand.CommandText = "UPDATE DrugDefinition SET LatinDrug = @LatinDrug, ShortDrug = @ShortDrug ,PersionDrug=@PersionDrug ,AccessCodeDrug=@AccessCodeDrug ,ExchangeUnit=@ExchangeUnit ,Price=@Price , DrugForm=@DrugForm WHERE AccessCodeDrug = @AccessCodeDrug";
              objCommand.CommandType = CommandType.Text;
            // Add parameters for the placeholders in the SQL in
            // the CommandText property...
            // Parameter for the title field...
             objCommand.Parameters.AddWithValue("@LatinDrug", txtDrugdefinition.Text);
             objCommand.Parameters.AddWithValue("@ShortDrug", txtShortDefinition.Text);
             objCommand.Parameters.AddWithValue("@PersionDrug", txtPersionDefinition.Text);
             //objCommand.Parameters.AddWithValue("@AccessCodeDrug", AccessCodeDrugV);
             objCommand.Parameters.AddWithValue("@ExchangeUnit", cmbDrugExchange.Text);
             objCommand.Parameters.AddWithValue("@Price", txtPrice.Text);
             objCommand.Parameters.AddWithValue("@DrugForm", cmbDrugform.Text);
             // Parameter for the title_id field...
            objCommand.Parameters.AddWithValue("@AccesscodeDrug",
            this.BindingContext[objDataView, "AccessCodeDrug"].Current);
            // Open the connection...
            objConnection.Open();
            // Execute the SqlCommand object to update the data...
            objCommand.ExecuteNonQuery();
            // Close the connection...
            objConnection.Close();
            // Fill the DataSet and bind the fields...
            FillDataSetAndView();
             BindFields();
            // Set the record position
            // to the one that you saved...
            objCurrencyManager.Position = intPosition;
            // Show the current record position...
            ShowPosition();
            // Display a message that the record was updated...
                MessageBox.Show("مشخصات  رکورد به هنگام سازی شد","ویرایش");
                btnUpdate.Enabled = false;
}

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
        }
}   
        

        
    