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 MyDataAccess;

namespace Amozeshgah
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        cDataAccess cData = new cDataAccess(clsPath.ConnectionString());

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            cData.Sql_Command.Parameters.AddWithValue("@Name", txtName.Text);
            cData.Sql_Command.Parameters.AddWithValue("@Family", txtFamily.Text);
            cData.Sql_Command.Parameters.AddWithValue("@TarikhTavalod", txtTarikhTavalod.Text);
            cData.Sql_Command.Parameters.AddWithValue("@CodeMeli", txtCodeMeli.Text);
            cData.Sql_Command.Parameters.AddWithValue("@Jensiyat", cmbJen.Text);
            cData.Sql_Command.Parameters.AddWithValue("@Addres", txtAddres.Text);
            if (picUser.Image == null)
            {
                cData.Sql_Command.Parameters.Add("@Image", SqlDbType.Image).Value = DBNull.Value;
            }
            else
            {
                cData.Sql_Command.Parameters.AddWithValue("@Image", clsPic.ImageToByteArray(picUser.Image));

            }


            string sql = "INSERT INTO [tUsers]([Name],[Family],[TarikhTavalod],[CodeMeli],[Jensiyat],[Addres],[Image])VALUES(@Name,@Family,@TarikhTavalod,@CodeMeli,@Jensiyat,@Addres,@Image)";

            cData.ExecuteNonQuery(sql);
            cData.Sql_Command.Parameters.Clear();

            txtName.Text = "";
            txtFamily.Text = "";
            txtTarikhTavalod.Text = "";
            txtCodeMeli.Text = "";
            cmbJen.Text = "";
            txtAddres.Text = "";
            picUser.Image = null;
           
 
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            OpenFileDialog Ofd = new OpenFileDialog();
            Ofd.Multiselect = false;
            Ofd.Filter = "Image | *.jpg";
            if (Ofd.ShowDialog() == DialogResult.OK)
            {
                picUser.Image = Image.FromFile(Ofd.FileName);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            picUser.Image = null;
        }
    }
}
