using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

using CrystalDecisions.Windows.Forms;


namespace DabirKhane
{
    public  class Payegah:Variables  
    {
     

        public static void ShowData(string Etesal ,string StrSql, int Tedad, DataGridView Grid)
        {
            
            SqlConnection conection = new SqlConnection(Etesal);
            conection .Open ();
            SqlCommand Comand = new SqlCommand(StrSql, conection);
            SqlDataReader dr = Comand.ExecuteReader();
            Grid.Rows.Clear();
            object[] Obj = new object[Tedad];
            while (dr.Read())
            {
                dr.GetValues(Obj);
                Grid.Rows.Add(Obj);
            }
            //LastNumber = Obj[0].ToString();
            conection.Close();
            //SqlDataAdapter SDA = new SqlDataAdapter(StrSql, conection);
            //DataSet DS = new DataSet();
            //SDA.Fill(DS, TableName );
            //DataTable DT = new DataTable();
            //SDA.Fill(DT,"groh");
            //DataGridView Grid = new DataGridView();
            //Grid.DataSource = DS.Tables[TableName].DefaultView;

        }
        public static void Insert(string Etesal, string StrSql)
        {
            SqlConnection conection = new SqlConnection(Etesal);
            conection.Open();
            SqlCommand Comand = new SqlCommand(StrSql, conection);
            try
            {
                Comand.ExecuteNonQuery();
                MessageBox.Show("اطلاعات مورد نظر ثبت شد","اضافه کردن");
            }
            catch 
            {
             MessageBox.Show("خطا در اضافه کردن اطلاعات", "خطا",MessageBoxButtons .OK, MessageBoxIcon.Exclamation);
            }
            conection.Close();
            
        }

        public static void Jostejo(string Etesal,string StrSql,int Tedad, DataGridView Grid)
        {
            SqlConnection conection = new SqlConnection(Etesal);
            conection.Open();
            SqlCommand Comand = new SqlCommand(StrSql, conection);
            SqlDataReader dr = Comand.ExecuteReader();
            Grid.Rows.Clear();
            object[] Obj = new object[Tedad];
            while (dr.Read())
            {
                dr.GetValues(Obj);
                Grid.Rows.Add(Obj);
            }
            conection.Close();
        }
        public static void Delet(string Etesal, string StrSql,TextBox TxtBox1,TextBox TxtBox2,string StrSqlDELETE)
        {
 
            SqlConnection Conection = new SqlConnection(Etesal );
            Conection.Open();
            SqlCommand Comand = new SqlCommand(StrSql, Conection );
            SqlDataReader dr = Comand.ExecuteReader();
            dr.Read();
            object[] O = new object[4];

            try
            {
                dr.GetValues(O);

                str1 = O[0].ToString();
                str2 = O[1].ToString();
                    

            }
            catch { }
            Conection.Close();
            Conection.Open();
            if (str1==TxtBox1.Text && str2==TxtBox2 .Text )
            {
                Comand.CommandText = StrSqlDELETE;


                try
                {
                    Comand.ExecuteNonQuery();
                    MessageBox.Show("رکوردها با موفقیت حذف شدند");
                }
                catch
                {
                    MessageBox.Show("خطا در حذف اطلاعات");
                }
            }
            else
            {
                MessageBox.Show("چنين مشخصاتي وجود ندارد", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            Conection.Close();
        }
        public static void UpDate(string Etesal, string StrSql)
        {
            SqlConnection conection = new SqlConnection(Etesal);
            conection.Open();
            SqlCommand Comand = new SqlCommand(StrSql, conection);
         // try
           // {
                Comand.ExecuteNonQuery();
                MessageBox.Show("اطلاعات مورد نظر ویرایش شد", "ویرایش کردن");
            //}
            //catch 
            //{
             // MessageBox.Show("خطا در ویرایش کردن اطلاعات", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            //}
            conection.Close();
        }

        public static void ComboAddItem(string Etesal, string StrSql, int Tedad, ComboBox ComboName)
        {
            SqlConnection conection = new SqlConnection(Etesal);
            conection.Open();
            SqlCommand Comand = new SqlCommand(StrSql, conection);
            SqlDataReader dr = Comand.ExecuteReader();
            object[] Obj = new object[Tedad];
            while (dr.Read())
            {
                dr.GetValues(Obj);
                ComboName.Items.Add(Obj[1]);
            }
            conection.Close();
           
        }
       

        public static void FindNumberORpassWord(string Etesal,string StrSql)
        {
            SqlConnection conection = new SqlConnection(Etesal);
            conection.Open();
            SqlCommand Comand = new SqlCommand(StrSql, conection);
            SqlDataReader dr = Comand.ExecuteReader();
            object[] Obj = new object[1];
            while (dr.Read())
            {
                dr.GetValues(Obj);
            }
            LastNumber = Obj[0].ToString();
            PassWord = Obj[0].ToString();
            conection.Close();
        }
        public static void ShowKeyPress(DataGridView DataGridName,TextBox TextMabda,TextBox Maghsad)
        {

            for (int i = 0; i < DataGridName.Rows.Count; i++)
            {
                if (DataGridName.Rows[i].Cells[0].Value.ToString() == TextMabda.Text)
                {
                    Maghsad.Text = DataGridName.Rows[i].Cells[1].Value.ToString();
                    break;
                }
                else
                {
                    Maghsad.Text = "";
                }
            }
        }



         }
public static  class mask 
    {
        public static void IntText(KeyPressEventArgs R)
        {
            if (R.KeyChar >32)
                if (char.IsDigit(R.KeyChar) == false)
                {
                    R.KeyChar = Convert.ToChar(0);

                }
    }

        public static void StrText(KeyPressEventArgs R)
        {
            if (R.KeyChar > 32)
                if (char.IsLetter(R.KeyChar) == false)
                {
                    R.KeyChar = Convert.ToChar(0);

                }
        }

        
    }
    public class Variables
    {
        public static string masir = "server=(local);database=dabirkhane;integrated security=true";
       //public static string masir = "Data Source=" + Application.StartupPath + ";Initial Catalog=dabirkhane;Integrated Security=True";
        public static string StrCommandTel = "select * from tel";
        public static string StrCommandMokateb = "select * from mokateb";
        public static string StrCommandGroh = "select * from groh";
        public static string StrCommandErsali = "select * from ersali";
        public static string StrCommandBazyabiErsali = "select * from bazyabiersali";
        public static string StrCommandBazyabiDaryafti = "select * from bazyabidaryafti";
        public static string StrCommandDaryafti = "select * from daryafti";
        public static string StrCommandRamz = "select * from ramz";
        public static string LastNumber;
        public static string PassWord;
        public static string List;
        public static string str1, str2;
        public static string tab;
        public static string tab2;
        public static bool REPbool;
        public static string TableName;
        public static string GozareshComand;





    }
}





