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);
try
{
string prodname, areacode, partycode;
int prodrate, prodqty;
con.Open();
// 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))
{
File.Create(filepath).Dispose();
}
// 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("N");
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
sw.WriteLine("P1");
sw.Close();
}
}
mydr.Dispose();
// 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);
File.Create(batchfile).Dispose();
using (StreamWriter sw = new StreamWriter(fi.Open(FileMode.Truncate)))
{
// Writing a command to the file
sw.WriteLine("type StockBarcode.prn >prn");
sw.Close();
}
}
// Executing a command from batch file
Process.Start(batchfile);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
con.Close();
}
}
}
Print box means what actually? Can you attach the desired barcode screen shot.