Friday 10 November 2017


 Dynamically generate and display QR code Image in ASP.Net Using C#



Hello Friends i am Navid Shaikh ,in this article I will explain how to dynamically generate and display barcode image using ASP.Net in C# .
For generating QR Codes I will make use of QRCoder which is an Open Source Library QR code generator.
QR Code Library
You will need to download the QR code library from the following location and open the project in Visual Studio and build it. Once it is build, you can find the DLL in the Debug folder.
HTML Markup
I have a simple ASP.Net page with a TextBox where the user will type in the barcode to be generated and a Button to trigger. The generated QR code image will be displayed in the Placeholder.
<form id="form1" runat="server">
<asp:TextBox ID="txt_Code" runat="server"></asp:TextBox>
<asp:Button ID="btnCreate" runat="server" Text="Create" onclick="btnCreate_Click" />
<hr />
<asp:PlaceHolder ID="plCode" runat="server" />
</form>
 
Namespaces
You will need to import the following namespaces.
C#
using QRCoder;
using System.IO;
using System.Drawing;
 
Generating and displaying QR code image in ASP.Net
The following code is of the Button Click event handler. The Text from the TextBox 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 and displayed on the page using an Image control.
C#
protected void btnCreate_Click(object sender, EventArgs e)
{
    string code = txt_Code.Text;
    QRCodeGenerator qrcodeGenerator = new QRCodeGenerator();
    QRCodeGenerator.QRCode qrCode = qrcodeGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
    System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
    imgBarCode.Height = 150;
    imgBarCode.Width = 150;
    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);
        }
        plCode.Controls.Add(imgBarCode);
    }
}
 
Output

Thank You....................





Saturday 29 April 2017


ListBox save multiple selected item into Database


This example shows the basics on how to save multiple selected items from the ASP.Net ListBox control to the database in ASP.Net. Please note that this example requires a basic knowledge of ADO.NET.

STEP1: Setting up the User Interface (GUI)

For the simplicity of this demo, I just set up the web form like below:


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Student Names: <br />
        <asp:ListBox ID="ListBox1" runat="server" Height="149px" SelectionMode="Multiple" Width="113px">
        <asp:ListItem>Ram</asp:ListItem>
        <asp:ListItem>Sham</asp:ListItem>
        <asp:ListItem>Ravi</asp:ListItem>
        <asp:ListItem>Sachin</asp:ListItem>
        <asp:ListItem>Gopal</asp:ListItem>
        <asp:ListItem>Sani</asp:ListItem>
       
        </asp:ListBox>    
    </div>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />
    </form>
</body>
</html>


Note:
* Since the ListBox is intended for multiple item selections then we need to set the SelectionMode attribute of the ListBox to Multiple
* To do multiple Selections in the ListBox then just hold Ctrl key and select the items you want.

STEP 2: Creating a Simple Database Table

Add the following namespaces below:

using System.Data.SqlClient;
using System.Collections.Specialized;
using System.Text;


Here are the code blocks below:

private string GetConnectionString()
{
    //Where DBConnection is the connetion string that was set up in the web config file
    return System.Configuration.ConfigurationManager.ConnectionStrings["DBCon"].ConnectionString;
}

private void InsertRecords(StringCollection sc)
{
    SqlConnection conn = new SqlConnection(GetConnectionString());
    StringBuilder sb = new StringBuilder(string.Empty);
    foreach (string item in sc)
    {
        const string sqlStatement = "INSERT INTO Table1 (Students) VALUES";
        sb.AppendFormat("{0}('{1}'); ", sqlStatement, item);
    }

    try
    {
         conn.Open();
         SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
         cmd.CommandType = CommandType.Text;
         cmd.ExecuteNonQuery();
         Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script""alert('Records Successfuly Saved!');"true);
    }

    catch (System.Data.SqlClient.SqlException ex)
    {
         string msg = "Insert Error:";
         msg += ex.Message;
         throw new Exception(msg);
    }
    finally
    {
         conn.Close();
    }
}

protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
    StringCollection sc = new StringCollection();

    foreach (ListItem item in ListBox1.Items)
    {
         if (item.Selected)
         {
            sc.Add(item.Text);
         }
    }
    InsertRecords(sc);
}

Output:





Thank You