Print barcode from mysql database using C#

Here i am going to explain you that how to print a bar codes from MySQL database. and working with file for printing a bar codes using C# language. It is very simple. Here i will read data from data table and write the data into file to print a bar codes.

Step 1:
Add namespaces which are mentioned below:

using System.Configuration;
using MySql.Data.MySqlClient;
using System.IO;

Step 2:
I have made a function so that i can use it anywhere but you can use on any event also..
Code to print a barcode:

// Function to print a barcode.. Defined it publicly so it can be used at anywhere..

public void printbarcode()
// set connection string
string connect = ConfigurationManager.ConnectionStrings["mycon"].ConnectionString;
MySqlConnection con = new MySqlConnection(connect);

string prodname, areacode, partycode;
int prodrate, prodqty;

// Getting data from database and putting values to variables
cmd = new MySqlCommand("SELECT Prod_Name, Area_Code, Party_Code, Prod_Qty, Prod_Rate FROM TempStockInBarcode WHERE Tran_ID = " +

TextBoxTranID.Text + "", con);
mydr = cmd.ExecuteReader();
while (mydr.Read())
prodname = mydr["Prod_Name"].ToString();
areacode = mydr["Area_Code"].ToString();
partycode = mydr["Party_Code"].ToString();
prodrate = Convert.ToInt32(mydr["Prod_Rate"].ToString());
prodqty = Convert.ToInt32(mydr["Prod_Qty"].ToString());

string filepath = Application.StartupPath + "\\StockBarcode.txt";
// Checking if file is exist or not.. If file does not exist it will create it
if (!File.Exists(filepath))

// writing data for barcode into the StockBarcode.prn file
// Location on barcode to print the values :--> A215,25,0,4,2,1,N,AnyValue

using (StreamWriter sw = File.AppendText(filepath))
sw.WriteLine("A215,25,0,4,2,1,N," + "\"" + prodname + "\"");
sw.WriteLine("A215,55,0,4,2,1,N," + "\"" + areacode + "\"");
sw.WriteLine("A290,135,0,4,2,1,N," + "\"" + partycode + "\"");
sw.WriteLine("A40,135,0,4,1,1,N," + "\"" + "Rs." + "\"");
sw.WriteLine("A95,110,0,3,2,3,N," + "\"" + prodrate + "/-" + "\"");
sw.WriteLine("B205,75,2,3,3,1,50,N," + "\"" + prodrate + "\"");
// P1 is used to print the barcode for one time.. If you want to the print the barcode for 10 times use P10

// Creating a batch file to execute a command a directly to print a barcode
string batchfile = Application.StartupPath + "\\SBarcode.bat";
if (!File.Exists(batchfile))
FileInfo fi = new FileInfo(batchfile);
using (StreamWriter sw = new StreamWriter(fi.Open(FileMode.Truncate)))
// Writing a command to the file
sw.WriteLine("type StockBarcode.prn >prn");
// Executing a command from batch file
catch (Exception ex)
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);


Author: Nirav Lalan21 Mar 2016 Member Level: Gold   Points : 0

Print box means what actually? Can you attach the desired barcode screen shot.

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: