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

namespace amlakyar
{
    public partial class frm_etelat_kharidar_va_foroshande : Form
    {
        static SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\amlak.mdf;integrated security=true;User Instance=True");
        SqlDataAdapter da = new SqlDataAdapter("", con);
        DataSet ds = new DataSet();


        string s1;

        public frm_etelat_kharidar_va_foroshande()
        {
            InitializeComponent();
        }

       

        

        private void frm_etelat_kharidar_va_foroshande_Load(object sender, EventArgs e)
        {
            da.SelectCommand.CommandText = "select * from kharidar_forushande order by code";

            da.Fill(ds, "h");

            DGkharidar_forushande.DataSource = ds;
            DGkharidar_forushande.DataMember = "h";
            DGkharidar_forushande.Columns[0].HeaderText = "كد";
            DGkharidar_forushande.Columns[1].HeaderText = "نام";
            DGkharidar_forushande.Columns[2].HeaderText = "تلفن";
            DGkharidar_forushande.Columns[3].HeaderText = "آدرس";
        }

        

       

    

        private void DGkharidar_forushande_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            s1 = Convert.ToString(DGkharidar_forushande.CurrentRow.Cells["code"].Value);
            da.SelectCommand.CommandText = "select * from kharidar_forushande where code='" + s1 + "'";
            da.Fill(ds, "q");
            txtdel.Text = Convert.ToString(DGkharidar_forushande.CurrentRow.Cells["code"].Value);

            textBox1.Text = Convert.ToString(DGkharidar_forushande.CurrentRow.Cells["code"].Value);
            txtname.Text = Convert.ToString(DGkharidar_forushande.CurrentRow.Cells["name"].Value);
            txttel.Text = Convert.ToString(DGkharidar_forushande.CurrentRow.Cells["tel"].Value);
            txtaddress.Text = Convert.ToString(DGkharidar_forushande.CurrentRow.Cells["address"].Value);
        }

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

        private void button3_Click_1(object sender, EventArgs e)
        {
            string c;

            Int16 i;
            SqlDataAdapter da = new SqlDataAdapter("", con);
            if (txtname.Text != "" && txttel.Text != "" && txtaddress.Text != "")
            {
                ds.Clear();
                da.SelectCommand.CommandText = "select code from kharidar_forushande";
                da.Fill(ds, "n");
                if (ds.Tables["n"].Rows.Count > 0)
                {
                    da.SelectCommand.CommandText = "select max(code) as id from kharidar_forushande";
                    da.Fill(ds, "v");
                    dg5.DataSource = ds;
                    dg5.DataMember = "v";

                    i = Convert.ToInt16(dg5.CurrentRow.Cells["id"].Value);
                    i++;
                }
                else
                {
                    i = 1;
                }
                c = "insert into kharidar_forushande(name,tel,address,code) values ('" + txtname.Text + "'," + txttel.Text + ", '" + txtaddress.Text + "', " + i + " )";
                SqlCommand cmd = new SqlCommand(c, con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();


                da.SelectCommand.CommandText = "select * from kharidar_forushande order by code";
                da.Fill(ds, "h");
                DGkharidar_forushande.DataSource = ds;
                DGkharidar_forushande.DataMember = "h";



                MessageBox.Show("اطلاعات با موفقیت ثبت شد");
            }
            else { MessageBox.Show("لطفا فيلد ها را به دقت وارد نماييد"); }
        }

        private void Button1_Click_1(object sender, EventArgs e)
        {
            string d;

            d = "delete from kharidar_forushande where code='" + txtdel.Text + "'";
            SqlCommand cmd = new SqlCommand(d, con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            ds.Clear();
            da.SelectCommand.CommandText = "select * from kharidar_forushande";
            da.Fill(ds, "h9");
            DGkharidar_forushande.DataSource = ds;
            DGkharidar_forushande.DataMember = "h9";
            DGkharidar_forushande.Columns[0].HeaderText = "كد";
            DGkharidar_forushande.Columns[1].HeaderText = "نام";
            DGkharidar_forushande.Columns[2].HeaderText = "تلفن";
            DGkharidar_forushande.Columns[3].HeaderText = "آدرس";
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            string p;

            p = "update  kharidar_forushande  set name='" + txtname.Text + "',tel=" + txttel.Text + ",address='" + txtaddress.Text + "' where code='" + textBox1.Text + "'";
            SqlCommand cmd = new SqlCommand(p, con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            ds.Clear();
            da.SelectCommand.CommandText = "select * from kharidar_forushande";
            da.Fill(ds, "x");
            DGkharidar_forushande.DataSource = ds;
            DGkharidar_forushande.DataMember = "x";
        }
    }
}