protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            if (HttpContext.Current.Session["CurrentUser"] == null)
            {
                Response.Redirect("Logout.aspx");
            }

            var eDoc = new BestDocuments();
            OleDbCommand myCmd = eDoc.dbCmd;
            myCmd.CommandText = "select documentFileSize, documentFileName, documentFile from BestDocuments where centerid = ? and studentid = ? and guidfield=?";
            OleDbParameter sid = new OleDbParameter("studentid", OleDbType.VarChar, 50);
            sid.Value = Request.Form["sid"];
            OleDbParameter cid = new OleDbParameter("centerid", OleDbType.VarChar, 50);
            cid.Value = Request.Form["cid"];
            OleDbParameter guid = new OleDbParameter("guidfield", OleDbType.Guid, 40);
            guid.Value = new Guid(Request.Form["guid"]);

            myCmd.Parameters.Add(cid);
            myCmd.Parameters.Add(sid);
            myCmd.Parameters.Add(guid);
            OleDbDataReader readDoc = myCmd.ExecuteReader();
            if (readDoc.Read())
            {
                if (readDoc.GetValue(1).GetType() != Type.GetType("System.DBNull"))
                {
                    string docName = readDoc.GetString(1);
                    var tvalue = readDoc.GetValue(2);
                    docImage = (byte[])tvalue;

                    MemoryStream ms = new MemoryStream();
                    ms.Write(docImage, 0, docImage.Length);

                    Response.ClearContent();
                    Response.ClearHeaders();
                    Response.ContentType = "application/doc";
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + docName);

                    Response.OutputStream.Write(ms.ToArray(), 0, ms.ToArray().Length);
                    Response.Flush();
                    Response.Close();
                    ms.Close();
                }
            }
            else
            {
                Response.Write("No Document");
            }
        }
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session["CurrentUser"] == null)
            {
                Response.Redirect("Logout.aspx");
            }
            ltrSubMenu.Text = UtilMenu.StudentMenu("studentdocument");
            if (!Utils.User.UserRoleByName("Student - Documents").allowView)
            {
                ltrGrid.Text = "You do not have rights to view.";
                return;
            }

            string ms = Request.QueryString["ms"];
            this.ltrMScript.Text = Utils.MenuSelectScript(ms);

            string saveClicked = Request.Form["SaveClicked"] ?? "";
            if (IsPostBack && saveClicked.Equals("1"))
            {
                BestDocuments bs = new BestDocuments();
                string isnew = Request.Form["isnew"];
                bool cansave = true;
                if (string.IsNullOrEmpty(isnew))
                {
                    List<BestField> bparams = new List<BestField>();
                    BestField guid = new BestField() { fieldName = "guidfield", fieldSize = 40, fieldType = "System.Guid", paramOledbType = System.Data.OleDb.OleDbType.Guid, displayField = false };
                    guid.fieldValue = Request.Form["guidfield"];
                    bparams.Add(guid);

                    if (!string.IsNullOrEmpty(guid.fieldValue))
                    {
                        bs.LoadRows("guidfield=?", bparams);
                    }
                    else
                    {
                        string delguid = Request.Form["deleteguid"];
                        if (!string.IsNullOrEmpty(delguid))
                        {
                            bparams[0].fieldValue = delguid;
                            bs.LoadRows("guidfield=?", bparams);
                            bs.CurrentRow.IsDelete = true;
                            bs.CurrentRow.Save();
                        }
                        cansave = false;
                    }
                }

                if (cansave)
                {
                    if (!string.IsNullOrEmpty(isnew))
                    {
                        string guid = Request.Form["g_studentGuid"];
                        if (string.IsNullOrEmpty(guid))
                        {
                            ltrValidateMsg.Text = Utils.WarningMessage("Student Name is Required.");
                            cansave = false;
                        }
                        bs.studentGuid = new Guid(guid);
                    }
                    if (cansave)
                    {
                        bool showToStudent = (Request.Form["g_showToStudent"] ?? "").ToLower().Equals("true");
                        bs.showToStudent = showToStudent;
                        string docdate = Request.Form["g_documentDate"];
                        if (!string.IsNullOrEmpty(docdate))
                        {
                            bs.documentDate = Convert.ToDateTime(docdate);
                        }
                        bs.documentTitle = Request.Form["g_documentTitle"];
                        string expdate = Request.Form["g_documentExpiry"];
                        if (!string.IsNullOrEmpty(expdate))
                        {
                            bs.documentExpiry = Convert.ToDateTime(expdate);
                        }
                        bs.CenterId = Utils.User.CenterId;
                        BestStudents bestStd = new BestStudents();
                        bestStd.LoadRows("guidfield=?", "studentguid", bs.studentGuid, "");
                        bs.StudentId = bestStd.StudentId;

                        if (!bs.CurrentRow.Save())
                        {
                            ltrValidateMsg.Text = Utils.WarningMessage(bs.CurrentRow.lastError);
                        }
                    }
                }
            }

            /* Auto Student */
            string autoSample = "{value:\"[paramValue]\", label:[paramLabel]}";
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("autoStudents = [");
            BestStudents bstd = new BestStudents();
            bstd.LoadRows("CenterId=?", Utils.User.CIdParam);
            for (int s = 0; s < bstd.TableRows.Count; s++)
            {
                string result = autoSample;
                result = result.Replace("[paramValue]", bstd.TableRows[s].Fields["guidfield"].fieldValue);
                result = result.Replace("[paramLabel]", Utils.EnquoteJS((bstd.TableRows[s].Fields["firstName"].fieldValue ?? "") + " " +
                    (bstd.TableRows[s].Fields["lastName"].fieldValue ?? "")));
                sb.Append(result);
                if (s < bstd.TableRows.Count - 1) sb.AppendLine(",");
            }
            sb.AppendLine("];");
            ltrScript.Text = @"<script type=""text/javascript"">
            $(document).ready(function(){ " + sb.ToString() + @"
            $('#studentTag1').autocomplete( { source:autoStudents, delay: 0, select : function( event, ui){
            $('#studentGuid').val( ui.item.value );
            $( this ).val( ui.item.label );
            return false;
            } } ); }); </script>";

            BestGrid bsGrid = new BestGrid();
            bsGrid.PageRequest = Page.Request;
            bsGrid.Title = "Student Documents";
            bsGrid.securityPage = "Student - Documents";
            bsGrid.GridTable = new BestDocuments();
            bsGrid.extraRowHTML = "<span><a href=\"javascript:void(0);\" onclick=\"showUploadDocument('[paramCID]','[paramSID]','[paramGuid]', [paramRowNum]);\" title=\"Upload Document\" ><img src=\"images\\folder_page.png\" alt=\"upload\" /></a></span><span><a href=\"javascript:void(0);\" onclick=\"DownloadDocument('[paramGuid]','[paramCID]','[paramSID]');\" title=\"Download Document\"><img src=\"images\\page_white_put.png\" alt=\"upload\" /></a></span>";
            bsGrid.whereClause = "";
            ltrGrid.Text = bsGrid.ToHTML();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session["CurrentUser"] == null)
            {
                Response.Redirect("Logout.aspx");
            }
            string centerid = Request.QueryString["cid"];
            string studid = Request.QueryString["sid"];
            string docGuid = Request.QueryString["guid"];
            if (centerid.Equals(Utils.User.CenterId))
            {
                if (string.IsNullOrEmpty(studid) && string.IsNullOrEmpty(docGuid))
                {
                    Response.Write("Document Details is not found");
                }
                else
                {
                    if (IsPostBack)
                    {
                        if (Request.Files.Count > 0)
                        {
                            BestField sid = new BestField() { fieldName = "studentid", fieldSize = 50, fieldType = "System.String", paramOledbType = System.Data.OleDb.OleDbType.VarChar };
                            sid.fieldValue = Request.QueryString["sid"];
                            BestField cid = new BestField() { fieldName = "centerid", fieldSize = 50, fieldType = "System.String", paramOledbType = System.Data.OleDb.OleDbType.VarChar };
                            cid.fieldValue = Request.QueryString["cid"];
                            BestField docguid = new BestField() { fieldType = "System.Guid", fieldSize = 50, fieldName = "guidfield", paramOledbType = OleDbType.Guid };
                            docguid.fieldValue = docGuid;

                            BestDocuments bestDocs = new BestDocuments();
                            List<BestField> bparams = new List<BestField>();
                            bparams.Add(sid);
                            bparams.Add(cid);
                            bparams.Add(docguid);
                            bestDocs.LoadRows("studentid = ? and centerid = ? and guidfield=?", bparams);

                            OleDbCommand myCmd = bestDocs.dbCmd;
                            if (bestDocs.CurrentRow.IsNew)
                            {
                                this.errorDisplay.Text = "<div class=\"err\">Document Details Not Found</div>";
                            }
                            else
                            {
                                myCmd.CommandText = "update BestDocuments set documentFile = ?, documentFileSize = ?, documentFileName = ? where studentid=? and centerid=? and guidfield=?";

                                HttpPostedFile PostedFile = docFile.PostedFile;
                                byte[] docBytes = new byte[PostedFile.ContentLength];
                                HttpPostedFile uploadedImage = PostedFile;
                                uploadedImage.InputStream.Read(docBytes, 0, (int)PostedFile.ContentLength);

                                object tvalue = docBytes;
                                OleDbParameter p1 = new OleDbParameter("documentFile", OleDbType.LongVarBinary, -1, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, tvalue);
                                OleDbParameter p2 = new OleDbParameter("documentFileSize", OleDbType.VarChar, 12);
                                p2.Value = PostedFile.ContentLength;
                                OleDbParameter p3 = new OleDbParameter("documentFileName", OleDbType.VarChar, 100);
                                p3.Value = PostedFile.FileName;
                                myCmd.Parameters.Clear();
                                myCmd.Parameters.Add(p1);
                                myCmd.Parameters.Add(p2);
                                myCmd.Parameters.Add(p3);
                                myCmd.Parameters.Add(sid.Param);
                                myCmd.Parameters.Add(cid.Param);
                                myCmd.Parameters.Add(docguid.Param);
                                try
                                {
                                    myCmd.ExecuteNonQuery();
                                    this.errorDisplay.Text = "<div>Document Uploaded.</div><script type=\"text/javascript\">window.parent.closeUploadDoc();</script>";
                                }
                                catch (Exception ex)
                                {
                                    this.errorDisplay.Text = "<div class=\"err\">There was an error saving the photo.<br />" + HttpUtility.HtmlEncode(ex.Message) + "</div>";
                                }
                            }

                        }

                    }
                }
            }
            else
            {
                Response.Write("Center Information is not valid.");
            }
        }