protected void SelectProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            DataSet   ds = new DataSet();

            #region Populating Product Type DropDown
            ProductType.Items.Add("Select Type");
            ProductType.Items.Add("Medicine (HAAD)");
            ProductType.Items.Add("Medicine (NON HAAD)");
            ProductType.Items.Add("Non Medicine");

            #endregion

            #region Getting Product Details
            try
            {
                DataView dv = new DataView();
                dv           = ProductSet.Tables[0].DefaultView;
                dv.RowFilter = "ProductID = '" + SelectProduct.SelectedValue.ToString() + "'";
                dt           = dv.ToTable();
                String Query = "Select tblSub_Category.Sub_CatID as SubcatID, tblSub_Category.Name AS SUBCAT,tblCategory.Name AS CAT,tblDepartment.Name AS DEPT From tblSub_Category INNER JOIN tblCategory ON tblSub_Category.CategoryID = tblCategory.CategoryID INNER JOIN tblDepartment ON tblCategory.DepartmentID = tblDepartment.DepId Where Sub_CatID = '" + dt.Rows[0]["SubCategoryID"] + "'";

                connection.Open();
                SqlCommand     command = new SqlCommand(Query, connection);
                SqlDataAdapter SA      = new SqlDataAdapter(command);
                SA.Fill(ds);
                if (ds.Tables[0].Rows[0]["SubcatID"] != DBNull.Value || !ds.Tables[0].Rows[0]["SubcatID"].Equals(""))
                {
                    Session["SubCatID"] = ds.Tables[0].Rows[0]["SubcatID"].ToString();
                }
                if (dt.Rows[0]["Product_Id_Org"] != DBNull.Value || !dt.Rows[0]["ItemCode"].Equals(""))
                {
                    BarCodeSerial.Text = dt.Rows[0]["Product_Id_Org"].ToString();
                }
                if (dt.Rows[0]["ItemCode"] != DBNull.Value || !dt.Rows[0]["ItemCode"].Equals(""))
                {
                    GreenRainCode.Text = dt.Rows[0]["ItemCode"].ToString();
                }
                if (dt.Rows[0]["Product_Name"] != DBNull.Value || !dt.Rows[0]["Product_Name"].Equals(""))
                {
                    ProductName.Text = dt.Rows[0]["Product_Name"].ToString();
                }
                if (dt.Rows[0]["Description"] != DBNull.Value || !dt.Rows[0]["Description"].Equals(""))
                {
                    ProdcutDesc.Text = dt.Rows[0]["Description"].ToString();
                }
                if (dt.Rows[0]["Brand_Name"] != DBNull.Value || !dt.Rows[0]["Brand_Name"].Equals(""))
                {
                    ProdcutBrand.Text = dt.Rows[0]["Brand_Name"].ToString();
                }
                if (dt.Rows[0]["DrugType"] != DBNull.Value)
                {
                    if (!dt.Rows[0]["DrugType"].Equals(""))
                    {
                        ProductType.SelectedValue = dt.Rows[0]["DrugType"].ToString();
                    }
                }

                //if (ds.Tables[0].Rows[0]["DEPT"] != DBNull.Value || !ds.Tables[0].Rows[0]["DEPT"].Equals(""))
                //{
                //    ProductDept.SelectedValue = ds.Tables[0].Rows[0]["DEPT"].ToString();
                //}

                if (ds.Tables[0].Rows[0]["CAT"] != DBNull.Value || !ds.Tables[0].Rows[0]["CAT"].Equals(""))
                {
                    ProductCat.SelectedValue = ds.Tables[0].Rows[0]["CAT"].ToString();
                }
                if (ds.Tables[0].Rows[0]["SUBCAT"] != DBNull.Value || !ds.Tables[0].Rows[0]["SUBCAT"].Equals(""))
                {
                    ProductSubCat.SelectedValue = ds.Tables[0].Rows[0]["SUBCAT"].ToString();
                }
                if (dt.Rows[0]["UnitCost"] != DBNull.Value || !ds.Tables[0].Rows[0]["UnitCost"].Equals(""))
                {
                    ProductCost.Text = dt.Rows[0]["UnitCost"].ToString();
                }
                if (dt.Rows[0]["SP"] != DBNull.Value || !ds.Tables[0].Rows[0]["SP"].Equals(""))
                {
                    ProductSale.Text = dt.Rows[0]["SP"].ToString();
                }
                if (dt.Rows[0]["MaxiMumDiscount"] != DBNull.Value || !ds.Tables[0].Rows[0]["MaxiMumDiscount"].Equals(""))
                {
                    ProductDiscount.Text = dt.Rows[0]["MaxiMumDiscount"].ToString();
                }
                if (dt.Rows[0]["ProductID"] != DBNull.Value || !ds.Tables[0].Rows[0]["ProductID"].Equals(""))
                {
                    Session["ProductID"] = dt.Rows[0]["ProductID"].ToString();
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                connection.Close();
            }
            #endregion

            #region Populating Product Department DropDown
            try
            {
                connection.Open();
                SqlCommand     command = new SqlCommand("Select * From tblDepartment", connection);
                DataSet        dsA     = new DataSet();
                SqlDataAdapter sA      = new SqlDataAdapter(command);
                sA.Fill(dsA);
                ProductDept.DataSource     = dsA.Tables[0];
                ProductDept.DataTextField  = "Name";
                ProductDept.DataValueField = "DepId";


                ProductDept.DataBind();
                if (ds.Tables[0].Rows[0]["DEPT"] != DBNull.Value || !ds.Tables[0].Rows[0]["DEPT"].Equals(""))
                {
                    ProductDept.SelectedIndex = -1; // ProductDept.Items.IndexOf(ProductDept.Items.FindByText(ds.Tables[0].Rows[0]["DEPT"].ToString()));
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                connection.Close();
            }
            #endregion
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // need to change now for new fcked up logic

                if (Session["MODE"].Equals("ADD"))
                {
                    btnCreateProduct.Text = "ADD";

                    #region Populating BarCode Serial
                    try
                    {
                        connection.Open();
                        SqlCommand     command = new SqlCommand("Select Count(*) From tbl_ProductMaster Where Product_Id_Org LIKE '2%';", connection);
                        DataSet        ds      = new DataSet();
                        SqlDataAdapter sA      = new SqlDataAdapter(command);
                        sA.Fill(ds);

                        if (ds.Tables[0].Rows[0][0].ToString().Length.Equals(7))
                        {
                            BarCodeSerial.Text = "2" + (Int32.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1).ToString();
                        }
                        else if (ds.Tables[0].Rows[0][0].ToString().Length.Equals(6))
                        {
                            BarCodeSerial.Text = "20" + (Int32.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1).ToString();
                        }
                        else if (ds.Tables[0].Rows[0][0].ToString().Length.Equals(5))
                        {
                            BarCodeSerial.Text = "200" + (Int32.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1).ToString();
                        }
                        else if (ds.Tables[0].Rows[0][0].ToString().Length.Equals(4))
                        {
                            BarCodeSerial.Text = "2000" + (Int32.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1).ToString();
                        }
                        else if (ds.Tables[0].Rows[0][0].ToString().Length.Equals(3))
                        {
                            BarCodeSerial.Text = "20000" + (Int32.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1).ToString();
                        }

                        else if (ds.Tables[0].Rows[0][0].ToString().Length.Equals(2))
                        {
                            BarCodeSerial.Text = "200000" + (Int32.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1).ToString();
                        }

                        else if (ds.Tables[0].Rows[0][0].ToString().Length.Equals(1))
                        {
                            BarCodeSerial.Text = "2000000" + (Int32.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1).ToString();
                        }
                        else if (ds.Tables[0].Rows[0][0].ToString().Length < 1)
                        {
                            BarCodeSerial.Text = "2000000" + (Int32.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1).ToString();
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    finally
                    {
                        connection.Close();
                    }
                    #endregion

                    BarCodeSerial.Visible = true;
                }
                else if (Session["MODE"].Equals("EDIT"))
                {
                    btnCreateProduct.Text    = "UPDATE";
                    BarCodeSerial.Visible    = false;
                    lblBarcodeSerial.Visible = false;
                }

                #region Master Search Mechanism
                if (Session["PageMasterProduct"] != null && Session["PageMasterProduct"].ToString() != null &&
                    Session["PageMasterProduct"].ToString() != "" && Session["PageMasterProduct"].ToString() != "false")
                {
                    FromMaster_Load(Session["MS_ItemNo"].ToString(), Session["MS_ItemName"].ToString(), Session["MS_ItemType"].ToString(), Session["MS_Manufacterer"].ToString(),
                                    Session["MS_Category"].ToString(), Session["MS_GenericName"].ToString(), Session["MS_Control"].ToString(), Session["MS_BinNumber"].ToString(),
                                    Session["MS_GreenRainCode"].ToString(), Session["MS_BrandName"].ToString(), Session["MS_MaxiMumDiscount"].ToString(), Session["MS_LineID"].ToString(),
                                    Session["MS_UnitSale"].ToString(), Session["MS_UnitCost"].ToString(), Session["MS_itemAWT"].ToString(), Session["MS_itemForm"].ToString(),
                                    Session["MS_itemStrength"].ToString(), Session["MS_itemPackType"].ToString(), Session["MS_itemPackSize"].ToString(), Session["MS_Description"].ToString());
                }
                #endregion


                #region Populating Product Type DropDown
                ProductType.Items.Add("Medicine(HAAD)");
                ProductType.Items.Add("Medicine(Non HAAD)");
                ProductType.Items.Add("NonMedicine");

                if (Session["MODE"].Equals("EDIT"))
                {
                    foreach (ListItem Items  in ProductType.Items)
                    {
                        if (Items.Text.Equals(Session["MS_ItemType"].ToString()))
                        {
                            ProductType.SelectedIndex = ProductType.Items.IndexOf(Items);
                            break;
                        }
                    }
                    //int selIndex;
                    //int.TryParse(Session["MS_ProductOrderType"].ToString(), out selIndex);
                    //ddlProductOrderType.SelectedIndex = selIndex;
                    // foreach( )
                }
                #endregion

                #region Populating Product Department DropDown
                try
                {
                    connection.Open();
                    SqlCommand     command = new SqlCommand("Select * From tblDepartment", connection);
                    DataSet        ds      = new DataSet();
                    SqlDataAdapter sA      = new SqlDataAdapter(command);
                    sA.Fill(ds);
                    ProductDept.DataSource     = ds.Tables[0];
                    ProductDept.DataTextField  = "Name";
                    ProductDept.DataValueField = "DepId";
                    ProductDept.DataBind();
                    if (ProductDept != null)
                    {
                        ProductDept.Items.Insert(0, "Select Department");
                        ProductDept.SelectedIndex = 0;
                    }
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    connection.Close();
                }
                #endregion

                #region Populate Product Order Type

                ddlProductOrderType.DataSource     = IMSGlobal.GetOrdersType();
                ddlProductOrderType.DataTextField  = "Name";
                ddlProductOrderType.DataValueField = "OrderTypeId";
                ddlProductOrderType.DataBind();

                if (ddlProductOrderType != null)
                {
                    ddlProductOrderType.Items.Insert(0, "Select Product Order Type");
                    ddlProductOrderType.SelectedIndex = 0;
                }

                if (Session["MODE"].Equals("EDIT"))
                {
                    int selIndex;
                    int.TryParse(Session["MS_ProductOrderType"].ToString(), out selIndex);
                    ddlProductOrderType.SelectedIndex = selIndex;
                    // foreach( )
                }
                #endregion
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                #region Populating Product Type DropDown
                ProductType.Items.Add("Select Product Type");
                ProductType.Items.Add("Medicine(HAAD)");
                ProductType.Items.Add("Medicine(Non HAAD)");
                ProductType.Items.Add("Non Medicine");
                #endregion

                #region Populating Product Department DropDown
                try
                {
                    connection.Open();
                    SqlCommand     command = new SqlCommand("Select * From tblDepartment", connection);
                    DataSet        ds      = new DataSet();
                    SqlDataAdapter sA      = new SqlDataAdapter(command);
                    sA.Fill(ds);
                    ProductDept.DataSource     = ds.Tables[0];
                    ProductDept.DataTextField  = "Name";
                    ProductDept.DataValueField = "DepId";
                    ProductDept.DataBind();
                    if (ProductDept != null)
                    {
                        ProductDept.Items.Insert(0, "Select Department");
                        ProductDept.SelectedIndex = 0;
                    }
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    connection.Close();
                }
                #endregion

                #region Populating Category Dropdown
                try
                {
                    connection.Open();
                    SqlCommand     command = new SqlCommand("Select * From tblCategory", connection);
                    DataSet        ds      = new DataSet();
                    SqlDataAdapter sA      = new SqlDataAdapter(command);
                    sA.Fill(ds);
                    ProductCat.DataSource     = ds.Tables[0];
                    ProductCat.DataTextField  = "Name";
                    ProductCat.DataValueField = "CategoryID";
                    ProductCat.DataBind();
                    if (ProductCat != null)
                    {
                        ProductCat.Items.Insert(0, "Select Category");
                        ProductCat.SelectedIndex = 0;
                    }
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    connection.Close();
                }
                #endregion

                #region Populating SubCategory Dropdown
                try
                {
                    connection.Open();
                    SqlCommand     command = new SqlCommand("Select * From tblSub_Category", connection);
                    DataSet        ds      = new DataSet();
                    SqlDataAdapter sA      = new SqlDataAdapter(command);
                    sA.Fill(ds);
                    ProductSubCat.DataSource     = ds.Tables[0];
                    ProductSubCat.DataTextField  = "Name";
                    ProductSubCat.DataValueField = "Sub_CatID";
                    ProductSubCat.DataBind();

                    if (ProductSubCat != null)
                    {
                        ProductSubCat.Items.Insert(0, "Select Sub Category");
                        ProductSubCat.SelectedIndex = 0;
                    }
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    connection.Close();
                }
                #endregion

                #region ProductOrderType dropdown population
                ddlProductOrderType.DataSource     = IMSGlobal.GetOrdersType();
                ddlProductOrderType.DataTextField  = "Name";
                ddlProductOrderType.DataValueField = "OrderTypeId";
                ddlProductOrderType.DataBind();

                if (ddlProductOrderType != null)
                {
                    ddlProductOrderType.Items.Insert(0, "Select Product Order Type");
                    ddlProductOrderType.SelectedIndex = 0;
                }
                #endregion

                BindGridbyFilters();
            }
        }