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 WindowsFormsApplication8
{
    public partial class FrmEdit : Form
    {
        SqlConnection con;
        SqlDataAdapter ada = new SqlDataAdapter();
        DataSet ds = new DataSet();

        private string idnum;
        public string idnum_
        {
            set
            {
                idnum = value;
            }
        }

        public FrmEdit()
        {
            InitializeComponent();
        }

        private void FrmEdit_Load(object sender, EventArgs e)
        {
            MinimizeBox = false;
            MaximizeBox = false;

            ConnectionInfo info = new ConnectionInfo();
            info.ConnectDB_Info();
            con = new SqlConnection(ConnectionInfo.ConnectionString);

            ada.SelectCommand = new SqlCommand("select * from Telltbl where ID=" + idnum , con);

            SqlCommandBuilder b = new SqlCommandBuilder(ada);
            ada.InsertCommand = b.GetInsertCommand();
            ada.DeleteCommand = b.GetDeleteCommand();
            ada.UpdateCommand = b.GetUpdateCommand();

            ada.Fill(ds);

            comboTitle.SelectedItem = "آقا";


            txtName.DataBindings.Add(new Binding("Text", ds.Tables[0], "TName"));
            txtLname.DataBindings.Add(new Binding("Text", ds.Tables[0], "TLname"));
            txtCompany.DataBindings.Add(new Binding("Text", ds.Tables[0], "TCompany"));
            txtTell.DataBindings.Add(new Binding("Text", ds.Tables[0], "TTell"));
            txtMobile.DataBindings.Add(new Binding("Text", ds.Tables[0], "TMobile"));
            txtFax.DataBindings.Add(new Binding("Text", ds.Tables[0], "TFax"));
            txtAdress.DataBindings.Add(new Binding("Text", ds.Tables[0], "TAdress"));
            txtEmail.DataBindings.Add(new Binding("Text", ds.Tables[0], "Email"));
            comboTitle.DataBindings.Add(new Binding("SelectedItem", ds.Tables[0], "TTitle"));

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            ds.Tables[0].Rows[BindingContext[ds.Tables[0]].Position].Delete();
            ada.Update(ds);
            this.Close();
        }

        private void btnSave_Click_1(object sender, EventArgs e)
        {
            BindingContext[ds.Tables[0]].EndCurrentEdit();
            ada.Update(ds);
            this.Close();
        }

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

        private void txtTell_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b')  //Back Space
                return;

            if (e.KeyChar < '0' || e.KeyChar > '9')
            {
                e.Handled = true;
                Console.Beep();
            }
        }

        private void txtFax_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b')  //Back Space
                return;

            if (e.KeyChar < '0' || e.KeyChar > '9')
            {
                e.Handled = true;
                Console.Beep();
            }
        }

        private void txtMobile_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b')  //Back Space
                return;

            if (e.KeyChar < '0' || e.KeyChar > '9')
            {
                e.Handled = true;
                Console.Beep();
            }
        }
    }
}
