Beispiel #1
0
        protected void btnUpdateDocument_Click(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "UpdateSuccessfully()", true);

            Boolean FileOK    = false;
            Boolean FileSaved = false;

            if ((DocumentFileUpload.PostedFile != null) && (DocumentFileUpload.PostedFile.ContentLength > 0))
            {
                string extension       = DocumentFileUpload.PostedFile.FileName.Substring(DocumentFileUpload.PostedFile.FileName.LastIndexOf('.'));
                string friendlyName    = DocumentFileUpload.PostedFile.FileName.Replace(extension, "");
                string fn              = RegistrationId + "_" + friendlyName + "_" + DateTime.Now.ToShortDateString().Replace("/", "_") + extension;
                string SaveLocationAll = Server.MapPath("DesktopModules/ThSport/DocRegFile") + "\\" + fn;
                DocumentFileUpload.SaveAs(Server.MapPath("DesktopModules/ThSport/DocRegFile") + "\\" + fn);
                string SaveLocation = Path.GetFileName(("~/DesktopModules/ThSport/DocRegFile") + "\\" + fn);

                bool isExists = System.IO.Directory.Exists(Server.MapPath("~/DesktopModules/ThSport/DocRegFile"));
                if (!isExists)
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath("~/DesktopModules/ThSport/DocRegFile"));
                }

                try
                {
                    cc.RegistrationDocId             = Convert.ToInt32(hidRegID.Value);
                    cc.RegistrationId                = RegistrationId;
                    cc.RegistrationDocTypeId         = Convert.ToInt32(ddlDocumentType.SelectedValue);
                    cc.RegistrationDocNumber         = txtRegistrationDocNumber.Text.Trim();
                    cc.RegistrationDocCountryOfIssue = txtRegistrationDocCountryOfIssue.Text.Trim();
                    cc.RegistrationDocDateOfIssue    = txtRegistrationDocDateOfIssue.Text.Trim();
                    cc.RegistrationDocDateOfExpiry   = txtRegistrationDocDateOfExpiry.Text.Trim();
                    cc.RegistrationDocFile           = SaveLocation;
                    cc.PortaID      = PortalId;
                    cc.ModifiedById = currentUser.Username;

                    ccc.UpdateDocument(cc);

                    pnlEntryDocument.Visible = false;
                    PnlGridDocument.Visible  = true;
                    FillGridView();
                    funClearData();
                }
                catch (Exception ex)
                {
                }
            }

            pnlEntryDocument.Visible = false;
            PnlGridDocument.Visible  = true;
            FillGridView();
            funClearData();
        }
        // public string ImageName;
        protected void btnSave_Click(object sender, System.EventArgs e)
        {
            if (txtEmailAddress.Text != null && txtPassword.Text != null)
            {
                //       SqlConnection sqlConn = new SqlConnection();
                // Local Database Connection String
                //sqlConn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["ConnString"].ToString();

                // Production Database Connection String
                //sqlConn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString();

                // For Production Database
                //String strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                // For Local Database
                String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString();

                //        SqlConnection con = new SqlConnection(strConnString);
                //         SqlCommand sqlComm = new SqlCommand();

                //          sqlComm.CommandType = CommandType.StoredProcedure;
                //          sqlComm.CommandText = "ssp_InsertUsers";
                //            sqlComm.Connection = con;

                string        sqlUserExist = "SELECT Count(*) FROM Users_TB WHERE EmailAddress = '" + txtEmailAddress.Text + "'";
                SqlConnection sqlConn      = new SqlConnection(strConnString);
                SqlCommand    sqlComm      = new SqlCommand(sqlUserExist, sqlConn);
                sqlConn.Open();
                int UserCount = (int)sqlComm.ExecuteScalar();
                if (UserCount == 1)
                {
                    lblOutput.Text = "User already Exist, Select another name";
                    return;
                }
                else
                {
                    //   btnDisplay_Click();
                }


                // Image Path + FileName + Extension
                string imgfilePath  = ImageFileUpload.PostedFile.FileName;
                string imgfilename  = Path.GetFileName(imgfilePath);
                string strImageName = imgfilename;
                string ImageExt     = Path.GetExtension(imgfilename);

                // Document Path + FileName + Extension
                string profilePath     = DocumentFileUpload.PostedFile.FileName;
                string profilename     = Path.GetFileName(profilePath);
                string strDocumentName = profilename; // DocumentFileUpload.ToString(); //  documentUpload.Value;
                string DocumentExt     = Path.GetExtension(profilename);


                // Document Path + FileName + Extension
                Microsoft.Office.Interop.Word.Application objWord = new Application();
                //This creates new object of Word.ApplicationClass
                string strPathToUpload;
                //Path to upload files "Uploaded"
                string strPathToConvert;
                //Path to convert uploaded files and save
                object fltDocFormat = 10;
                //For filtered HTML Output
                object missing = System.Reflection.Missing.Value;
                //Is just to skeep the parameters which are passed as boject reference, these are seems to be optional parameters
                object readOnly  = false;
                object isVisible = false;
                //The process has to be in invisible mode


                //  byte byteStatus = 1;
                DateTime txtRegisterDate = DateTime.Today;
                DateTime txtModifiedDate = DateTime.Now;

                // Selected User
                string intRoleId   = ddlRoles.SelectedValue;
                string txtRoleName = ddlRoles.SelectedItem.ToString();

                // Selected Project
                string intProjectId   = ddlProjects.SelectedValue;
                string txtProjectName = ddlProjects.SelectedItem.ToString();

                //      string strLoginId = txtEmailAddress.Text;
                string strYourIp = HttpContext.Current.Request.UserHostAddress;

                sqlComm.CommandType = CommandType.StoredProcedure;
                sqlComm.CommandText = "ssp_InsertUser";

                //sqlComm.CommandText = "INSERT INTO Users_TB(FirstName, LastName, Domain, Location, UserPassword, PhoneNumber, EmailAddress," +
                //    "ProjectId, WebsiteURL, RegisterDate, RoleId, Comments," +
                //    "Technology, ImageName, ProfileName, Status, ModifiedDate) VALUES('" + txtFirstName.Text + "', '" + txtLastName.Text + "','" + txtDomain.Text + "','" + txtLocation.Text +
                //    "','" + txtPassword.Text + "','" + txtUserPhone.Text + "','" + txtEmailAddress.Text + "','" + intProjectId + "','" + txtWebsiteURL.Text +
                //    "','" + txtRegisterDate.Date + "','" + intRoleId + "', '" + txtComments.Text + "', '" + txtTechnology.Text + "','" + strImageName + "','" + strDocumentName +
                //    "', '" + byteStatus + "', '" + txtModifiedDate + "')";


                sqlComm.Parameters.Add("@FirstName", SqlDbType.VarChar).Value     = txtFirstName.Text;
                sqlComm.Parameters.Add("@LastName", SqlDbType.VarChar).Value      = txtLastName.Text;
                sqlComm.Parameters.Add("@Domain", SqlDbType.VarChar).Value        = txtDomain.Text;
                sqlComm.Parameters.Add("@Location", SqlDbType.VarChar).Value      = txtLocation.Text;
                sqlComm.Parameters.Add("@UserPassword", SqlDbType.Text).Value     = txtPassword.Text;
                sqlComm.Parameters.Add("@PhoneNumber", SqlDbType.VarChar).Value   = txtUserPhone.Text;
                sqlComm.Parameters.Add("@EmailAddress", SqlDbType.Text).Value     = txtEmailAddress.Text;
                sqlComm.Parameters.Add("@ProjectId", SqlDbType.Int).Value         = ddlProjects.SelectedValue;
                sqlComm.Parameters.Add("@WebsiteURL", SqlDbType.Text).Value       = txtWebsiteURL.Text;
                sqlComm.Parameters.Add("@RegisterDate", SqlDbType.DateTime).Value = DateTime.Now;
                sqlComm.Parameters.Add("@RoleId", SqlDbType.Int).Value            = ddlRoles.SelectedValue;
                sqlComm.Parameters.Add("@Comments", SqlDbType.Text).Value         = txtComments.Text;
                sqlComm.Parameters.Add("@Technology", SqlDbType.Text).Value       = txtTechnology.Text;
                sqlComm.Parameters.Add("@ModifiedDate", SqlDbType.DateTime).Value = DateTime.Now;
                sqlComm.Parameters.Add("@Status", SqlDbType.Bit).Value            = true;
                sqlComm.Parameters.Add("@ImageName", SqlDbType.VarChar).Value     = imgfilename;
                sqlComm.Parameters.Add("@ProfileName", SqlDbType.VarChar).Value   = profilename;

                sqlComm.ExecuteNonQuery();
                // Image File Upload
                if ((ImageFileUpload.PostedFile != null) && (ImageFileUpload.PostedFile.ContentLength > 0))
                {
                    string fn           = System.IO.Path.GetFileName(ImageFileUpload.PostedFile.FileName);
                    string SaveLocation = Server.MapPath("ImageData") + "\\" + fn;
                    try
                    {
                        imgPicture.ImageUrl = imgfilePath;
                        ImageFileUpload.PostedFile.SaveAs(SaveLocation);
                        //    btnImageUpload_Click(sender, e);
                        Response.Write("Image file has been Uploaded.");
                    }
                    catch (Exception ex)
                    {
                        Response.Write("Error: " + ex.Message);
                        //Note: Exception.Message returns a detailed message that describes the current exception.
                        //For security reasons, we do not recommend that you return Exception.Message to end users in
                        //production environments. It would be better to return a generic error message.
                    }
                }
                else
                {
                    Response.Write("Please select a file to upload.");
                }
                // Profile Document Upload
                if ((DocumentFileUpload.PostedFile != null) && (DocumentFileUpload.PostedFile.ContentLength > 0))
                {
                    try
                    {
                        //To check the file extension if it is word document or something else
                        string   strFileName = DocumentFileUpload.FileName;
                        string[] strSep      = DocumentFileUpload.FileName.Split('.');
                        int      arrLength   = strSep.Length - 1;
                        string   strExt      = strSep[arrLength].ToString().ToUpper();
                        //Save the uploaded file to the folder
                        //strPathToUpload = Server.MapPath(@"ProfileData\");
                        strPathToUpload = Server.MapPath("\\ProfileData");
                        //Map-path to the folder where html to be saved
                        strPathToConvert = Server.MapPath(@"ConvertedLocation\");
                        object FileName   = strPathToUpload + "\\" + DocumentFileUpload.FileName;
                        object FileToSave = strPathToConvert + "\\" + DocumentFileUpload.FileName + ".htm";
                        if (strExt.ToUpper().Equals("DOC"))
                        {
                            DocumentFileUpload.SaveAs(strPathToUpload + "\\" + DocumentFileUpload.FileName);
                            lblMessage.Text = "File uploaded successfully";
                            //open the file internally in word. In the method all the parameters should be passed by object reference
                            objWord.Documents.Open(ref FileName, ref readOnly, ref missing, ref missing, ref missing, ref missing,
                                                   ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing,
                                                   ref missing, ref missing);
                            //Do the background activity
                            objWord.Visible = false;
                            Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;
                            oDoc.SaveAs(ref FileToSave, ref fltDocFormat, ref missing, ref missing, ref missing, ref missing,
                                        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                                        ref missing, ref missing);
                            lblMessage.Text = DocumentFileUpload.FileName + " converted to HTML successfully";
                        }
                        else
                        {
                            lblMessage.Text = "Invalid file selected!";
                        }

                        //Read the Html File as Byte Array and Display it on browser
                        imgDocument.Visible.ToString();

                        imgDocument.Visible = true;

                        //  imgDocument.Attributes.Add("src", FileToSave.ToString());
                        imgDocument.InnerText = FileToSave.ToString();

                        //imgDocument.Attributes["src"] = FileToSave.ToString();

                        //byte[] bytes;
                        //using (FileStream fs = new FileStream(FileToSave.ToString(), FileMode.Open, FileAccess.Read))
                        //{
                        //    BinaryReader reader = new BinaryReader(fs);
                        //    bytes = reader.ReadBytes((int)fs.Length);
                        //    fs.Close();
                        //}
                        //Response.BinaryWrite(bytes);
                        //Response.Flush();



                        //Close/quit word
                        //objWord.Quit(ref missing, ref missing, ref missing);
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                }
                sqlConn.Close();
                //divResult.InnerText = "User Name has Added Successfully";
                return;
            }
        }