Hi makumbi,
Please refer below sample code.
HTML
Default
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<table>
<tr>
<td>Name:<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Country:<asp:TextBox ID="txtCountry" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="Insert" /></td>
</tr>
</table>
<br />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="CustomerId" DataField="CustomerId" />
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:BoundField HeaderText="Country" DataField="Country" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkSend" Text="Send" runat="server" OnClick="Send"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Panel runat="server" ID="pnlDetails" Visible="false">
<table>
<tr>
<td><asp:Label ID="lblName" runat="server" Text=""></asp:Label></td>
<td><asp:Label ID="lblCountry" runat="server" Text=""></asp:Label></td>
<td><asp:Image ID="imgBarCode" runat="server" /></td>
</tr>
</table>
</asp:Panel>
</asp:Content>
Home
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:Panel runat="server" ID="pnlDetails">
<table>
<tr>
<td><asp:Label ID="lblName" runat="server" Text=""></asp:Label></td>
<td><asp:Label ID="lblCountry" runat="server" Text=""></asp:Label></td>
<td><asp:Image ID="imgBarCode" runat="server" /></td>
</tr>
</table>
</asp:Panel>
<asp:Button ID="btnPrint" runat="server" Text="Print" OnClientClick="return PrintPanel()" />
<script type="text/javascript">
function PrintPanel() {
var contents = document.getElementById('pnlDetails').innerHTML;
var frame1 = document.createElement('iframe');
frame1.name = "frame1";
frame1.style.position = "absolute";
frame1.style.top = "-1000000px";
document.body.appendChild(frame1);
var frameDoc = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.document ? frame1.contentDocument.document : frame1.contentDocument;
frameDoc.document.open();
frameDoc.document.write('<html><head>');
frameDoc.document.write('</head><body>');
frameDoc.document.write(contents);
frameDoc.document.write('</body></html>');
frameDoc.document.close();
window.frames["frame1"].focus();
window.frames["frame1"].print();
document.body.removeChild(frame1);
return false;
}
</script>
</asp:Content>
Namespaces
C#
using System.IO;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.IO
Imports System.Drawing
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
Default
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
protected void Send(object sender, EventArgs e)
{
GridViewRow gvRow = (sender as LinkButton).NamingContainer as GridViewRow;
Response.Redirect("Home.aspx?Id=" + gvRow.Cells[0].Text);
}
protected void Insert(object sender, EventArgs e)
{
int customerId;
string name = txtName.Text;
string country = txtCountry.Text;
txtName.Text = "";
txtCountry.Text = "";
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string sql = "Insert into Customers (Name, Country) Values (@Name, @Country)";
sql += " SELECT SCOPE_IDENTITY()";
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Country", country);
con.Open();
customerId = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
}
this.BindGrid();
lblName.Text = name;
lblCountry.Text = country;
imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(GenerateBarcode(customerId.ToString()) as byte[]);
pnlDetails.Visible = true;
}
private void BindGrid()
{
string sql = "SELECT * FROM Customers";
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
cmd.CommandType = CommandType.Text;
con.Open();
this.gvCustomers.DataSource = cmd.ExecuteReader();
this.gvCustomers.DataBind();
con.Close();
}
}
}
protected byte[] GenerateBarcode(string id)
{
byte[] byteImage;
using (Bitmap bitMap = new Bitmap(id.Length * 40, 80))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
Font oFont = new Font("IDAutomationHC39M Free Version", 16);
PointF point = new PointF(2.0F, 2.0F);
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush whiteBrush = new SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
graphics.DrawString("*" + id + "*", oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byteImage = ms.ToArray();
}
}
return byteImage;
}
Home
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("Select * From Customers WHERE CustomerId = @Id", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Id", Request.QueryString["Id"]);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
lblName.Text = sdr["Name"].ToString();
lblCountry.Text = sdr["Country"].ToString();
imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(GenerateBarcode(sdr["CustomerId"].ToString()) as byte[]);
}
con.Close();
}
}
}
}
protected byte[] GenerateBarcode(string id)
{
byte[] byteImage;
using (Bitmap bitMap = new Bitmap(id.Length * 40, 80))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
Font oFont = new Font("IDAutomationHC39M Free Version", 16);
PointF point = new PointF(2.0F, 2.0F);
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush whiteBrush = new SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
graphics.DrawString("*" + id + "*", oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byteImage = ms.ToArray();
}
}
return byteImage;
}
VB.Net
Default
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Protected Sub Send(ByVal sender As Object, ByVal e As EventArgs)
Dim gvRow As GridViewRow = TryCast((TryCast(sender, LinkButton)).NamingContainer, GridViewRow)
Response.Redirect("Home.aspx?Id=" & gvRow.Cells(0).Text)
End Sub
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
Dim customerId As Integer
Dim name As String = txtName.Text
Dim country As String = txtCountry.Text
txtName.Text = ""
txtCountry.Text = ""
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim sql As String = "Insert into Customers (Name, Country) Values (@Name, @Country)"
sql += " SELECT SCOPE_IDENTITY()"
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand(sql, con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@Country", country)
con.Open()
customerId = Convert.ToInt32(cmd.ExecuteScalar())
con.Close()
End Using
End Using
Me.BindGrid()
lblName.Text = name
lblCountry.Text = country
imgBarCode.ImageUrl = "data:image/png;base64," & Convert.ToBase64String(TryCast(GenerateBarcode(customerId.ToString()), Byte()))
pnlDetails.Visible = True
End Sub
Private Sub BindGrid()
Dim sql As String = "SELECT * FROM Customers"
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand(sql, con)
cmd.CommandType = CommandType.Text
con.Open()
Me.gvCustomers.DataSource = cmd.ExecuteReader()
Me.gvCustomers.DataBind()
con.Close()
End Using
End Using
End Sub
Protected Function GenerateBarcode(ByVal id As String) As Byte()
Dim byteImage As Byte()
Using bitMap As Bitmap = New Bitmap(id.Length * 40, 80)
Using graphics As Graphics = Graphics.FromImage(bitMap)
Dim oFont As Font = New Font("IDAutomationHC39M Free Version", 16)
Dim point As PointF = New PointF(2.0F, 2.0F)
Dim blackBrush As SolidBrush = New SolidBrush(Color.Black)
Dim whiteBrush As SolidBrush = New SolidBrush(Color.White)
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height)
graphics.DrawString("*" & id & "*", oFont, blackBrush, point)
End Using
Using ms As MemoryStream = New MemoryStream()
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
byteImage = ms.ToArray()
End Using
End Using
Return byteImage
End Function
Home
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("Select * From Customers WHERE CustomerId = @Id", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Id", Request.QueryString("Id"))
con.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader()
If sdr.Read() Then
lblName.Text = sdr("Name").ToString()
lblCountry.Text = sdr("Country").ToString()
imgBarCode.ImageUrl = "data:image/png;base64," & Convert.ToBase64String(TryCast(GenerateBarcode(sdr("CustomerId").ToString()), Byte()))
End If
con.Close()
End Using
End Using
End If
End Sub
Protected Function GenerateBarcode(ByVal id As String) As Byte()
Dim byteImage As Byte()
Using bitMap As Bitmap = New Bitmap(id.Length * 40, 80)
Using graphics As Graphics = Graphics.FromImage(bitMap)
Dim oFont As Font = New Font("IDAutomationHC39M Free Version", 16)
Dim point As PointF = New PointF(2.0F, 2.0F)
Dim blackBrush As SolidBrush = New SolidBrush(Color.Black)
Dim whiteBrush As SolidBrush = New SolidBrush(Color.White)
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height)
graphics.DrawString("*" & id & "*", oFont, blackBrush, point)
End Using
Using ms As MemoryStream = New MemoryStream()
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
byteImage = ms.ToArray()
End Using
End Using
Return byteImage
End Function
Screenshot