Save (Insert) QR Code Image in Database Table using ASP.Net C#

Last Reply 15 days ago By Adnaan

Posted 15 days ago

Teble structure:-

CREATE TABLE [dbo].[tblQRCode] ( 
    [Id] INT IDENTITY (1, 1) NOT NULL, 
    [Name] VARCHAR(50) NULL, 
    [Data] IMAGE NULL, PRIMARY KEY CLUSTERED ([Id] ASC) 
);

 

namespace QR_CODE
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void btnGenerate_Click(object sender, EventArgs e)
        {
            string code = txtCode.Text;
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeGenerator.QRCode qrCode = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
            System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
            imgBarCode.Height = 300;
            imgBarCode.Width = 300;
            using (Bitmap bitMap = qrCode.GetGraphic(20))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    byte[] byteImage = ms.ToArray();
                    imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
                    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
                    SqlCommand cmd = new SqlCommand("INSERT INTO tblQRCode(Data,Name) VALUES(@Data,@Name)", con);

                    cmd.Parameters.AddWithValue("@Data", byteImage);
                    cmd.Parameters.AddWithValue("@Name", txtCode.Text);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    cmd = new SqlCommand("SELECT Data FROM tblQRCode", con);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    gvQRCode.DataSource = dt;
                    gvQRCode.DataBind();
                }
            }
        }
        protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView dr = (DataRowView)e.Row.DataItem;
                string imageUrl = "data:image/jpg;base64," + Convert.ToBase64String((byte[])dr["Data"]);
                (e.Row.FindControl("Image1") as System.Web.UI.WebControls.Image).ImageUrl = imageUrl;
            }
        }
    }
}
<asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
<asp:Button ID="btnGenerate" runat="server" Text="Generate" OnClick="btnGenerate_Click" />
<hr />
<br />
<asp:GridView runat="server" ID="gvQRCode" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound" CssClass="auto-style1" Width="131px">
    <Columns>
        <asp:TemplateField HeaderText="Image">
            <ItemTemplate>
                <asp:Image ID="Image1" runat="server" Height="150px" Width="200px" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

 

Posted 15 days ago