In this article I will explain with an example, how to generate QRCode from SQL Server database in ASP.Net MVC.
Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
 
 

Installing QRCoder package using Nuget

In order to install QRCoder library using Nuget, please refer my article Install QRCoder library using Nuget.
 
 

Database

I have made use of the following table Links with the schema as follows.
ASP.Net MVC: Generate QR Code from Database
 
I have already inserted few records in the table.
ASP.Net MVC: Generate QR Code from Database
 
Note: You can download the database table SQL by clicking the download link below.  
         Download SQL file
 
 

Entity Framework Model

Once the Entity Framework is configured and connected to the database table, the Model will look as shown below.
Note: For beginners in ASP.Net MVC and Entity Framework, please refer my article ASP.Net MVC: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework.
 
ASP.Net MVC: Generate QR Code from Database
 
 

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 { getset; }
    public string Link { getset; }
    public string QRCodeImage { getset; }
}
 
 

Controller

The Controller consists of following Action method.

Action method for handling GET operation

Inside this Action method, the records from the SQL Server database are fetched using Entity Framework.
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

ASP.Net MVC: Generate QR Code from Database
 
 

Demo

 
 

Downloads