protected void UpdateFileList(AttachedQuotes file, int rowIndex = 0)
        {
            List <AttachedQuotes> fileList = GetFilesFromViewState();

            switch (file.action)
            {
            case "Add":
                fileList.Add(file);
                break;

            case "Delete":
                fileList.Remove(file);
                //file.id
                break;

            case "Update":
                fileList[rowIndex] = file;
                break;

            default:
                break;
            }

            ViewState["FileList"] = fileList;
            Session["FileList"]   = fileList.ToList();
            BindFiles(fileList);
        }
        private void BindGridView()
        {
            DataSet ds = VendorBLL.Instance.GetAllAttachedQuotes(SoldJobID);

            grdAttachQuotes.DataSource = ds;
            grdAttachQuotes.DataBind();
            List <AttachedQuotes> fileList = new List <AttachedQuotes>();

            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];

                    AttachedQuotes a = new AttachedQuotes();
                    a.id               = Convert.ToInt16(dr["Id"]);
                    a.DocName          = dr["DocName"].ToString();
                    a.TempName         = dr["TempName"].ToString();
                    a.VendorName       = dr["VendorName"].ToString();
                    a.VendorCategoryNm = dr["VendorCategoryNm"].ToString();
                    a.action           = "Add";

                    if (Convert.IsDBNull(dr["VendorCategpryId"]))
                    {
                        a.VendorCategoryId = 0;
                    }
                    else
                    {
                        a.VendorCategoryId = Convert.ToInt16(dr["VendorCategpryId"]);
                    }

                    if (Convert.IsDBNull(dr["VendorId"]))
                    {
                        a.VendorId = 0;
                    }
                    else
                    {
                        a.VendorId = Convert.ToInt16(dr["VendorId"]);
                    }
                    fileList.Add(a);
                }
            }
            ViewState["FileList"] = fileList;
        }
        protected void btnSaveQuotes_Click(object sender, EventArgs e)
        {
            try
            {
                if (selectedRowIndex == "-1")
                {
                    bool res = false;
                    bool fileUploaded = false, fileCreated = false;

                    if (drpVendorCategory.SelectedValue != "Select" && drpVendorName.SelectedValue != "Select")
                    {
                        int vendorId = Convert.ToInt16(drpVendorName.SelectedValue); // need check of blank
                        List <AttachedQuotes> fileList = GetFilesFromViewState();
                        foreach (var item in fileList)
                        {
                            if (vendorId == item.VendorId)
                            {
                                ScriptManager.RegisterStartupScript(this, this.GetType(), "AlertBox", "alert('You have alreay attached a quote with this vendor.');", true);
                                return;
                            }
                        }
                        if (uploadvendorquotes.HasFile)
                        {
                            fileUploaded = true;
                        }
                        if (txtFileName.Text != "" && txtFileContent.Text != "")
                        {
                            fileCreated = true;
                        }

                        if (fileCreated == true && fileUploaded == true)
                        {
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "AlertBox", "alert('Please either upload file or write file. Both operations in one go is not possible.');", true);
                            return;
                        }
                        else if (fileUploaded == true)
                        {
                            HttpPostedFile uploadfile = uploadvendorquotes.PostedFile;

                            string tempFileName     = DateTime.Now.Ticks + Path.GetFileName(uploadfile.FileName);
                            string originalFileName = Path.GetFileName(uploadfile.FileName);

                            if (uploadfile.ContentLength > 0)
                            {
                                uploadfile.SaveAs(Server.MapPath("~/CustomerDocs/VendorQuotes/") + tempFileName);
                            }

                            DataSet ds = VendorBLL.Instance.fetchVendorDetailsByVendorId(vendorId);

                            AttachedQuotes aq = new AttachedQuotes();
                            aq.TempName         = tempFileName;
                            aq.DocName          = originalFileName;
                            aq.action           = "Add";
                            aq.VendorId         = vendorId;
                            aq.VendorName       = ds.Tables[0].Rows[0]["VendorName"].ToString();
                            aq.VendorCategoryNm = ds.Tables[0].Rows[0]["VendorCategoryNm"].ToString();
                            aq.VendorCategoryId = Convert.ToInt16(ds.Tables[0].Rows[0]["VendorCategpryId"]);
                            aqList.Add(aq);
                            UpdateFileList(aq);

                            res = VendorBLL.Instance.AddVendorQuotes(SoldJobID, originalFileName, tempFileName, vendorId);

                            txtFileUpload.Text = "";
                            drpVendorCategory.SelectedIndex = -1;
                            drpVendorName.Items.Clear();
                            drpVendorName.Items.Insert(0, new ListItem("Select", "Select"));
                        }
                        else if (fileCreated == true)
                        {
                            AttachedQuotes aq        = new AttachedQuotes();
                            string         sourceDir = Server.MapPath("~/CustomerDocs/VendorQuotes/");

                            string filename = "";
                            if (!txtFileName.Text.Contains(".txt"))
                            {
                                filename = txtFileName.Text + ".txt";
                            }
                            else
                            {
                                filename = txtFileName.Text;
                            }

                            string tempFileName     = DateTime.Now.Ticks + filename;
                            string originalFileName = filename;

                            File.WriteAllText(sourceDir + tempFileName, txtFileContent.Text);
                            DataSet ds = VendorBLL.Instance.fetchVendorDetailsByVendorId(vendorId);

                            aq.TempName         = tempFileName;
                            aq.DocName          = originalFileName;
                            aq.action           = "Add";
                            aq.VendorId         = vendorId;
                            aq.VendorName       = ds.Tables[0].Rows[0]["VendorName"].ToString();
                            aq.VendorCategoryNm = ds.Tables[0].Rows[0]["VendorCategoryNm"].ToString();
                            aq.VendorCategoryId = Convert.ToInt16(ds.Tables[0].Rows[0]["VendorCategpryId"]);

                            aqList.Add(aq);

                            UpdateFileList(aq);

                            ResetControls();

                            res = VendorBLL.Instance.AddVendorQuotes(SoldJobID, originalFileName, tempFileName, vendorId);
                        }
                        else
                        {
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "AlertBox", "alert('Please upload document or specify file name and content.');", true);
                        }

                        if (res)
                        {
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "AlertBox", "alert('Quote attached successfully');", true);
                        }
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "AlertBox", "alert('Please select Vendor Category and Vendor Name');", true);
                    }
                }
                else //if record gets updated
                {
                    bool res      = false;
                    int  vendorId = selectedVendorID;

                    if (drpVendorCategory.SelectedValue != "Select" && drpVendorName.SelectedValue != "Select")
                    {
                        List <AttachedQuotes> fileListViewState = GetFilesFromViewState();
                        int rowIndex = 0;
                        foreach (var item in fileListViewState)
                        {
                            if (selectedRowIndex == item.TempName)
                            {
                                break;
                            }
                            rowIndex++;
                        }

                        var fileList       = ViewState["FileList"] as List <AttachedQuotes>;
                        var selectedRecord = fileList.Where(a => a.TempName == selectedRowIndex).FirstOrDefault();
                        if (vendorId != selectedRecord.VendorId)
                        {
                            List <AttachedQuotes> file = fileList.Where(a => a.TempName != selectedRowIndex).ToList();

                            foreach (var item in file)
                            {
                                if (vendorId == item.VendorId)
                                {
                                    ScriptManager.RegisterStartupScript(this, this.GetType(), "AlertBox", "alert('You have alreay attached a quote with this vendor.');", true);
                                    return;
                                }
                            }
                        }
                        string uploadedFilename = "", createdFilename = "";
                        bool   fileUploaded = false, fileCreated = false;
                        if (uploadvendorquotes.HasFile)
                        {
                            uploadedFilename = txtFileUpload.Text;
                            fileUploaded     = true;
                        }
                        if (txtFileName.Text != "" && txtFileContent.Text != "")
                        {
                            createdFilename = txtFileName.Text;
                            fileCreated     = true;
                        }

                        if (fileCreated == true && fileUploaded == true)
                        {
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "AlertBox", "alert('Please either upload file or write file. Both operations in one go is not possible.');", true);
                        }
                        else if (fileUploaded == true)
                        {
                            if (uploadvendorquotes.HasFile)
                            {
                                HttpPostedFile uploadfile = uploadvendorquotes.PostedFile;

                                string sourceDir = Server.MapPath("~/CustomerDocs/VendorQuotes/");

                                if (File.Exists(sourceDir + selectedRecord.TempName))   // to delete previous file
                                {
                                    File.Delete(sourceDir + selectedRecord.TempName);
                                }

                                string tempFileName     = DateTime.Now.Ticks + Path.GetFileName(uploadfile.FileName);
                                string originalFileName = Path.GetFileName(uploadfile.FileName);

                                if (uploadfile.ContentLength > 0)
                                {
                                    uploadfile.SaveAs(Server.MapPath("~/CustomerDocs/VendorQuotes/") + tempFileName);
                                }

                                DataSet ds = VendorBLL.Instance.fetchVendorDetailsByVendorId(vendorId);

                                AttachedQuotes aq = new AttachedQuotes();
                                aq.TempName         = tempFileName;
                                aq.DocName          = originalFileName;
                                aq.action           = "Update";
                                aq.VendorId         = vendorId;
                                aq.VendorName       = ds.Tables[0].Rows[0]["VendorName"].ToString();
                                aq.VendorCategoryNm = ds.Tables[0].Rows[0]["VendorCategoryNm"].ToString();
                                aq.VendorCategoryId = Convert.ToInt16(ds.Tables[0].Rows[0]["VendorCategpryId"]);
                                aqList.Add(aq);
                                UpdateFileList(aq, rowIndex);

                                res = VendorBLL.Instance.UpdateAttachedQuote(SoldJobID, originalFileName, tempFileName, vendorId);

                                txtFileUpload.Text = "";
                                drpVendorCategory.SelectedIndex = -1;
                                drpVendorName.Items.Clear();
                                drpVendorName.Items.Insert(0, new ListItem("Select", "Select"));
                            }

                            selectedRowIndex = "-1";   //means now no row is selected
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "AlertBox", "alert('Quote updated successfully');", true);
                        }
                        else if (fileCreated == true)
                        {
                            AttachedQuotes aq        = new AttachedQuotes();
                            string         sourceDir = Server.MapPath("~/CustomerDocs/VendorQuotes/");

                            string filename = "";
                            if (!txtFileName.Text.Contains(".txt"))
                            {
                                filename = txtFileName.Text + ".txt";
                            }
                            else
                            {
                                filename = txtFileName.Text;
                            }

                            string tempFileName     = DateTime.Now.Ticks + filename;
                            string originalFileName = filename;

                            File.WriteAllText(sourceDir + tempFileName, txtFileContent.Text);
                            DataSet ds = VendorBLL.Instance.fetchVendorDetailsByVendorId(vendorId);
                            aq.TempName         = tempFileName;
                            aq.DocName          = originalFileName;
                            aq.action           = "Update";
                            aq.VendorId         = vendorId;
                            aq.VendorName       = ds.Tables[0].Rows[0]["VendorName"].ToString();
                            aq.VendorCategoryNm = ds.Tables[0].Rows[0]["VendorCategoryNm"].ToString();
                            aq.VendorCategoryId = Convert.ToInt16(ds.Tables[0].Rows[0]["VendorCategpryId"]);

                            aqList.Add(aq);
                            UpdateFileList(aq, rowIndex);

                            if (File.Exists(sourceDir + selectedRecord.TempName))   // to delete previous file
                            {
                                File.Delete(sourceDir + selectedRecord.TempName);
                            }

                            res = VendorBLL.Instance.UpdateAttachedQuote(SoldJobID, originalFileName, tempFileName, vendorId);

                            ResetControls();

                            ScriptManager.RegisterStartupScript(this, this.GetType(), "AlertBox", "alert('Quote updated successfully');", true);
                        }
                        else
                        {
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "AlertBox", "alert('Please upload document or specify file name and content.');", true);
                        }
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "AlertBox", "alert('Please select Vendor Category and Vendor Name');", true);
                    }
                    enableDropdowns();
                }
            }
            catch (Exception ex)
            {
            }
        }