After learning how to upload and preview PDF file on a web page, how do I create a system where a QR code is generated for the displayed PDF file? I have a save button that inserts the PDF file into the database table. When the save button is clicked, it redirects to another page where the PDF file will display and an image control will show the QR code generated.
There are two web pages; the first page is used to upload the PDF file on to the web form with fileupload control, and inserts the file into database as binary with the click of the save button. The save button also redirects to another page where the PDF file will show alongside the QR code generated. I tried to make it work but when it redirects to another web form no PDF file is showed and no QR code too. How can I make this work please?
HTML for webform2
<div class="form-horizontal">
<h1 style="font-size: small; margin-top: 0%; color: red;">ID Card with QR Code</h1>
<hr />
<br />
<div class="row">
<asp:Panel ID="printpanel" runat="server">
<div class="contentt">
<canvas id="the-canvas" style="border: 1px solid black; height: 750px; width: 650px;"></canvas>
<asp:Image ID="Image1" runat="server" BorderStyle="None" Width="80px" Height="85px" />
</div>
</asp:Panel>
<br />
<div class="print">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br /><br />
<asp:Button ID="btnPrinnt" runat="server" CssClass="btn btn-primary navbar-btn" BackColor="SteelBlue" Font-Size="Small" Text="Send to Mail"/>
<br /><br />
<asp:Button ID="buttonmail" runat="server" CssClass="btn btn-primary navbar-btn" BackColor="SteelBlue" Font-Size="Small" Text="Download Invoice"/>
</div>
</div>
</div>
CODE FOR WEBFORM2
public partial class Invoiceprint : System.Web.UI.Page
{
SqlCommand cmd = new SqlCommand();
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");
private string imgBytes;
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True";
if (!this.IsPostBack)
{
//loads the PDF file on the canvas control
BindCardAndQR();
}
}
private void BindCardAndQR()
{
if (Session["paperinv"] != null)
{
int id = Convert.ToInt32(Session["paperinv"]);
//selecting the PDF file data and other related data from database table
string sql = "SELECT * FROM tableinvoice WHERE Id = @Id";
SqlParameter[] parameters = new[] {
new SqlParameter("@Id", id)
};
// Binding PDF file for canvas control
DataTable dt = SelectFromDatabase(sql, parameters);
if (dt.Rows.Count > 0)
{
var pdfAsArray = "data:image/jpg;base64," + Convert.ToBase64String((byte[])dt.Rows[0]["Data"]);
}
else
{
}
//This is where I Bind Image1 control for the QR code generated
byte[] QRBytes = GetQRCodeBytes(Server.MapPath("/Invoiceprint.aspx") + "?Id=" + id);
Image1.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(QRBytes);
}
}
public byte[] GetQRCodeBytes(string url)
{
//QR code generated here
QRCodeEncoder encoder = new QRCodeEncoder();
Bitmap bi = encoder.Encode(url);
MemoryStream tmpSteam = new MemoryStream();
bi.Save(tmpSteam, ImageFormat.Jpeg);
return tmpSteam.ToArray();
}
public static DataTable SelectFromDatabase(string sql, SqlParameter[] parameters)
{
using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (parameters != null)
{
cmd.Parameters.AddRange(parameters);
}
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
}