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;
using System.IO;

namespace AzmoonGir
{
    public partial class FrmAzmoonKatbi : Form
    {
        SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\DbAzmoon.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
        DataTable dtQuestionlist;
        SqlDataAdapter da;

        public FrmAzmoonKatbi()
        {
            InitializeComponent();
        }

        private void FrmAzmoonKatbi_Load(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("SelectAllGroup", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            da.Fill(dt);
            CmbGroups.DataSource = dt;
            CmbGroups.DisplayMember = "GroupName";
            CmbGroups.ValueMember = "GroupID";






        }

        private void TxtTedad_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt16(e.KeyChar) <= 57 & Convert.ToInt16(e.KeyChar) >= 48 || Convert.ToInt16(e.KeyChar) == 8)
            {
            }
            else
            {
                e.KeyChar = Convert.ToChar(Keys.None);
            }
        }

        private void BtnAzmoon_Click(object sender, EventArgs e)
        {

        
            
            if (string.IsNullOrWhiteSpace(TxtTedad.Text))
            {

                errorProvider1.SetError(TxtTedad, "تعداد سوالات را وارد کنید");
                return;

            }

            if (TxtTedad.Text == "0")
            {

                errorProvider1.SetError(TxtTedad, "تعداد سوالات را وارد کنید");
                return;

            }


            errorProvider1.SetError(TxtTedad, "");

           // SqlConnection con = new SqlConnection("server = .\\SQLEXPRESS;DataBase = DbAzmoon;Integrated Security = True");
            SqlCommand cmd = new SqlCommand("CheckQuestionNumber", con);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@groupid", CmbGroups.SelectedValue);
            cmd.Parameters.AddWithValue("@questionnumber", TxtTedad.Text);

            cmd.Parameters.Add("@result", SqlDbType.Int);
            cmd.Parameters["@result"].Direction = ParameterDirection.Output;

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            int res = Convert.ToInt32(cmd.Parameters["@result"].Value.ToString());



            Frm_Message Fm = new Frm_Message();

            if (res == 0)
            {
                Fm.lbltext.Text = "تعداد سوالات مد نظر بیشتر از تعداد سوالات موجود در بانک است";
                Fm.ImgClose.Visible = true;
                Fm.ImgYes.Visible = false;
                Fm.ImgNo.Visible = false;
                Fm.ShowDialog();
            }
            else
            {

            


                da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                dtQuestionlist = new DataTable();
                da.Fill(dtQuestionlist);



             




                stiReportAzmoon.Load("stiReportAzmoon.mrt");
                stiReportAzmoon.RegData(dtQuestionlist);
              //  stiReportAzmoon.Dictionary.Synchronize();
              //  stiReportAzmoon.Compile();
                stiReportAzmoon.Show();

                //this.Close();

            }

 
        }
    }
}
