In this article I will explain with an example, how to generate
QRCode from
SQL Server database in ASP.Net MVC.
Installing QRCoder package using Nuget
Database
I have made use of the following table Links with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Entity Framework Model
Once the
Entity Framework is configured and connected to the database table, the Model will look as shown below.
Namespaces
You will need to import the following namespaces.
using QRCoder;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
Model
The Model class consists of following properties.
public class QRCodeModel
{
public int LinkId { get; set; }
public string Link { get; set; }
public string QRCodeImage { get; set; }
}
Controller
The Controller consists of following Action method.
Action method for handling GET operation
The QRCodeImage is generated using the GenerateQRCode (explained later) method.
GenerateQRCode
Inside the
GnerateQRCode method, the value of the
QRCode from
SQL Server database is captured as parameter.
The
QRCode is passed to the
CreateQrCode method of the
QRCoder library which returns a Bitmap image.
The Bitmap image is then, saved as PNG image in MemoryStream which later is converted to a Base64 string.
Finally, the Base64 string is returned and set in the QRCodeImage property of the QRCodeModel class.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
AjaxSamplesEntities entities = new AjaxSamplesEntities();
List<QRCodeModel> links = (from link in entities.Links.AsEnumerable()
select new QRCodeModel
{
LinkId = link.LinkId,
Link = link.Link1,
QRCodeImage = GenerateQRCode(link.Link1)
}).ToList();
return View(links);
}
public static string GenerateQRCode(string qrcode)
{
using (MemoryStream ms = new MemoryStream())
{
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrcode, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
using (Bitmap bitMap = qrCode.GetGraphic(20))
{
bitMap.Save(ms, ImageFormat.Png);
return "data:image/png;base64," + Convert.ToBase64String(ms.ToArray());
}
}
}
}
View
HTML Markup
Inside the View, in the very first line the QRCodeModel class is declared as IEnumerable which specifies that it will be available as a Collection.
For displaying the records, an HTML Table is used. A FOR EACH loop will be executed over the Model which will generate the HTML Table rows with the records.
The
QRCode image is displayed using an HTML Image element.
@model IEnumerable<QRCode_Database_MVC.Models.QRCodeModel>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>Link Id</th>
<th>Link</th>
<th>QRCode</th>
</tr>
</thead>
<tbody>
@foreach (var link in Model)
{
<tr>
<td>@link.LinkId</td>
<td>@link.Link</td>
<td><img src="@link.QRCodeImage" height="100" width="100" /></td>
</tr>
}
</tbody>
</table>
</body>
</html>
Screenshot
Demo
Downloads