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.Security.Cryptography;
namespace Project_management_system
{
    public partial class frmProjectManagement : Form
    {
        DataTable dtable = new DataTable();
        SqlConnection con = null;
        SqlCommand cmd = null;
        ConnectionString cs = new ConnectionString();
        SqlDataAdapter adp;
        DataSet ds;
        SqlDataReader rdr;
        clsFunc cf = new clsFunc();
        string st1;
        string st2;
        public frmProjectManagement()
        {
            InitializeComponent();
        }
        private void AutocompleteProjectName()
        {
            try
            {

                con = new SqlConnection(cs.ConDB);
                con.Open();


                string ct = "select distinct RTRIM(Projectname) from Client  ";

                cmd = new SqlCommand(ct);
                cmd.Connection = con;

                rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    txtProjectName.Items.Add(rdr[0]);
                }
                con.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public void FillDesignation()
        {

            try
            {
                con = new SqlConnection(cs.ConDB);
                con.Open();
                adp = new SqlDataAdapter();
                adp.SelectCommand = new SqlCommand("SELECT distinct RTRIM(Designation) FROM Employee", con);
                ds = new DataSet("ds");
                adp.Fill(ds);
                dtable = ds.Tables[0];
                Designation.Items.Clear();
                foreach (DataRow drow in dtable.Rows)
                {
                    Designation.Items.Add(drow[0].ToString());

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void auto()
        {
            txtProjectID.Text = "P-" + GetUniqueKey(5);
        }
        private void frmProjectManagement_Load(object sender, EventArgs e)
        {
            AutocompleteProjectName();
            Delete.Enabled = true;
            //FillCategory();  
            FillStage();
            FillEmployee_PM();
            FillDesignation();
        }
        private void Reset()
        {
            txtProjectName.Text = "";
            txtProjectManager.Text = "";
            txtProjectID.Text = "";
            txtStageID.Text = "";
            txtCategoryID.Text = "";
            dtpStartingDate.Text = DateTime.Today.ToString();
            dtpCompletionDate.Text = DateTime.Today.ToString();
            cmbCategory.Text = "";
            cmbProjectManager.Text = "";
            cmbStage.Text = "";
            txtDescription.Text = "";
            txtMembers.Text = "";
            txtTeamLeaders.Text = "";
            ListView1.Items.Clear();
            Save.Enabled = true;
            Delete.Enabled = false;
            btnUpdate.Enabled = false;
            ListView1.Enabled = true;
            btnAdd.Enabled = true;
            dataGridView2.Visible = false;
            Designation.SelectedIndex = -1;


        }

        private void NewRecord_Click(object sender, EventArgs e)
        {
            Reset();
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            {
                if (MessageBox.Show("Do you really want to delete this record?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    delete_records();
                }
            }
        }
        private void delete_records()
        {

            try
            {

                int RowsAffected = 0;
                con = new SqlConnection(cs.ConDB);
                con.Open();
                string cq1 = "delete from projectDetails where ProjectID='" + txtProjectID.Text + "'";
                cmd = new SqlCommand(cq1);
                cmd.Connection = con;
                RowsAffected = cmd.ExecuteNonQuery();
                con.Close();
                con = new SqlConnection(cs.ConDB);
                con.Open();
                string cq = "delete from ProjectInfo where ID='" + txtProjectID.Text + "'";
                cmd = new SqlCommand(cq);
                cmd.Connection = con;
                RowsAffected = cmd.ExecuteNonQuery();
                if (RowsAffected > 0)
                {
                    st1 =lblUser.Text;
                    st2 = "deleteded the Project '" + txtProjectName.Text + "'";
                    cf.LogFunc(st1, System.DateTime.Now, st2);
                    MessageBox.Show("Successfully deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Reset();
                }
                else
                {
                    MessageBox.Show("No Record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Reset();
                }
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public void FillCategory() 
        {

            try
            {
                con = new SqlConnection(cs.ConDB);
                con.Open();
                adp = new SqlDataAdapter();
                adp.SelectCommand = new SqlCommand("SELECT distinct RTRIM(CategoryName) FROM Category", con);
                ds = new DataSet("ds");
                adp.Fill(ds);
                dtable = ds.Tables[0];
                cmbCategory.Items.Clear();
                foreach (DataRow drow in dtable.Rows)
                {
                    cmbCategory.Items.Add(drow[0].ToString());

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public void FillStage() 
        {

            try
            {
                con = new SqlConnection(cs.ConDB);
                con.Open();
                adp = new SqlDataAdapter();
                adp.SelectCommand = new SqlCommand("SELECT distinct RTRIM(StageName) FROM Stages", con);  
                ds = new DataSet("ds");
                adp.Fill(ds);
                dtable = ds.Tables[0];
                cmbStage.Items.Clear();
                foreach (DataRow drow in dtable.Rows)
                {
                    cmbStage.Items.Add(drow[0].ToString());

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public void FillEmployee_PM() 
        {

            try
            {
                con = new SqlConnection(cs.ConDB);
                con.Open();
                adp = new SqlDataAdapter();
                adp.SelectCommand = new SqlCommand("SELECT distinct RTRIM(Name) FROM Employee where Designation='Project Manager'", con); 
                ds = new DataSet("ds");
                adp.Fill(ds);
                dtable = ds.Tables[0];
                cmbProjectManager.Items.Clear();
                foreach (DataRow drow in dtable.Rows)
                {
                    cmbProjectManager.Items.Add(drow[0].ToString());

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Designation_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                dataGridView2.Visible = false;
                con = new SqlConnection(cs.ConDB);
                con.Open();
                String sql = "SELECT RTRIM(ID),RTRIM(Name),RTRIM(Designation),RTRIM(Department) from Employee where Designation='" + Designation.Text + "' order by Name";
                cmd = new SqlCommand(sql, con);
                rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                dataGridView1.Rows.Clear();
                while (rdr.Read() == true)
                {
                    dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3]);
                }
                con.Close();
                if (Designation.Text == "Team Leader")
                {
                    dataGridView2.Visible = true;
                    con = new SqlConnection(cs.ConDB);
                    con.Open();
                    String sql1 = "SELECT RTRIM(ID),RTRIM(Name),RTRIM(Designation),RTRIM(Department) from Employee where Designation='" + Designation.Text + "' order by Name";
                    cmd = new SqlCommand(sql1, con);
                    rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    dataGridView2.Rows.Clear();
                    while (rdr.Read() == true)
                    {
                        dataGridView2.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3]);
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


     
        private void cmbCategory_SelectedIndexChanged(object sender, EventArgs e)  
        {
            try
            {
                con = new SqlConnection(cs.ConDB);

                con.Open();
                cmd = con.CreateCommand();
                cmd.CommandText = "SELECT ID from Category WHERE CategoryName = '" + cmbCategory.Text + "'";  
                rdr = cmd.ExecuteReader();

                if (rdr.Read())
                {
                    txtCategoryID.Text = rdr.GetValue(0).ToString().Trim();
                }
                if ((rdr != null))
                {
                    rdr.Close();
                }
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cmbProjectManager_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                con = new SqlConnection(cs.ConDB);

                con.Open();
                cmd = con.CreateCommand();
                cmd.CommandText = "SELECT ID from Employee WHERE Name = '" + cmbProjectManager.Text + "'";
                rdr = cmd.ExecuteReader();

                if (rdr.Read())
                {
                    txtProjectManager.Text = rdr.GetValue(0).ToString().Trim();
                }
                if ((rdr != null))
                {
                    rdr.Close();
                }
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cmbStage_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                con = new SqlConnection(cs.ConDB);

                con.Open();
                cmd = con.CreateCommand();
                cmd.CommandText = "SELECT ID from Stages WHERE StageName = '" + cmbStage.Text + "'";
                rdr = cmd.ExecuteReader();

                if (rdr.Read())
                {
                    txtStageID.Text = rdr.GetValue(0).ToString().Trim();
                }
                if ((rdr != null))
                {
                    rdr.Close();
                }
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {

            try
            {
                if (cmbStage.Text == "")
                {
                    MessageBox.Show("Please select stage", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    cmbStage.Focus();
                    return;
                }

                if (txtMembers.Text == "")
                {
                    MessageBox.Show("Please retrieve Members from gridview by press enter button(Control+mouseClick+enter for many members)", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (txtTeamLeaders.Text == "")
                {
                    MessageBox.Show("Please retrieve team leader from Gridview by press enter button(Control+mouseClick+enter for many members)", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (ListView1.Items.Count == 0)  
                {

                    ListViewItem lst = new ListViewItem();  
                    lst.SubItems.Add(txtStageID.Text);      
                    lst.SubItems.Add(cmbStage.Text);
                    lst.SubItems.Add(dtpStartingDate.Text);
                    lst.SubItems.Add(dtpCompletionDate.Text);
                    lst.SubItems.Add(txtMembers.Text);
                    lst.SubItems.Add(txtTeamLeaders.Text);
                    lst.SubItems.Add(txtDescription.Text);
                    ListView1.Items.Add(lst);

                    cmbStage.Text = "";
                    txtStageID.Text = "";
                    txtMembers.Text = "";
                    txtTeamLeaders.Text = "";
                    txtDescription.Text = "";
                    dtpStartingDate.Text = System.DateTime.Today.ToString();
                    dtpCompletionDate.Text = System.DateTime.Today.ToString();  
                    return;
                }

                for (int j = 0; j <= ListView1.Items.Count - 1; j++)
                {
                    if (ListView1.Items[j].SubItems[1].Text == txtStageID.Text)
                    {
                        MessageBox.Show("Stage already added", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        cmbStage.Text = "";
                        txtStageID.Text = "";
                        txtMembers.Text = "";
                        txtTeamLeaders.Text = "";
                        txtDescription.Text = "";
                        dtpStartingDate.Text = System.DateTime.Today.ToString();
                        dtpCompletionDate.Text = System.DateTime.Today.ToString();
                        return;

                    }
                }

                ListViewItem lst1 = new ListViewItem();  
                lst1.SubItems.Add(txtStageID.Text);
                lst1.SubItems.Add(cmbStage.Text);
                lst1.SubItems.Add(dtpStartingDate.Text);
                lst1.SubItems.Add(dtpCompletionDate.Text);
                lst1.SubItems.Add(txtMembers.Text);
                lst1.SubItems.Add(txtTeamLeaders.Text);
                lst1.SubItems.Add(txtDescription.Text);
                ListView1.Items.Add(lst1);

                cmbStage.Text = "";    
                txtStageID.Text = "";
                txtMembers.Text = "";
                txtTeamLeaders.Text = "";
                txtDescription.Text = "";
                dtpStartingDate.Text = System.DateTime.Today.ToString();
                dtpCompletionDate.Text = System.DateTime.Today.ToString();
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        public static string GetUniqueKey(int maxSize)
        {
            char[] chars = new char[62]; 
            chars = "123456789".ToCharArray();
            byte[] data = new byte[1];
            RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
            crypto.GetNonZeroBytes(data);
            data = new byte[maxSize];
            crypto.GetNonZeroBytes(data);
            StringBuilder result = new StringBuilder(maxSize);
            foreach (byte b in data)
            {
                result.Append(chars[b % (chars.Length)]);
            }
            return result.ToString();
        }
        private void Save_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtProjectName.Text == "")
                {
                    MessageBox.Show("Please enter Project name", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtProjectName.Focus();
                    return;
                }

                if (cmbCategory.Text == "")
                {
                    MessageBox.Show("Please select category", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    cmbCategory.Focus();
                    return;
                }

                if (cmbProjectManager.Text == "")
                {
                    MessageBox.Show("Please select project manager", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    cmbProjectManager.Focus();
                    return;
                }
                if (ListView1.Items.Count == 0)
                {
                    MessageBox.Show("sorry no product added", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                auto();
                con = new SqlConnection(cs.ConDB);

                con = new SqlConnection(cs.ConDB);
                con.Open();
                string cb = "insert Into ProjectInfo(ID, ProjectName, CategoryID, EmployeeID_PM) VALUES ('" + txtProjectID.Text + "','" + txtProjectName.Text + "','" + txtCategoryID.Text + "','" + txtProjectManager.Text + "')";
                cmd = new SqlCommand(cb);
                cmd.Connection = con;
                cmd.ExecuteReader();
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
                con.Close();


                for (int i = 0; i <= ListView1.Items.Count - 1; i++)
                {
                    con = new SqlConnection(cs.ConDB);

                    string cd = "insert Into ProjectDetails(ProjectID, StageID, StartingDate, CompletionDate, Employee_members, Employee_teamleaders, Description) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7)";
                    cmd = new SqlCommand(cd);
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@d1", txtProjectID.Text);
                    cmd.Parameters.AddWithValue("@d2", ListView1.Items[i].SubItems[1].Text);
                    cmd.Parameters.AddWithValue("@d3", ListView1.Items[i].SubItems[3].Text);
                    cmd.Parameters.AddWithValue("@d4", ListView1.Items[i].SubItems[4].Text);
                    cmd.Parameters.AddWithValue("@d5", ListView1.Items[i].SubItems[5].Text);
                    cmd.Parameters.AddWithValue("@d6", ListView1.Items[i].SubItems[6].Text);
                    cmd.Parameters.AddWithValue("@d7", ListView1.Items[i].SubItems[7].Text);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }

                Save.Enabled = false;
                st1 = lblUser.Text;
                st2 = "added the new Project '" + txtProjectName.Text + "'";
                cf.LogFunc(st1, System.DateTime.Now, st2);
                MessageBox.Show("Successfully saved", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dataGridView2_KeyDown(object sender, KeyEventArgs e)
        {

            if ((e.KeyCode == Keys.Enter))  
            {


                if (dataGridView2.SelectedRows != null)
                {
                    txtTeamLeaders.Text = "";
                    bool firstvalue = true;
                    foreach (DataGridViewRow drv in dataGridView2.SelectedRows)  
                    {
                        if (!firstvalue)
                        {
                            txtTeamLeaders.Text += ",";
                        }
                        txtTeamLeaders.Text += drv.Cells[1].Value.ToString();
                        firstvalue = false;
                    }

                }

            }
        }

        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if ((e.KeyCode == Keys.Enter))
                {


                    if (dataGridView1.SelectedRows != null)
                    {
                        txtMembers.Text = "";
                        bool firstvalue = true;
                        foreach (DataGridViewRow drv in dataGridView1.SelectedRows)
                        {
                            if (!firstvalue)
                            {
                                txtMembers.Text += ",";
                            }
                            txtMembers.Text += drv.Cells[1].Value.ToString();
                            firstvalue = false;
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }



        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
          
            Project_management_Records frm = new Project_management_Records();
            frm.lblUser.Text = lblUser.Text;
            frm.Show();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                con = new SqlConnection(cs.ConDB);
                con.Open();

                string ct = "select projectID from ProjectDetails where projectID='" + txtProjectID.Text + "'";
                cmd = new SqlCommand(ct);
                cmd.Connection = con;
                rdr = cmd.ExecuteReader();

                if (rdr.Read())
                {
                    con = new SqlConnection(cs.ConDB);
                    con.Open();
                    string cb2 = "Update projectDetails set StageID= '" + txtStageID.Text + "',startingDate='" + dtpStartingDate.Text + "',CompletionDate ='" + dtpCompletionDate.Text + "',   Employee_members ='" + txtMembers.Text + "',Employee_Teamleaders ='" + txtTeamLeaders.Text + "',Description ='" + txtDescription.Text + "'where projectID = '" + txtProjectID.Text + "'";
                    cmd = new SqlCommand(cb2);
                    cmd.Connection = con;
                    cmd.ExecuteReader();
                    con.Close();

                }
                else
                {
                    con = new SqlConnection(cs.ConDB);
                    con.Open();
                    string cb1 = "insert into projectdetails (StageID,StartingDate,compleitionDate,Employee_members,employee_teamleaders,Description) VALUES ('" + txtStageID.Text + "'," + dtpStartingDate.Text + " ," + dtpCompletionDate.Text + ",'" + txtMembers.Text + "','" + txtTeamLeaders.Text + "','" + txtDescription.Text + "')";
                    cmd = new SqlCommand(cb1);
                    cmd.Connection = con;

                    cmd.ExecuteReader();
                    con.Close();
                }
                con = new SqlConnection(cs.ConDB);
                con.Open();

                string ct2 = "select ID from Projectinfo where ID='" + txtProjectID.Text + "'";
                cmd = new SqlCommand(ct2);
                cmd.Connection = con;
                rdr = cmd.ExecuteReader();

                if (rdr.Read())
                {
                    con = new SqlConnection(cs.ConDB);
                    con.Open();
                    string cb2 = "Update projectinfo set CategoryID= '" + txtCategoryID.Text + "',EmployeeID_PM ='" + txtProjectManager.Text + "' where ID = '" + txtProjectID.Text + "'";
                    cmd = new SqlCommand(cb2);
                    cmd.Connection = con;
                    cmd.ExecuteReader();
                    con.Close();

                }
                else
                {
                    con = new SqlConnection(cs.ConDB);
                    con.Open();
                    string cb1 = "insert into projectinfo (categoryID,EmployeeID_PM) VALUES ('" + txtCategoryID.Text + "','" + txtProjectManager.Text + "')";
                    cmd = new SqlCommand(cb1);
                    cmd.Connection = con;

                    cmd.ExecuteReader();
                    con.Close();
                }
                con = new SqlConnection(cs.ConDB);
                con.Open();
                string cb = "Update Employee set Name='" + cmbProjectManager.Text + "' where ID='" + txtProjectManager.Text + "'";
                cmd = new SqlCommand(cb);
                cmd.Connection = con;
                cmd.ExecuteReader();
                con.Close();
                con = new SqlConnection(cs.ConDB);
                con.Open();
                string p1 = "Update Category set  CategoryName='" + cmbCategory.Text + "' where ID='" + txtCategoryID.Text + "'";
                cmd = new SqlCommand(p1);
                cmd.Connection = con;
                cmd.ExecuteReader();
                con.Close();
                con = new SqlConnection(cs.ConDB);
                con.Open();
                string p2 = "Update Stages set StageName ='" + cmbStage.Text + "' where ID='" + txtStageID.Text + "'";
                cmd = new SqlCommand(p2);
                cmd.Connection = con;
                cmd.ExecuteReader();
                con.Close();
                st1 = lblUser.Text;
                st2 = "Updted the Project '" + txtProjectName.Text + "'";
                cf.LogFunc(st1, System.DateTime.Now, st2);
                MessageBox.Show("Successfully updated", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
                btnUpdate.Enabled = false;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            {
                try
                {
                    Cursor = Cursors.WaitCursor;
               
                    projectInvoice rpt = new projectInvoice();
            
                    cmd = new SqlCommand();
                    SqlDataAdapter myDA = new SqlDataAdapter();
                   DataSet  myDS = new DataSet();
              
                    con = new SqlConnection(cs.ConDB);
                    cmd.Connection = con;
                    cmd.CommandText = "SELECT ProjectDetails.StageID, ProjectDetails.ID, ProjectDetails.StartingDate, ProjectDetails.CompletionDate, ProjectDetails.Employee_members, ProjectDetails.Employee_teamleaders, ProjectInfo.ProjectName,ProjectInfo.CategoryID, Category.CategoryName, Stages.StageName FROM ProjectInfo INNER JOIN ProjectDetails ON ProjectInfo.ID = ProjectDetails.ProjectID INNER JOIN Category ON ProjectInfo.CategoryID = Category.Id INNER JOIN Stages ON ProjectDetails.StageID = Stages.Id where projectinfo.ID='"+txtProjectID.Text+"'";
                    cmd.CommandType = CommandType.Text;
                    myDA.SelectCommand = cmd;
                    myDA.Fill(myDS, "Stages");
                    myDA.Fill(myDS, "Category");
                    myDA.Fill(myDS, "Projectinfo");
                    myDA.Fill(myDS, "ProjectDetails");
                    rpt.SetDataSource(myDS);
                    projectsreport frm = new projectsreport();
                    frm.crystalReportViewer1.ReportSource = rpt;
                    frm.Visible = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void txtProjectName_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                cmbCategory.Items.Clear();
                cmbCategory.Text = "";
                cmbCategory.Enabled = true;
                cmbCategory.Focus();

                con = new SqlConnection(cs.ConDB);

                con.Open();
                cmd = con.CreateCommand();

                cmd.CommandText = "SELECT RTRIM(Category.categoryName) FROM Client,category WHERE category.id=client.categoryID and ProjectName = '" +txtProjectName.Text+ "'";
                rdr = cmd.ExecuteReader();

                if (rdr.Read())
                {

                    cmbCategory.Items.Add(rdr[0]);
                    
                }


                if ((rdr != null))
                {
                    rdr.Close();
                }
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dataGridView2_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            string strRowNumber = (e.RowIndex + 1).ToString();
            SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);
            if (dataGridView2.RowHeadersWidth < Convert.ToInt32((size.Width + 20)))
            {
                dataGridView2.RowHeadersWidth = Convert.ToInt32((size.Width + 20));
            }
            Brush b = SystemBrushes.ControlText;
            e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
     
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            string strRowNumber = (e.RowIndex + 1).ToString();
            SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);
            if (dataGridView1.RowHeadersWidth < Convert.ToInt32((size.Width + 20)))
            {
                dataGridView1.RowHeadersWidth = Convert.ToInt32((size.Width + 20));
            }
            Brush b = SystemBrushes.ControlText;
            e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
     
        }
    }
}

     

        
        
        

    
