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

namespace projectcsharp
{
    public partial class frm_rep_customer : Form
    {
        public frm_rep_customer()
        {
            InitializeComponent();
        }
        public SqlConnection con = new SqlConnection("server=.;database=resturan;Integrated Security=True");

        private void txt_name_Enter(object sender, EventArgs e)
        {
            System.Globalization.CultureInfo fa;
            fa = InputLanguage.InstalledInputLanguages[1].Culture;
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(fa);
        }

        private void txt_family_Enter(object sender, EventArgs e)
        {
            System.Globalization.CultureInfo fa;
            fa = InputLanguage.InstalledInputLanguages[1].Culture;
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(fa);
        }

        private void txt_address_Enter(object sender, EventArgs e)
        {
            System.Globalization.CultureInfo fa;
            fa = InputLanguage.InstalledInputLanguages[1].Culture;
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(fa);
        }

        private void btn_back_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txt_tell_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((char.IsDigit(e.KeyChar) == false) && (e.KeyChar != (char)Keys.Back) && (e.KeyChar != (char)Keys.Delete))
            {
                e.Handled = true;
            }
        }

        private void txt_codemeli_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((char.IsDigit(e.KeyChar) == false) && (e.KeyChar != (char)Keys.Back) && (e.KeyChar != (char)Keys.Delete))
            {
                e.Handled = true;
            }
        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            string str = "";
            bool bol = false;
            if(txt_name.Text !="")
            {
                if(bol==true)
                    str += "and name=N'" + txt_name.Text + "'";
                else
                    str += "name=N'" + txt_name.Text + "'";
                bol=true;
            }
            if (txt_family.Text != "")
            {
                if (bol == true)
                    str += "and family=N'" + txt_family.Text + "'";
                else
                    str += "family=N'" + txt_family.Text + "' ";
                bol = true;
            }
            if (txt_codemeli.Text != "")
            {
                if (bol == true)
                    str += "and codemeli= '" + txt_codemeli.Text + " '";
                else
                    str += " codemeli = '" + txt_codemeli.Text + "' ";
                bol = true;
            }
            if (txt_tell.Text != "")
            {
                if (bol == true)
                    str += " and tell= '" + txt_tell.Text + "' ";
                else
                    str += " tell = '" + txt_tell.Text + "' ";
                bol = true;
            }
            if (txt_address.Text != "")
            {
                if (bol == true)
                    str += " and address=N'" + txt_address.Text + "' ";
                else
                    str += "address=N '" + txt_address.Text + "' ";
                bol = true;
            }
            try
            {
                str = "select name as 'نام',family as [نام خانوادگی],codemeli as 'کدملی',tell as 'تلفن',address as 'آدرس' from tbl_customer where " + str;
                SqlDataAdapter da = new SqlDataAdapter(str, con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
            catch
            {
                MessageBox.Show("free textboxs");
            } 
        }

    }
}
