Quantcast
Channel: ฟอรัม Getting started with SQL Server
Viewing all articles
Browse latest Browse all 8428

Deposit generated PDF to SQL DB using Entity Framework

$
0
0

Hello,

I am the system admin and have no real experience with .NET and/or SQL. My task is to make a change to existing application so the PDF file it generates using ASPpdf.Net (Persits.PDF) functions is deposited to SQL database as binary data to be retrieved as pdf later, if needed. One of the requirements is that I use Entity Framework to make this happen.

The page I need to modify is a registration page. It uses PDF Manager to populate a preformatted PDF form stored in the root folder that has form field labeled as "f1-1", and saves a copy of it (named with the session ID for the file name) into a "files" folder in the root. Below is the code that populates the PDF form with values from certain input fields from ASPX page:

****************************************************************************************************

SQL Query for the DB table is:

USE [DatabaseName]
GO

ALTER TABLE [dbo].[TableName] DROP CONSTRAINT [FK_TableName_Users]
GO

ALTER TABLE [dbo].[TableName] DROP CONSTRAINT [DF_TableName_DateCreated]
GO

DROP TABLE [dbo].[TableName]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[TableName](
 [Id] [int] IDENTITY(1,1) NOT NULL,
 [File] [nvarchar](max) NOT NULL,
 [DateCreated] [datetime] NOT NULL,
 [DateModified] [datetime] NULL,
 [UserId] [int] NOT NULL,
 CONSTRAINT [PK_TableName] PRIMARY KEY CLUSTERED
(
 [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

ALTER TABLE [dbo].[TableName] ADD  CONSTRAINT [DF_TableName_DateCreated]  DEFAULT (getutcdate()) FOR [DateCreated]
GO

ALTER TABLE [dbo].[TableName]  WITH CHECK ADD  CONSTRAINT [FK_TableName_Users] FOREIGN KEY([UserId])
REFERENCES [dbo].[Users] ([UserId])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[TableName] CHECK CONSTRAINT [FK_TableName_Users]
GO


The C# code is:


<script runat="server" LANGUAGE="C#">
void Page_Load(Object Source, EventArgs E)
{
   
 if( !IsPostBack )
 {
 txtSignature.Text = "";
        txtDay.Text = DateTime.Now.ToString("d" + " 'day of' " + "MMMMMMMMMM" + ", " + "yyyy");
 }
 lblResult.Text = "Hello!";
 // to keep sessionID (used for file name generation) from changing every time
 Session["dummy"] = 1;
}
public void GeneratePDF(object sender, System.EventArgs args)
{
 // create instance of the PDF manager
 PdfManager objPDF = new PdfManager();
 // Create new document
    PdfDocument objDoc = objPDF.OpenDocument(Server.MapPath("images/pdftest.pdf"));
    PdfFont objFont1 = objDoc.Fonts["Times New Roman"]; // a standard font
 PdfAnnot objField;
   
    // Set Day
    objField = objDoc.Form.FindField("f1-1");
    objField.SetFieldValue(txtDay.Text, objFont1);

    // I use Session ID for file names.
 // false means "do not overwrite"
 // The method returns generated file name

 String strPath = Server.MapPath( "files") + "\\" + Session.SessionID + ".pdf";
 String strFileName = objDoc.Save( strPath, false );
 lblResult.Text = "Success. Your PDF file <font color=gray>" + strFileName + "</font> can be downloaded <A TARGET=\"_new\" HREF=\"files/" + strFileName + "\"><B>here</B></A>.";
}
</script>


IN THE BODY OF THE ASPX PAGE, THERE IS CORRESPONDING .NET CODE:


<div><ASP:TextBox ID="txtDay" runat="server" Width="25px"/></div>
<asp:LinkButton OnClick="GeneratePDF" runat="server">Submit</asp:LinkButton>

*******************************************************************************************************************************************

I saw several posts describing the process of depositing a file to SQL DB using EF, but I am totally perplexed about what and where to change in this code to make the resulting generated pdf file deposited in the table as binary.

Can anyone kindly help? I am pulling my last hair trying to get this to work for the past four days, but every tweaking I tried so far breaks the page and returns errors... Can someone tell me what exactly needs to change, since I am not an expert and need to make this modification to work properly? Greatly appreciate any help! :)

JV


Viewing all articles
Browse latest Browse all 8428

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>