Tuesday 29 January 2013

how to find TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE of a table


SELECT        TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE
FROM            INFORMATION_SCHEMA.COLUMNS
WHERE        (TABLE_NAME = 'EMP')

Monday 28 January 2013

example of virtual and override

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication23
{
    class DML
    {
     protected int a, b;
       public DML(int x, int y)
       {
           a = x;
           b = y;
       }
       public virtual int add()
       {
           return a + b;
       }
    }
    class DML1:DML
    {
        public DML1(int m,int n)
           :base(m,n)
        { }
        public override int add()
        {
            return a + b + 5;
        }
    }
}

private void button1_Click_1(object sender, EventArgs e)
        {
            DML o = new DML(10,15);
           textBox1.Text = o.add().ToString();
        }
out put
-------------------------
25
-------------------------

private void button1_Click_1(object sender, EventArgs e)
        {
            DML o = new DML1(10,15);
           textBox1.Text = o.add().ToString();
        }
out put
-------------------------
30
-------------------------

Sunday 27 January 2013

how to validate money in c#.net

  private void textBox1_Leave(object sender, EventArgs e)
        {
            textBox1.Text = decimal.Parse(textBox1.Text).ToString("0.00");
        }

jQuery filter() example

<html>
<head>
<title>jQuery filter example</title>
 
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
 
</head>
 
<body>
 
<h1>jQuery filter example</h1>
 
<script type="text/javascript">
 
  $(document).ready(function(){
 
    $("#filterSelector").click(function () {
 
	$('div').css('background-color', 'white');
 
	$("div").filter("#div1").css('background-color', 'blue');
 
    });
 
    $("#filterFunction").click(function () {
 
	$('div').css('background-color', 'white');
 
	$('div').filter(function(index) {
		if(index==2 || index==3){ //0 index based
			return true;
		}
	}).css('background-color', 'blue');
 
    });
 
    $("#filterFunction2").click(function () {
 
	$('div').css('background-color', 'white');
 
	$('div').filter(function(index) {
		return $("b", this).length == 1;
	}).css('background-color', 'blue');
 
    });
 
  });
</script>
</head><body>
 
<div id="div1">
	<b>This is div 1 with 'b' tag</b>
</div>
<div id="div2">
	This is div 2
</div>
<div id="div3">
	<b>This is div 3 with 'b' tag</b>
</div>
<div id="div4">
	This is div 4
</div>
 
<br/>
<br/>
<br/>
 
<input type='button' value='filter(selector)' id='filterSelector'>
<input type='button' value='filter(index)' id='filterFunction'>
<input type='button' value='filter(index)+b' id='filterFunction2'>
</body>
</html>
------------------ 
  <section id="lists">
       <ul class="itemlist">
          <li>Item 0</li>
          <li>Item 1</li>
          <li>Item 2</li>
          <li>Item 3</li>
          <li>Item 4</li>
          <li>Item 5</li>
          <li>Item 6</li>
          <li>Item 7</li>
      </ul>
    <input type="button" id="list" value="Even Listed Items" />
   </section>
And, now we will add the following lines of jQuery inside the document event function of the index.html file:
$('#list').click(function () {   $('ul.itemlist li:even').toggleClass('even'); }); Next, we will add in the following styles to the styles.css file:
#lists {      width:400px;      border: 2px solid #C63;      padding:5px;      margin-top:20px; } .even {      background: #CFC; } Be sure to save the files, then open the index.html file in your browser, and then click on the Even Listed Items button. You will see that the even listed items are now rendered with a light green background; click again, and the background reverts to none. The example list is displayed below in Figures A and B as displayed in Chrome 17.0.9 in:

Figure A


Figure B


jQuery has many other selectors that can be utilized in varying ways, for example, to automatically style tables with every other row assigned a different styling, such as background style.
Add this jQuery code into the document event handler within the <script> area of index.html:
     $('table.odd_row tr:odd').addClass('odd_row'); With this styling added into the styles.css:
tr.odd_row {      background: #CDCDCD; } Add the following snippet to the index.html file, which will produce the table below with eight rows:
<section id="tables">      <table class="odd_row">        <tbody>          <tr>            <th>Row 0</th>            <th>Column</th>            <th>Column</th>          </tr>          <tr>            <td>Row 1</td>            <td>Column</td>            <td>Column</td>          </tr>           .           .        </tbody>      </table> </section> If you are following along with the coding of the demonstrations, be sure to save your index.html, and styles.css files, then refresh your browser to view. The results of this table view are displayed in Chrome 17.0.9 in Figure C:

Figure C


Change an object’s content text

Maybe you have an alert message that needs to be updated, and the change is applied in multiple locations across your website. With jQuery you only have to make the text entry once, and it gets applied automatically to the matched set of elements with the .text(textString) function. The .text() function can be used in both XML and HTML documents.
In the index.html file, add in this line of jQuery code just below the previous line we added and inside the document ready function:
$(".test").text("Alert: This is the new text content!"); Save the file, then refresh the page in your browser, and you will see that the content text for the article and section with the class=”test” were updated with the new text: “Alert: This is the new text content!.” jQuery does all the work of applying the text update across all elements with the defined class. Imagine if this was an update for 100+ objects — the effect is the same and immediate across the site.
The resulting document as displayed in Chrome 17.0.9 is shown in Figure D:

Figure D


In the next installments of the jQuery series, we will review changing the appearances of objects with the show and hide functions, and finally, we will create an accordion effect for an FAQ document using the power of jQuery.
:first Selects the first matched element of the selector’s returned set
:last Selects the last and single instance of the element matching the selector’s returned set
:even Selects the even elements with a zero-based index within the matched set
:odd Selects the odd elements with zero-based indexing within the matched set
:eq(index) Select the element that is equal to the given index n within the matched set
:gt(index) Select all elements that are greater than the given zero-based index within the matched set
:lt(index) Select all elements that are less than the given zero-based index within the matched set
:animated Selects all elements that are currently being animated at the time the selector is run
:header Selects all header elements (H1…H6)
:not Select all elements that do not match the given selector
:checkbox Select all elements that match the type checkbox
:contains(text) Select a string of text (case sensitive)
:disabled Selects all elements that are disabled
:enabled Selects all elements that are enabled
:file Selects all elements of a certain file type

barcode

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Swash.Objects;
using Swash.BusinessLayer;
using System.Drawing.Printing;
using System.Drawing.Imaging;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Drawing.Drawing2D;
using System.IO;
using System.Globalization;
using System.Drawing.Text;

namespace Barcodes
{
    public partial class frmPrintBarcode : Form
    {
        List<System.Drawing.Image> imgList = new List<System.Drawing.Image>();
        List<barList> Bars = new List<barList>();
        //Image img;
        class barList
        {
            public System.Drawing.Image BarcodeImage { get; set; }
            public string Barcode { get; set; }
            public string GrossWt { get; set; }
            public string NetWt { get; set; }
            public string Price { get; set; }
            public string Grade { get; set; }
            public string ItemName { get; set; }
            public string StoreName { get; set; }
            public string Colour { get; set; }
            public string Size { get; set; }
            public string StyleNo { get; set; }
            public string BrandName { get; set; }
            public string GoldCategory { get; set; }
            public string SilverPurity { get; set; }
            public string SilverCategory { get; set; }
            public string Metal { get; set; }
            public string ProductCategory { get; set; }
            public string ProductType { get; set; }
            public int Quantity { get; set; }
            public string StoreMRP { get; set; }
            public string Color { get; set; }
            public string Stone_Name { get; set; }
            public string Gold_Purity { get; set; }
            public string Product_Type { get; set; }
            public string Item_Type_Name { get; set; }
       

        }
        private Ean13 ean13 = null;
        DataTable dt;
        TAGGING_Item obj = new TAGGING_Item();
        ERPManagement objdml = new ERPManagement();
        static int Total = 0;
        static int TotalCopies = 0;
        public frmPrintBarcode()
        {
            InitializeComponent();
        }
        protected override void WndProc(ref Message message)
        {
            const int WM_SYSCOMMAND = 0x0112;
            const int SC_MOVE = 0xF010;
            switch (message.Msg)
            {
                case WM_SYSCOMMAND:
                    int command = message.WParam.ToInt32() & 0xfff0;
                    if (command == SC_MOVE)
                        return;
                    break;
            }
            base.WndProc(ref message);
        }
        #region Events
        private void frmPrintBarcode_Load(object sender, EventArgs e)
        {
            try
            {
                RbtnBarcode.Checked = true;
                CmbBrand.Enabled = false;
                CmbItemName.Enabled = false;
                CmbItemType.Enabled = false;
                Fillbrand();
                CreateDataTable();
                DefaultDropdowns();
            }
            catch (Exception ex)
            {
            }
        }
        private void RbtnBarcode_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                // Reset();
                if (RbtnBarcode.Checked == true)
                {
                    TxtBarcode.Enabled = true;
                    TxtBarcode.Focus();
                    CmbBrand.SelectedIndex = -1;
                    CmbBrand.Enabled = false;
                    CmbItemType.SelectedIndex = -1;
                    CmbItemName.SelectedIndex = -1;
                    CmbItemName.Enabled = false;
                    CmbItemType.Enabled = false;
                    DgviewBarcode.DataSource = null;
                    DgviewBarcode.Columns.Clear();

                }
            }
            catch (Exception ex)
            {
            }
        }
        private void RbtnBrand_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                //  Reset();
                if (RbtnBrand.Checked == true)
                {
                    Fillbrand();
                    DefaultDropdowns();
                    TxtBarcode.Text = "";
                    TxtBarcode.Enabled = false;
                    CmbBrand.Enabled = true;
                    CmbItemName.Enabled = false;
                    CmbItemType.Enabled = false;
                    CmbBrand.Focus();
                    DgviewBarcode.DataSource = null;
                    DgviewBarcode.Columns.Clear();
                }
            }
            catch (Exception ex)
            {
            }
        }
        private void CmbBrand_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (CmbBrand.SelectedIndex > 0)
                {
                    fillItemType(int.Parse(CmbBrand.SelectedValue.ToString()));
                    List<TAGGING_PrintBarcode> Listobj = new List<TAGGING_PrintBarcode>();
                    TAGGING_PrintBarcode Select = new TAGGING_PrintBarcode();
                    Select.Item_ID = 0;
                    Select.Item_Name = "Select";
                    Listobj.Add(Select);
                    CmbItemName.DataSource = Listobj;
                    CmbItemName.ValueMember = "Item_ID";
                    CmbItemName.DisplayMember = "Item_Name";
                }
                else
                {
                    CmbItemName.Enabled = false;
                    CmbItemType.Enabled = false;
                    DefaultDropdowns();
                }
                DgviewBarcode.Columns[0].Visible = false;
                DgviewBarcode.Columns[1].Visible = false;
                if (DgviewBarcode.Columns.Contains("Copies"))
                {
                    DgviewBarcode.Columns.Remove("Copies");
                }
                DgviewBarcode.DataSource = null;
                DgviewBarcode.ColumnHeadersVisible = false;// Added
                TxtQuantity.Text = "0";

            }
            catch (Exception ex)
            {

            }
        }
        private void CmbItemType_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (CmbItemType.SelectedIndex > 0)
                {
                    fillItemName(int.Parse(CmbItemType.SelectedValue.ToString()));
                }
                else
                {
                    CmbItemName.Enabled = false;
                    List<TAGGING_PrintBarcode> Listobj = new List<TAGGING_PrintBarcode>();
                    TAGGING_PrintBarcode Select = new TAGGING_PrintBarcode();
                    Select.Item_ID = 0;
                    Select.Item_Name = "Select";
                    Listobj.Add(Select);
                    CmbItemName.DataSource = Listobj;
                    CmbItemName.ValueMember = "Item_ID";
                    CmbItemName.DisplayMember = "Item_Name";
                }
                DgviewBarcode.Columns[0].Visible = false;
                DgviewBarcode.Columns[1].Visible = false;
                if (DgviewBarcode.Columns.Contains("Copies"))
                {
                    DgviewBarcode.Columns.Remove("Copies");
                }
                DgviewBarcode.DataSource = null;
                DgviewBarcode.ColumnHeadersVisible = false; // added
                TxtQuantity.Text = "0";

            }
            catch (Exception ex)
            {

            }
        }

        private void BtnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                Search();
            }
            catch (Exception ex)
            {
            }
        }
        private void TxtBarcode_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyValue == 13)
                {
                    Search();
                }
            }
            catch (Exception ex)
            {
            }
        }
        private void DgviewBarcode_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (checkquantity() == true)
                {
                    int x = 0, j = 0;
                    for (int i = 0; i < DgviewBarcode.Rows.Count; i++)
                    {
                        if (DgviewBarcode.Rows[i].Cells[9].Value != "" && DgviewBarcode.Rows[i].Cells[9].Value != null)
                        {
                            j = int.Parse(DgviewBarcode.Rows[i].Cells[9].Value.ToString());
                            x = x + j;
                        }
                    }
                    TxtQuantity.Text = x.ToString();
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("Object reference not set to an instance of an object."))
                {
                    return;
                }

            }

        }

        bool checkquantity()
        {
            bool ReturnValue = false;
            try
            {
                if (DgviewBarcode.Rows.Count > 0)
                {
                    for (int i = 0; i < DgviewBarcode.Rows.Count; i++)
                    {
                        int result = 0;
                        int count = DgviewBarcode.Columns.Count;
                      //  string abc = DgviewBarcode.Rows[i].Cells[6].Value.ToString();
                        if (DgviewBarcode.Rows[i].Cells[9].Value != null && DgviewBarcode.Rows[i].Cells[9].Value != "")
                        {

                            if (int.TryParse(DgviewBarcode.Rows[i].Cells[9].Value.ToString(), out result))
                            {
                                if (int.Parse(DgviewBarcode.Rows[i].Cells[9].Value.ToString()) < 0)
                                {
                                    MessageBox.Show("Copies can't be negative.", "KenCloud", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                    DataGridViewTextBoxCell txt = (DataGridViewTextBoxCell)(DgviewBarcode.Rows[i].Cells[9]);
                                    DgviewBarcode.Rows[i].Cells[9].Value = "";
                                    txt.Selected = true;
                                    ReturnValue = false;
                                }
                                else if (int.Parse(DgviewBarcode.Rows[i].Cells[9].Value.ToString()) == 0)
                                {
                                    MessageBox.Show("Copies can't be zero.", "KenCloud", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                    DataGridViewTextBoxCell txt = (DataGridViewTextBoxCell)(DgviewBarcode.Rows[i].Cells[9]);
                                    DgviewBarcode.Rows[i].Cells[9].Value = "";
                                    txt.Selected = true;
                                    ReturnValue = false;
                                }
                                else
                                {
                                    ReturnValue = true;
                                }
                            }
                            else
                            {
                                MessageBox.Show("Enter only integers.", "Kencloud", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                DgviewBarcode.Rows[i].Cells[9].Value = "";
                                ReturnValue = false;
                            }
                        }
                        else
                        {
                            ReturnValue = true;
                        }

                    }
                }
            }
            catch (Exception ex)
            {

            }
            return ReturnValue;
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            try
            {

                Reset();
            }
            catch (Exception ex)
            {
            }
        }

        private void BtnPrintBarcode_Click(object sender, EventArgs e)
        {
            try
            {
                if (DgviewBarcode.Rows.Count > 0)
                {
                    SaveBarcode();
                }
                else
                {
                    MessageBox.Show("No items has been selected.", "Ken Cloud", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    TxtBarcode.Focus();
                    return;
                }
            }
            catch (Exception ex)
            {
            }
        }

        #endregion

        #region Method
        void fillItemType(int value)
        {
            List<TAGGING_PrintBarcode> Listobj = new List<TAGGING_PrintBarcode>();
            TAGGING_PrintBarcode Type = new TAGGING_PrintBarcode();
            Type.Item_Type_Name = "Select";
            Type.Item_Type_ID = 0;
            Listobj.Add(Type);
            var x = (from m in ERPManagement.GetInstance.GetAllItemBrandDetails()
                    where m.Brand_ID == value
                    select m );
            var y = (from s in x
                     select s.Item_Type_ID).Distinct().ToList();
            List<int> it=new List<int>();
            for(int i=0; i<x.ToList().Count; i++)
            {
                it.Add(x.ToList()[i].Item_Type_ID);
            }            
            for (int i = 0; i < y.Count(); i++)
            {
               
                if (it.Contains(y[i]))
                {
                   var srch= (from t in x
                             where t.Item_Type_ID == y[i]
                                  select t.Item_Type_Name).Distinct();
                   TAGGING_PrintBarcode ch=new TAGGING_PrintBarcode();
                    ch.Item_Type_ID=y[i];
                    ch.Item_Type_Name = srch.ToList()[0];
                    //ch.Item_Type_Name=
                    Listobj.Add(ch);
                }
            }

            //for (int i = 0; i < x.Count(); i++)
            //{
            //    Listobj.Add(x.ToList()[i]);
            //}
            CmbItemType.DataSource = Listobj;
            CmbItemType.DisplayMember = "Item_Type_Name";
            CmbItemType.ValueMember = "Item_Type_ID";
            CmbItemType.Enabled = true;
        }

        void fillItemName(int ItemtypeID)
        {
            List<TAGGING_PrintBarcode> Listobj = new List<TAGGING_PrintBarcode>();
            TAGGING_PrintBarcode Select = new TAGGING_PrintBarcode();
            Select.Item_ID = 0;
            Select.Item_Name = "Select";
            Listobj.Add(Select);
            var x = from m in ERPManagement.GetInstance.GetAllItemBrandDetails()
                    where m.Item_Type_ID == ItemtypeID
                    select m;
            for (int i = 0; i < x.Count(); i++)
            {
                Listobj.Add(x.ToList()[i]);
            }
            CmbItemName.DataSource = Listobj;
            CmbItemName.DisplayMember = "Item_Name";
            CmbItemName.ValueMember = "Item_ID";
            CmbItemName.Enabled = true;
        }

        void Reset()
        {
            TxtBarcode.Text = "";
            TxtQuantity.Text = "0";
            CmbBrand.SelectedIndex = -1;
            CmbBrand.DataSource = null;
            CmbItemType.SelectedIndex = -1;
            CmbItemType.DataSource = null;
            CmbItemName.SelectedIndex = -1;
            CmbItemName.DataSource = null;
            DgviewBarcode.Columns.Clear();
            DgviewBarcode.DataSource = null;
            RbtnBrand.Checked = false;
            CmbBrand.Enabled = false;
            CmbItemType.Enabled = false;
            CmbItemName.Enabled = false;
            RbtnBarcode.Checked = true;
            TxtBarcode.Enabled = true;

        }

        void Fillbrand()
        {
            obj.T_Name = "Inventory.MST_Brand";
            obj.Vf_Name = "Brand_ID";
            obj.Tf_Name = "Brand_Name";
            CmbBrand.DataSource = objdml.GetItemData1(obj);
            CmbBrand.DisplayMember = "Text";
            CmbBrand.ValueMember = "Value";
        }

        void Search()
        {
            try
            {
                List<TAGGING_PrintBarcode> ListobjBarcode = new List<TAGGING_PrintBarcode>();
                //ListobjBarcode = ERPManagement.GetInstance.GetAllBarcodeDetails();
                ListobjBarcode = ERPManagement.GetInstance.GetAllBarcodeDetailsForPrint();
                if (TxtBarcode.Enabled == false)
                {
                    if (CmbBrand.SelectedIndex > 0)
                    {
                       
                        // Code to retrive all the records when all combo box are checked
                        if (CmbItemType.SelectedIndex > 0 && CmbItemName.SelectedIndex > 0)
                        {
                            var lnqBarcode = from Barcode in ListobjBarcode
                                             where Barcode.Item_ID == int.Parse(CmbItemName.SelectedValue.ToString())
                                             select Barcode;
                            if (lnqBarcode.ToList().Count > 0)
                            {
                                GenerateGridView(DgviewBarcode, lnqBarcode.ToList());
                                DgviewBarcode.ColumnHeadersVisible = true;
                                DgviewBarcode.ScrollBars = ScrollBars.Both;
                                //if (DgviewBarcode.Rows.Count > 0)
                                //{
                                //    DgviewBarcode.Rows[0].Cells[6].Selected = true;
                                //}
                            }
                            else
                            {
                                DgviewBarcode.DataSource = null;
                                DgviewBarcode.ColumnHeadersVisible = false;
                                DgviewBarcode.ScrollBars = ScrollBars.None;
                            }
                        }
                        // end of code to retrive all the records when all combo box are checked
                        // For Brand and Item type
                        else if (CmbItemType.SelectedIndex > 0)
                        {
                            var lnqBarcode = from Barcode in ListobjBarcode
                                             where Barcode.Item_Type_ID == int.Parse(CmbItemType.SelectedValue.ToString())
                                             && Barcode.Brand_ID == int.Parse(CmbBrand.SelectedValue.ToString())
                                             select Barcode;
                            if (lnqBarcode.ToList().Count > 0)
                            {
                                GenerateGridView(DgviewBarcode, lnqBarcode.ToList());
                                DgviewBarcode.ColumnHeadersVisible = true;
                                DgviewBarcode.ScrollBars = ScrollBars.Both;
                            }
                            else
                            {
                                DgviewBarcode.ColumnHeadersVisible = false;
                                DgviewBarcode.DataSource = null;
                                DgviewBarcode.ScrollBars = ScrollBars.None;
                            }
                        }
                        // end of Brand and Item type
                        // For only Brand Combo box
                        else
                        {
                            var lnqBarcode = from Barcode in ListobjBarcode
                                             where Barcode.Brand_ID == int.Parse(CmbBrand.SelectedValue.ToString())
                                             select Barcode;
                            if (lnqBarcode.ToList().Count > 0)
                            {
                                GenerateGridView(DgviewBarcode, lnqBarcode.ToList());
                                DgviewBarcode.ColumnHeadersVisible = true;
                                DgviewBarcode.ScrollBars = ScrollBars.Both;
                            }
                            else
                            {
                                DgviewBarcode.ColumnHeadersVisible = false;
                                DgviewBarcode.DataSource = null;
                                DgviewBarcode.ScrollBars = ScrollBars.None;
                            }
                        }
                        // end of only Brand
                        // Old Code get Commented
                        //if (CmbItemType.SelectedIndex > 0)
                        //{
                        //    if (CmbItemName.SelectedIndex > 0)
                        //    {
                        //        var lnqBarcode = from Barcode in ListobjBarcode
                        //                         where Barcode.Item_ID == int.Parse(CmbItemName.SelectedValue.ToString())
                        //                         select Barcode;
                        //        if (lnqBarcode.ToList().Count > 0)
                        //        {
                        //            GenerateGridView(DgviewBarcode, lnqBarcode.ToList());
                        //            //if (DgviewBarcode.Rows.Count > 0)
                        //            //{
                        //            //    DgviewBarcode.Rows[0].Cells[6].Selected = true;
                        //            //}
                        //        }
                        //        else
                        //        {
                        //            DgviewBarcode.DataSource = null;
                        //        }
                        //    }
                        //    else
                        //    {
                        //        MessageBox.Show("Select an item name.", "Kencloud", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        //        CmbItemName.Focus();
                        //        return;
                        //    }
                        //}
                        //else
                        //{
                        //    MessageBox.Show("Select an item type.", "Kencloud", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        //    CmbItemType.Focus();
                        //    return;
                        //}
                        // end of Old Code
                    }
                    else
                    {
                        MessageBox.Show("Select a brand.", "Kencloud", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        CmbBrand.Focus();
                        return;
                    }
                }
                else
                {
                    if (TxtBarcode.Text.Trim() != "")
                    {
                        SearchonBarcode();
                    }
                    else
                    {
                        MessageBox.Show("Enter barcode no.", "Kencloud", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        TxtBarcode.Focus();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
            }

        }

        void GenerateGridView(DataGridView dg, List<TAGGING_PrintBarcode> barcodes)
        {
            DgviewBarcode.Columns.Clear();
            dg.AutoGenerateColumns = false;
            DataGridViewCheckBoxColumn check = new DataGridViewCheckBoxColumn();
            check.Width = 30;
            dg.Columns.Add(check);
            DataGridViewTextBoxColumn Barcode = new DataGridViewTextBoxColumn();
            Barcode.DataPropertyName = "Barcode";
            Barcode.HeaderText = "Barcode";
            Barcode.Width = 160;
            Barcode.ReadOnly = true;
            dg.Columns.Add(Barcode);
            DataGridViewTextBoxColumn Brand = new DataGridViewTextBoxColumn();
            Brand.DataPropertyName = "Brand_Name";
            Brand.HeaderText = "Brand";
            Brand.Width = 160;
            Brand.ReadOnly = true;
            dg.Columns.Add(Brand);
            DataGridViewTextBoxColumn ItemTypeName = new DataGridViewTextBoxColumn();
            ItemTypeName.DataPropertyName = "Item_Type_Name";
            ItemTypeName.HeaderText = "Item Type";
            ItemTypeName.Width = 160;
            ItemTypeName.ReadOnly = true;
            dg.Columns.Add(ItemTypeName);
            DataGridViewTextBoxColumn ProductType = new DataGridViewTextBoxColumn();
            ProductType.DataPropertyName = "Product_Type";
            ProductType.HeaderText = "Product Type";
            ProductType.Width = 160;
            ProductType.ReadOnly = true;
            dg.Columns.Add(ProductType);
            DataGridViewTextBoxColumn GrossWt = new DataGridViewTextBoxColumn();
            GrossWt.DataPropertyName = "Total_Gross_Weight";
            GrossWt.HeaderText = "Gross Wt.";
            GrossWt.Width = 100;
            GrossWt.ReadOnly = true;
            dg.Columns.Add(GrossWt);
            DataGridViewTextBoxColumn StoneWt = new DataGridViewTextBoxColumn();
            StoneWt.DataPropertyName = "Total_Stone_Weight";
            StoneWt.HeaderText = "Stone Wt.";
            StoneWt.Width = 100;
            StoneWt.ReadOnly = true;
            dg.Columns.Add(StoneWt);
            DataGridViewTextBoxColumn NetWt = new DataGridViewTextBoxColumn();
            NetWt.DataPropertyName = "Total_Net_Weight";
            NetWt.HeaderText = "Net Wt.";
            NetWt.Width = 100;
            NetWt.ReadOnly = true;
            dg.Columns.Add(NetWt);
            DataGridViewTextBoxColumn StoneColor = new DataGridViewTextBoxColumn();
            StoneColor.DataPropertyName = "Stone_Color";
            StoneColor.HeaderText = "Stone Color";
            StoneColor.Width = 140;
            StoneColor.ReadOnly = true;
            dg.Columns.Add(StoneColor);
            DataGridViewTextBoxColumn Quantity = new DataGridViewTextBoxColumn();
            Quantity.HeaderText = "Copies";
            Quantity.Width = 140;
            Quantity.ReadOnly = false;
            //Quantity.DefaultCellStyle.BackColor = System.Drawing.Color.LightBlue;
            dg.Columns.Add(Quantity);
            DataGridViewTextBoxColumn MRP = new DataGridViewTextBoxColumn();
            MRP.HeaderText = "Price";
            MRP.DataPropertyName = "MRP";
            MRP.Width = 140;
            MRP.ReadOnly = true;
            MRP.Visible = false;
            dg.Columns.Add(MRP);
            DataGridViewTextBoxColumn ItemName = new DataGridViewTextBoxColumn();
            ItemName.HeaderText = "Item Name";
            ItemName.DataPropertyName = "Item_Name";
            ItemName.Width = 140;
            ItemName.ReadOnly = true;
            ItemName.Visible = false;
            dg.Columns.Add(ItemName);
            //DataGridViewTextBoxColumn Store = new DataGridViewTextBoxColumn();
            //Store.HeaderText = "Store Name";
            //Store.DataPropertyName = "StoreName";
            //Store.Width = 140;
            //Store.ReadOnly = true;
            //Store.Visible = false;
            //dg.Columns.Add(Store);
            DataGridViewTextBoxColumn Purity = new DataGridViewTextBoxColumn();
            Purity.HeaderText = "Purity";
            Purity.DataPropertyName = "PurityName";
            Purity.Width = 140;
            Purity.ReadOnly = true;
            Purity.Visible = false;
            dg.Columns.Add(Purity);
            DataGridViewTextBoxColumn StyleNo = new DataGridViewTextBoxColumn();
            StyleNo.HeaderText = "Style No";
            StyleNo.DataPropertyName = "Style_No";
            StyleNo.Width = 140;
            StyleNo.ReadOnly = true;
            StyleNo.Visible = true;
            dg.Columns.Add(StyleNo);
            DataGridViewTextBoxColumn Size = new DataGridViewTextBoxColumn();
            Size.HeaderText = "Size";
            Size.DataPropertyName = "Size";
            Size.Width = 140;
            Size.ReadOnly = true;
            Size.Visible = true;
            dg.Columns.Add(Size);
            DataGridViewTextBoxColumn SilverPurity = new DataGridViewTextBoxColumn();
            SilverPurity.HeaderText = "Silver Purity";
            SilverPurity.DataPropertyName = "Silver_Purity_Name";
            SilverPurity.Width = 140;
            SilverPurity.ReadOnly = true;
            SilverPurity.Visible = true;
            dg.Columns.Add(SilverPurity);
            dg.DataSource = barcodes;
            DataGridViewTextBoxColumn StoneName = new DataGridViewTextBoxColumn();
            StoneName.HeaderText = "Stone Name";
            StoneName.DataPropertyName = "Stone_Name";
            StoneName.Width = 140;
            StoneName.ReadOnly = true;
            StoneName.Visible = true;
            dg.Columns.Add(StoneName);
            dg.DataSource = barcodes;
            DataGridViewTextBoxColumn StoreMRP = new DataGridViewTextBoxColumn();
            StoreMRP.HeaderText = "Store MRP";
            StoreMRP.DataPropertyName = "Store_MRP";
            StoreMRP.Width = 140;
            StoreMRP.ReadOnly = true;
            StoreMRP.Visible = true;
            dg.Columns.Add(StoreMRP);
            dg.DataSource = barcodes;

        }

        void SearchonBarcode()
        {
            List<TAGGING_PrintBarcode> ListobjBarcode = new List<TAGGING_PrintBarcode>();
            ListobjBarcode = ERPManagement.GetInstance.GetAllBarcodeDetailsForPrint();
            var lnqBarcode = from Barcode in ListobjBarcode
                             where Barcode.Barcode == TxtBarcode.Text.Trim()
                             select Barcode;
            if (lnqBarcode.ToList().Count > 0)
            {
                GenerateGridView(DgviewBarcode, lnqBarcode.ToList());
            }
            else
            {
                MessageBox.Show("Wrong Barcode.", "Kencloud", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DgviewBarcode.DataSource = null;
                DgviewBarcode.Columns[0].Visible = false;
                DgviewBarcode.Columns[1].Visible = false;
                TxtBarcode.Text = "";
            }
        }

        void SetGrid()
        {
        }

        void CreateDataTable()
        {
            dt = new DataTable();
            dt.Columns.Add("Barcode_Item", typeof(string));
            dt.Columns.Add("No_Of_Copies", typeof(int));
        }

        bool CheckDataTable()
        {
            bool insert = false;
            if (DgviewBarcode.Rows.Count > 0)
            {

                for (int i = 0; i < DgviewBarcode.Rows.Count; i++)
                {
                    if (DgviewBarcode.Rows[i].Cells[0].Value != null)
                    {
                        if ((bool)this.DgviewBarcode.Rows[i].Cells[0].Value == true)
                        {
                            if (DgviewBarcode.Rows[i].Cells[9].Value == null)
                            {
                                MessageBox.Show(" Enter copies to print.", "Kencloud", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                insert = false;
                            }
                            else
                            {
                                insert = true;
                            }

                        }
                    }
                }
            }
            return insert;
        }

        void InsertDataTable()
        {
            if (DgviewBarcode.Rows.Count > 0)
            {
                dt.Clear();
                for (int i = 0; i < DgviewBarcode.Rows.Count; i++)
                {

                    if (DgviewBarcode.Rows[i].Cells[0].Value != null)
                    {
                        if ((bool)this.DgviewBarcode.Rows[i].Cells[0].Value == true)
                        {
                            dt.Rows.Add(DgviewBarcode.Rows[i].Cells[1].Value,
                                DgviewBarcode.Rows[i].Cells[9].Value);
                            dt.AcceptChanges();

                        }
                    }
                }
            }
        }

        int Copyquantity()
        {
            int m = 0, n = 0;
            if (DgviewBarcode.Rows.Count > 0)
            {

                for (int i = 0; i < DgviewBarcode.Rows.Count; i++)
                {
                    if (DgviewBarcode.Rows[i].Cells[0].Value != null)
                    {
                        if ((bool)this.DgviewBarcode.Rows[i].Cells[0].Value == true)
                        {
                            if (DgviewBarcode.Rows[i].Cells[9].Value == null)
                            {
                            }
                            else
                            {
                                n = int.Parse(DgviewBarcode.Rows[i].Cells[9].Value.ToString());
                                m = m + n;
                            }

                        }
                    }
                }
            }
            return m;
        }

        void SaveBarcode()
        {
            if (TxtQuantity.Text != "0" && TxtQuantity.Text != "")
            {
                if (CheckDataTable() == true)
                {
                    TotalCopies = Copyquantity();
                    if (MessageBox.Show("Are you sure to print " + TotalCopies + " no of barcode(s) ?", "kenCloud", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        imgList.Clear();
                        Bars.Clear();
                        string Size="";
                        string GrossWt = "";
                        string Price = "";
                        for (int i = 0; i < DgviewBarcode.Rows.Count; i++)
                        {
                            if (DgviewBarcode.Rows[i].Cells[9].Value != null)
                            {
                                string Barcode = DgviewBarcode.Rows[i].Cells[1].Value.ToString();
                                string BrandName = DgviewBarcode.Rows[i].Cells[2].Value.ToString();
                                Total = int.Parse(DgviewBarcode.Rows[i].Cells[9].Value.ToString());
                             
                                if (DgviewBarcode.Rows[i].Cells[5].Value != null)
                                {
                                      GrossWt = DgviewBarcode.Rows[i].Cells[5].Value.ToString();
                                }  
                                string NetWt = DgviewBarcode.Rows[i].Cells[7].Value.ToString();
                                if (DgviewBarcode.Rows[i].Cells[2].Value.ToString() == "Milaan")
                                {
                                     Price = DgviewBarcode.Rows[i].Cells[17].Value.ToString();
                                }
                                else if (DgviewBarcode.Rows[i].Cells[2].Value.ToString() == "Glizmore")
                                {
                                    Price = DgviewBarcode.Rows[i].Cells[10].Value.ToString();
                                }
                                string ItemName = DgviewBarcode.Rows[i].Cells[11].Value.ToString();
                                //  string StoreName = DgviewBarcode.Rows[i].Cells[9].Value.ToString();
                                string Colour = DgviewBarcode.Rows[i].Cells[8].Value.ToString();
                                if (DgviewBarcode.Rows[i].Cells[14].Value != null)
                                    Size = DgviewBarcode.Rows[i].Cells[14].Value.ToString();
                                string StyleNo = DgviewBarcode.Rows[i].Cells[13].Value.ToString();
                                string Grade = DgviewBarcode.Rows[i].Cells[12].Value.ToString();
                                string SilverPurity = DgviewBarcode.Rows[i].Cells[15].Value.ToString();
                                //string Grade = "22KT";
                                string GoldCategory = "3%";
                                string SilverCategory = "97%";
                                string ProductType = DgviewBarcode.Rows[i].Cells[4].Value.ToString();
                                string ProductCategory = DgviewBarcode.Rows[i].Cells[3].Value.ToString();
                                string StoneName = DgviewBarcode.Rows[i].Cells[16].Value.ToString();
                               
                                //GenerateNewBarcode(Barcode, GrossWt, NetWt, Price, Grade, ItemName,BrandName, Colour, Size, StyleNo, GoldCategory, SilverPurity, SilverCategory);

                                GenerateListOfItemsToPrint(1, BrandName, NetWt, SilverPurity, ProductType, ProductCategory, StyleNo, Total, GrossWt, "", "", Price, Barcode, "", SilverPurity, Size, Colour, StoneName);
                            }
                        }
                        PrintDialog dig = new PrintDialog();
                        PrintDocument doc = new TextDocument();
                        dig.Document = doc;
                        if (dig.ShowDialog() == DialogResult.OK)
                        {
                            if (System.Drawing.Printing.PrinterSettings.InstalledPrinters.Count <= 0)
                            {
                                MessageBox.Show("Printer not found.", "Kencloud", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                                return;
                            }
                            else
                            {
                                PrintNewBarcode(dig, doc);
                                //PrintItems(dig, doc);
                                TAGGING_PrintBarcode printObj = new TAGGING_PrintBarcode();
                                printObj.Total_Copies = TotalCopies;
                                InsertDataTable();
                                printObj.Barcodeprint = dt;
                                printObj.Created_By = 1;
                                ERPManagement.GetInstance.InsertBarcodePrintDetails(printObj);
                                MessageBox.Show("Barcode(s) printed successfully.", "Kencloud", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                Reset();
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Please check the item.", "Kencloud", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }
            }
            else if (TxtBarcode.Text == "" && CmbItemName.SelectedValue == null)
            {
                if (TxtBarcode.Enabled == true)
                {
                    MessageBox.Show("Please enter barcode.", "Kencloud", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }
                else
                {
                    MessageBox.Show("Please select item name.", "Kencloud", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }
            }
            else
            {
                MessageBox.Show("Please enter copies to print.", "Kencloud", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                if (DgviewBarcode.Rows.Count > 0)
                {
                    // DgviewBarcode.Rows[0].Cells[6].Selected = true;
                    DgviewBarcode.CurrentCell = DgviewBarcode.Rows[0].Cells[9];
                    DgviewBarcode.BeginEdit(true);
                }
            }
        }

        void DefaultDropdowns()
        {
            List<TAGGING_PrintBarcode> Listobj = new List<TAGGING_PrintBarcode>();
            TAGGING_PrintBarcode Select = new TAGGING_PrintBarcode();
            Select.Item_Type_Name = "Select";
            Select.Item_Type_ID = 0;
            Select.Item_ID = 0;
            Select.Item_Name = "Select";
            Listobj.Add(Select);
            CmbItemType.DataSource = Listobj;
            CmbItemType.DisplayMember = "Item_Type_Name";
            CmbItemType.ValueMember = "Item_Type_ID";
            CmbItemName.DataSource = Listobj;
            CmbItemName.ValueMember = "Item_ID";
            CmbItemName.DisplayMember = "Item_Name";
        }

        #endregion

        #region Print

        //public System.Drawing.Image GenerateBarcode(string Text)
        //{
        //    Barcode128 code128 = new Barcode128();
        //    code128.CodeType = iTextSharp.text.pdf.Barcode.CODE128;
        //    code128.ChecksumText = true;
        //    code128.GenerateChecksum = true;
        //    code128.StartStopText = false;
        //    code128.Code = Text;
        //    System.Drawing.Bitmap bmpimg = new Bitmap(130, 18);
        //    Graphics bmpgraphics = Graphics.FromImage(bmpimg);
        //    bmpgraphics.Clear(Color.White);
        //    bmpgraphics.DrawImage(code128.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White), new Point(0, 0));
        //    //Label_Generic(bmpimg, Text);
        //    return bmpimg;
        //}

        class TextDocument : PrintDocument
        {
            public int PageNumber;
            public int Offset;
            public TextDocument()
            {
            }
        }

        #endregion

        #region Dated 17/12/2012

        public void GenerateNewBarcode(string Barcode, string GrossWt, string NetWt, string Price, string Grade, string ItemName,string BrandName, string Colour, string Size, string StyleNo, string GoldCategory, string SilverPurity, string SilverCategory)
        {
            try
            {
                for (int i = 0; i < Total; i++)
                {
                    System.Drawing.Image GeneratedImg = GenerateBarcode(Barcode);
                    barList bar = new barList();
                    bar.Barcode = Barcode;
                    bar.BarcodeImage = GeneratedImg;
                    bar.Grade = Grade;
                    bar.GrossWt = GrossWt;
                    bar.ItemName = ItemName;
                    bar.NetWt = NetWt;
                    bar.Price = Price;
                    bar.BrandName = BrandName;
                    bar.Colour = Colour;
                    bar.Size = Size;
                    bar.StyleNo = StyleNo;
                    bar.GoldCategory = GoldCategory;
                    bar.SilverPurity = SilverPurity;
                    bar.SilverCategory = SilverCategory;
                    Bars.Add(bar);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "KenCloud", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }
        }


        public void PrintNewBarcode(PrintDialog dig, PrintDocument doc)
        {
            doc.PrinterSettings.PrinterName = dig.PrinterSettings.PrinterName;
            PaperSize paperSize = new PaperSize();
            paperSize.RawKind = (int)PaperKind.Custom;
            paperSize.Height = 50;
            doc.DefaultPageSettings.PaperSize = paperSize;
            doc.DefaultPageSettings.Landscape = false;
            doc.DefaultPageSettings.Margins = new Margins(4, 1, 0, 1);
            doc.PrintPage += this.PrintingBarcode;
            doc.Print();
        }

        private void Doc_NewPrintPage(object sender, PrintPageEventArgs e)
        {
            //TextDocument doc = (TextDocument)sender;
            //System.Drawing.Font font = new System.Drawing.Font("Calibri", 6);
            //float lineHeight = 25;
            //float x = e.MarginBounds.Left;
            //float y = e.MarginBounds.Top;
            //float PositionY = 0f;
            //doc.PageNumber += 1;
            //while ((y + lineHeight) < e.MarginBounds.Bottom && doc.Offset < Bars.Count)
            //{
            //    System.Drawing.Font Newfont = new System.Drawing.Font("Calibri", 6, FontStyle.Bold);
            //    e.Graphics.DrawString(Bars[doc.Offset].ItemName, Newfont, Brushes.Black, x, y);
            //    e.Graphics.DrawString(Bars[doc.Offset].Ct + "K", Newfont, Brushes.Black, x + 90, y);
            //    PositionY += Newfont.Height;
            //    e.Graphics.DrawString("Gross Wt.", Newfont, Brushes.Black, x, PositionY);
            //    e.Graphics.DrawString(" : ", Newfont, Brushes.Black, x + 40, PositionY);
            //    e.Graphics.DrawString(decimal.Parse(Bars[doc.Offset].GrossWt).ToString("0.000") + " gm.", font, Brushes.Black, x + 50, PositionY);
            //    PositionY += Newfont.Height;
            //    e.Graphics.DrawString("Net Wt.", Newfont, Brushes.Black, x, PositionY);
            //    e.Graphics.DrawString(" : ", Newfont, Brushes.Black, x + 40, PositionY);
            //    e.Graphics.DrawString(decimal.Parse(Bars[doc.Offset].NetWt).ToString("0.000") + " gm.", font, Brushes.Black, x + 50, PositionY);
            //    PositionY += Newfont.Height;
            //    e.Graphics.DrawString("Price", Newfont, Brushes.Black, x, PositionY);
            //    e.Graphics.DrawString(" : ", Newfont, Brushes.Black, x + 40, PositionY);
            //    string destfile = Application.StartupPath;
            //    for (int i = 0; i < 2; i++)
            //    {
            //        destfile = destfile.Substring(0, destfile.LastIndexOf("\\"));
            //    }
            //    string path = destfile;
            //    PrivateFontCollection Rupees = new PrivateFontCollection();
            //    Rupees.AddFontFile(path + "\\Rupee_Foradian.ttf");
            //    e.Graphics.DrawString("` ", new System.Drawing.Font(Rupees.Families[0], 6, FontStyle.Bold), Brushes.Black, x + 50, PositionY);
            //    e.Graphics.DrawString(decimal.Parse(Bars[doc.Offset].Price).ToString("N", CultureInfo.CreateSpecificCulture("hi-IN")), Newfont, Brushes.Black, x + 56, PositionY);
            //    e.Graphics.DrawImage(Bars[doc.Offset].BarcodeImage, x + 134, y);
            //    e.Graphics.DrawString("Item Code : " + Bars[doc.Offset].Barcode, font, Brushes.Black, x + 134, y + Bars[doc.Offset].BarcodeImage.Height + 1);
            //    //if (File.Exists(destfile + "\\Resources\\Twitter-icon.png"))
            //    //{
            //    //    System.Drawing.Image bmp = Bitmap.FromFile(destfile + "\\Resources\\Twitter-icon.png");
            //    //    e.Graphics.DrawImage(bmp, x + 134, y + Bars[doc.Offset].BarcodeImage.Height + 9);
            //    //    e.Graphics.DrawString(Bars[doc.Offset].StoreName, Newfont, Brushes.Black, x + 134 + bmp.Width, y + Bars[doc.Offset].BarcodeImage.Height + 9);
            //    //}
            //    e.Graphics.DrawString(Bars[doc.Offset].StoreName, Newfont, Brushes.Black, x + 134, y + Bars[doc.Offset].BarcodeImage.Height + 9);
            //    doc.Offset += 1;
            //    y += lineHeight;
            //}

            //if (doc.Offset < Bars.Count)
            //{
            //    e.HasMorePages = true;
            //}
            //else
            //{
            //    doc.Offset = 0;
            //}
        }

        private System.Drawing.Image Label_Generic(System.Drawing.Image img, string Data)
        {
            try
            {
                System.Drawing.Font font = new System.Drawing.Font("Calibri", 6, FontStyle.Regular);

                using (Graphics g = Graphics.FromImage(img))
                {
                    g.DrawImage(img, (float)0, (float)0);
                    g.SmoothingMode = SmoothingMode.HighQuality;
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    g.CompositingQuality = CompositingQuality.HighQuality;
                    StringFormat f = new StringFormat();
                    f.Alignment = StringAlignment.Near;
                    f.LineAlignment = StringAlignment.Near;
                    int LabelX = 0;
                    int LabelY = 0;
                    LabelX = img.Width / 2;
                    LabelY = img.Height - (font.Height);
                    f.Alignment = StringAlignment.Center;
                    g.FillRectangle(Brushes.White, new RectangleF((float)0, (float)LabelY, (float)img.Width, (float)font.Height));
                    g.DrawString(Data, font, new SolidBrush(this.ForeColor), new RectangleF((float)0, (float)LabelY + 2, (float)img.Width, (float)font.Height), f);

                    g.Save();
                }
                return img;
            }
            catch (Exception ex)
            {
                throw new Exception("ELABEL_GENERIC-1: " + ex.Message);
            }
        }
        #endregion

        #region Dt 19/01/2012

        //private void PrintingBarcode(object sender, PrintPageEventArgs e)
        //{
        //    TextDocument doc = (TextDocument)sender;
        //    System.Drawing.Font font = new System.Drawing.Font("Calibri", 5);
        //    System.Drawing.Font fontBold = new System.Drawing.Font("Calibri", 5, FontStyle.Bold);
        //    float lineHeight = 25;
        //    float x = e.MarginBounds.Left;
        //    float y = e.MarginBounds.Top;
        //    doc.PageNumber += 1;
        //    string destfile = Application.StartupPath;
        //    while ((y + lineHeight) < e.MarginBounds.Bottom && doc.Offset < Bars.Count)
        //    {
        //        if (Bars[doc.Offset].BrandName == "Milaan")
        //        {
        //            float PositionY = 0f;
        //            System.Drawing.Font Newfont = new System.Drawing.Font("Calibri", 5, FontStyle.Regular);
        //            System.Drawing.Font NewfontBold = new System.Drawing.Font("Calibri", 5, FontStyle.Bold);
        //            e.Graphics.DrawString("MRP", NewfontBold, Brushes.Black, x + 15, PositionY);
        //            e.Graphics.DrawString(" : ", NewfontBold, Brushes.Black, x + 35, PositionY);
        //            for (int i = 0; i < 2; i++)
        //            {
        //                destfile = destfile.Substring(0, destfile.LastIndexOf("\\"));
        //            }
        //            string path = destfile;
        //            PrivateFontCollection Rupees = new PrivateFontCollection();
        //            Rupees.AddFontFile(path + "\\Rupee_Foradian.ttf");
        //            e.Graphics.DrawString("` ", new System.Drawing.Font(Rupees.Families[0], 6, FontStyle.Bold), Brushes.Black, x + 45, PositionY);
        //            e.Graphics.DrawString(decimal.Parse(Bars[doc.Offset].Price).ToString("N", CultureInfo.CreateSpecificCulture("hi-IN")), Newfont, Brushes.Black, x + 51, PositionY);

        //            if (File.Exists(destfile + "\\Resources\\Milaan.png"))
        //            {
        //                System.Drawing.Image bmp = Bitmap.FromFile(destfile + "\\Resources\\Milaan.png");
        //                e.Graphics.DrawImage(bmp, x, PositionY);
        //            }
        //            PositionY += Newfont.Height;
        //            e.Graphics.DrawString("Wt.", NewfontBold, Brushes.Black, x, PositionY);
        //            e.Graphics.DrawString(" : ", NewfontBold, Brushes.Black, x + 35, PositionY);
        //            e.Graphics.DrawString(decimal.Parse(Bars[doc.Offset].GrossWt).ToString("0.000") + " gm.", font, Brushes.Black, x + 45, PositionY);
        //            PositionY += Newfont.Height;
        //            e.Graphics.DrawString("Size", NewfontBold, Brushes.Black, x, PositionY);
        //            e.Graphics.DrawString(" : ", NewfontBold, Brushes.Black, x + 35, PositionY);
        //            e.Graphics.DrawString(Bars[doc.Offset].Size, font, Brushes.Black, x + 45, PositionY);
        //            e.Graphics.DrawString("Color : ", NewfontBold, Brushes.Black, x + 80, PositionY);
        //            e.Graphics.DrawString(Bars[doc.Offset].Colour, Newfont, Brushes.Black, x + 100, PositionY);
        //            PositionY += Newfont.Height;
        //            e.Graphics.DrawString("Gold", NewfontBold, Brushes.Black, x, PositionY);
        //            e.Graphics.DrawString(" : ", NewfontBold, Brushes.Black, x + 35, PositionY);
        //            e.Graphics.DrawString("(" + Bars[doc.Offset].Grade + ")" + Bars[doc.Offset].GoldCategory, Newfont, Brushes.Black, x + 45, PositionY);
        //            PositionY += Newfont.Height;
        //            e.Graphics.DrawString("Silver", NewfontBold, Brushes.Black, x, PositionY);
        //            e.Graphics.DrawString(" : ", NewfontBold, Brushes.Black, x + 35, PositionY);
        //            e.Graphics.DrawString("(" + Bars[doc.Offset].SilverPurity + ")" + Bars[doc.Offset].SilverCategory, Newfont, Brushes.Black, x + 45, PositionY);
        //            PositionY += Newfont.Height;
        //            if (File.Exists(destfile + "\\Resources\\Glizmore.png"))
        //            {
        //                System.Drawing.Image bmp = Bitmap.FromFile(destfile + "\\Resources\\Glizmore.png");
        //                e.Graphics.DrawImage(bmp, x + 134, y);
        //            }
        //            e.Graphics.DrawString(Bars[doc.Offset].ItemName, NewfontBold, Brushes.Black, x + 154, y);
        //            y += Newfont.Height;
        //            e.Graphics.DrawString(Bars[doc.Offset].BrandName, NewfontBold, Brushes.Black, x + 132, y);
        //            e.Graphics.DrawString(" : ", NewfontBold, Brushes.Black, x + 170, y);
        //            e.Graphics.DrawString(Bars[doc.Offset].StyleNo, Newfont, Brushes.Black, x + 176, y);
        //            y += Newfont.Height;
        //            e.Graphics.DrawImage(Bars[doc.Offset].BarcodeImage, x + 132, y);
        //            //e.Graphics.DrawString(Bars[doc.Offset].StoreName, Newfont, Brushes.Black, x + 134, y + Bars[doc.Offset].BarcodeImage.Height + 9);
        //            doc.Offset += 1;
        //            y += lineHeight;
        //        }
        //        else if (Bars[doc.Offset].BrandName == "Glizmore")
        //        {
        //            float PositionY = 0f;
        //            System.Drawing.Font Newfont = new System.Drawing.Font("Calibri", 5, FontStyle.Regular);
        //            System.Drawing.Font NewfontBold = new System.Drawing.Font("Calibri", 5, FontStyle.Bold);
        //            e.Graphics.DrawString("MRP", NewfontBold, Brushes.Black, x + 15, PositionY);
        //            e.Graphics.DrawString(" : ", NewfontBold, Brushes.Black, x + 35, PositionY);
        //            for (int i = 0; i < 2; i++)
        //            {
        //                destfile = destfile.Substring(0, destfile.LastIndexOf("\\"));
        //            }
        //            string path = destfile;
        //            PrivateFontCollection Rupees = new PrivateFontCollection();
        //            Rupees.AddFontFile(path + "\\Rupee_Foradian.ttf");
        //            e.Graphics.DrawString("` ", new System.Drawing.Font(Rupees.Families[0], 6, FontStyle.Bold), Brushes.Black, x + 45, PositionY);
        //            e.Graphics.DrawString(decimal.Parse(Bars[doc.Offset].Price).ToString("N", CultureInfo.CreateSpecificCulture("hi-IN")), Newfont, Brushes.Black, x + 51, PositionY);

        //            if (File.Exists(destfile + "\\Resources\\Milaan.png"))
        //            {
        //                System.Drawing.Image bmp = Bitmap.FromFile(destfile + "\\Resources\\Milaan.png");
        //                e.Graphics.DrawImage(bmp, x, PositionY);
        //            }
        //            PositionY += Newfont.Height;
        //            e.Graphics.DrawString("Wt.", NewfontBold, Brushes.Black, x, PositionY);
        //            e.Graphics.DrawString(" : ", NewfontBold, Brushes.Black, x + 35, PositionY);
        //            e.Graphics.DrawString(decimal.Parse(Bars[doc.Offset].GrossWt).ToString("0.000") + " gm.", font, Brushes.Black, x + 45, PositionY);
        //            PositionY += Newfont.Height;
        //            e.Graphics.DrawString("Size", NewfontBold, Brushes.Black, x, PositionY);
        //            e.Graphics.DrawString(" : ", NewfontBold, Brushes.Black, x + 35, PositionY);
        //            e.Graphics.DrawString(Bars[doc.Offset].Size, font, Brushes.Black, x + 45, PositionY);
        //            PositionY += Newfont.Height;
        //            e.Graphics.DrawString("Color", NewfontBold, Brushes.Black, x, PositionY);
        //            e.Graphics.DrawString(" : ", NewfontBold, Brushes.Black, x + 35, PositionY);
        //            e.Graphics.DrawString(Bars[doc.Offset].Colour, Newfont, Brushes.Black, x + 45, PositionY);
        //            PositionY += Newfont.Height;
        //            e.Graphics.DrawString("Silver", NewfontBold, Brushes.Black, x, PositionY);
        //            e.Graphics.DrawString(" : ", NewfontBold, Brushes.Black, x + 35, PositionY);
        //            e.Graphics.DrawString(Bars[doc.Offset].SilverPurity, Newfont, Brushes.Black, x + 45, PositionY);
        //            PositionY += Newfont.Height;
        //            if (File.Exists(destfile + "\\Resources\\Glizmore.png"))
        //            {
        //                System.Drawing.Image bmp = Bitmap.FromFile(destfile + "\\Resources\\Glizmore.png");
        //                e.Graphics.DrawImage(bmp, x + 134, y);
        //            }
        //            e.Graphics.DrawString(Bars[doc.Offset].ItemName, NewfontBold, Brushes.Black, x + 154, y);
        //            y += Newfont.Height;
        //            e.Graphics.DrawString(Bars[doc.Offset].BrandName, NewfontBold, Brushes.Black, x + 132, y);
        //            e.Graphics.DrawString(" : ", NewfontBold, Brushes.Black, x + 170, y);
        //            e.Graphics.DrawString(Bars[doc.Offset].StyleNo, Newfont, Brushes.Black, x + 176, y);
        //            y += Newfont.Height;
        //            e.Graphics.DrawImage(Bars[doc.Offset].BarcodeImage, x + 132, y);
        //            //e.Graphics.DrawString(Bars[doc.Offset].StoreName, Newfont, Brushes.Black, x + 134, y + Bars[doc.Offset].BarcodeImage.Height + 9);
        //            doc.Offset += 1;
        //            y += lineHeight;
        //        }

        //        if (doc.Offset < Bars.Count)
        //        {
        //            e.HasMorePages = true;
        //        }
        //        else
        //        {
        //            doc.Offset = 0;
        //        }
        //    }
        //}

        #endregion

        private void BtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        // Added code for new format of Brand here onwards.....
        public System.Drawing.Image GenerateBarcode(string Text)
        {
            Barcode128 code128 = new Barcode128();
            code128.CodeType = iTextSharp.text.pdf.Barcode.CODE128;
            code128.ChecksumText = true;
            code128.GenerateChecksum = true;
            code128.StartStopText = false;
            code128.Code = Text;
            System.Drawing.Bitmap bmpimg = new Bitmap(130, 12);
            Graphics bmpgraphics = Graphics.FromImage(bmpimg);
            bmpgraphics.Clear(Color.White);
            bmpgraphics.DrawImage(code128.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White), new Point(0, 0));
            return bmpimg;
        }
        //class TextDocument : PrintDocument
        //{
        //    public int PageNumber;
        //    public int Offset;
        //    public TextDocument()
        //    {
        //    }
        //}
        public void GenerateListOfItemsToPrint(int Item_ID, string Brand, string Metal, string SilverPurity, string ProductType, string ProductCategory, string StyleNo, int Quantity, string GrossWt, string DJILMRP, string FranchiseMRP, string StoreMRP, string Barcode, string Gold_Grade, string Gold_Purity, string Size, string Color, string Stone_Name)
        {
            for (int i = 0; i < Quantity; i++)
            {
                barList bar = new barList();
              //  bar.Item_ID = Item_ID;
                bar.BrandName = Brand;
              //  bar.DJILMRP = DJILMRP;
             //   bar.FranchiseMRP = FranchiseMRP;
                bar.GrossWt = GrossWt;
                bar.Metal = Metal;
                bar.ProductCategory = ProductCategory;
                bar.ProductType = ProductType;
                bar.Quantity = Quantity;
                bar.SilverPurity = SilverPurity;
                bar.StoreMRP = StoreMRP;
                bar.StyleNo = StyleNo;
                bar.Barcode = Barcode;
               // bar.Gold_Grade = Gold_Grade;
                bar.Gold_Purity = Gold_Purity;
                bar.Size = Size;
                bar.Color = Color;
                bar.Stone_Name = Stone_Name;
                Bars.Add(bar);
            }
        }
        public void PrintItems(PrintDialog dig, PrintDocument doc)
        {
            doc.PrinterSettings.PrinterName = dig.PrinterSettings.PrinterName;
            PaperSize paperSize = new PaperSize();
            paperSize.RawKind = (int)PaperKind.Custom;
            paperSize.Height = 50;
            doc.DefaultPageSettings.PaperSize = paperSize;
            doc.DefaultPageSettings.Landscape = false;
            doc.DefaultPageSettings.Margins = new Margins(4, 1, 0, 1);
            doc.PrintPage += this.PrintingBarcode;
            doc.Print();
        }
        private void PrintingBarcode(object sender, PrintPageEventArgs e)
        {
            TextDocument doc = (TextDocument)sender;
            System.Drawing.Font font = new System.Drawing.Font("Calibri", 6, FontStyle.Bold);
            System.Drawing.Font fontBold = new System.Drawing.Font("Calibri", 6, FontStyle.Bold);
            float lineHeight = 25;
            float x = e.MarginBounds.Left;
            float y = e.MarginBounds.Top;
            doc.PageNumber += 1;
            string destfile = Application.StartupPath;
            while ((y + lineHeight) < e.MarginBounds.Bottom && doc.Offset < Bars.Count)
            {
                #region IF BRAND IS MILAAN
                if (Bars[doc.Offset].BrandName == "Milaan")
                {
                    float PositionY = 0f;
                    System.Drawing.Font Newfont = new System.Drawing.Font("Calibri", 6, FontStyle.Bold);
                    System.Drawing.Font NewfontBold = new System.Drawing.Font("Calibri", 6, FontStyle.Bold);
                    e.Graphics.DrawString("MRP", NewfontBold, Brushes.Black, x + 32, PositionY);
                    e.Graphics.DrawString(" : ", NewfontBold, Brushes.Black, x + 57, PositionY);
                    for (int i = 0; i < 2; i++)
                    {
                        destfile = destfile.Substring(0, destfile.LastIndexOf("\\"));
                    }
                    string path = destfile;
                    PrivateFontCollection Rupees = new PrivateFontCollection();
                    string NewPath = Environment.CurrentDirectory;
                    if (File.Exists(NewPath + "\\Milaan.png"))
                    {
                        System.Drawing.Image bmp = Bitmap.FromFile(NewPath + "\\Milaan.png");
                        System.Drawing.Image newImage = ScaleImage(bmp, 30, 30);
                        e.Graphics.DrawImage(newImage, x, PositionY+2);
                    }
                    Rupees.AddFontFile(NewPath + "\\Rupee_Foradian.ttf");
                    e.Graphics.DrawString("` ", new System.Drawing.Font(Rupees.Families[0], 6, FontStyle.Bold), Brushes.Black, x + 67, PositionY);
                    e.Graphics.DrawString(decimal.Parse(Bars[doc.Offset].StoreMRP).ToString("N", CultureInfo.CreateSpecificCulture("hi-IN")), Newfont, Brushes.Black, x + 73, PositionY);

                    PositionY += Newfont.Height;
                    e.Graphics.DrawString("Wt.", NewfontBold, Brushes.Black, x + 32, PositionY);
                    e.Graphics.DrawString(" : ", NewfontBold, Brushes.Black, x + 57, PositionY);
                    e.Graphics.DrawString(decimal.Parse(Bars[doc.Offset].GrossWt).ToString("0.000") + " gm.", font, Brushes.Black, x + 67, PositionY);
                    PositionY += Newfont.Height;
                    e.Graphics.DrawString("Purity", NewfontBold, Brushes.Black, x + 32, PositionY);
                    e.Graphics.DrawString(" : ", NewfontBold, Brushes.Black, x + 57, PositionY);
                    int Purity = Bars[doc.Offset].SilverPurity.Length;
                    if (Purity > 15)
                    {
                        string PurityF = Bars[doc.Offset].SilverPurity.Substring(0, 15);
                        e.Graphics.DrawString(PurityF, NewfontBold, Brushes.Black, x + 67, PositionY);
                        PositionY += NewfontBold.Height;
                        string PurityL = Bars[doc.Offset].SilverPurity.Substring(15, Purity - 15);
                        e.Graphics.DrawString(PurityL, NewfontBold, Brushes.Black, x + 67, PositionY);
                        PositionY += NewfontBold.Height;
                    }
                    else
                    {
                        e.Graphics.DrawString(Bars[doc.Offset].SilverPurity, NewfontBold, Brushes.Black, x + 67, PositionY);
                        PositionY += NewfontBold.Height;
                    }
                    //e.Graphics.DrawString(Bars[doc.Offset].SilverPurity, font, Brushes.Black, x + 67, PositionY);
                    int RightY = 0;
                    if (File.Exists(NewPath + "\\Glizmore.png"))
                    {
                        System.Drawing.Image bmp = Bitmap.FromFile(NewPath + "\\Glizmore.png");
                        System.Drawing.Image newImage = ScaleImage(bmp, 16, 16);
                        e.Graphics.DrawImage(newImage, x + 132, RightY);
                    }
                    e.Graphics.DrawString("L/S : " + Bars[doc.Offset].Size, NewfontBold, Brushes.Black, x + 149, RightY);
                    e.Graphics.DrawString(" | ", NewfontBold, Brushes.Black, x + 197, RightY);
                    e.Graphics.DrawString(Bars[doc.Offset].Stone_Name, NewfontBold, Brushes.Black, x + 205, RightY);
                    RightY += Newfont.Height;
                    e.Graphics.DrawString("Glizmore", NewfontBold, Brushes.Black, x + 149, RightY);
                    e.Graphics.DrawString(" | ", NewfontBold, Brushes.Black, x + 180, RightY);
                    e.Graphics.DrawString(Bars[doc.Offset].Color, NewfontBold, Brushes.Black, x + 187, RightY);
                    e.Graphics.DrawString(" | ", NewfontBold, Brushes.Black, x + 196, RightY);
                    e.Graphics.DrawString(Bars[doc.Offset].StyleNo, NewfontBold, Brushes.Black, x + 202, RightY);
                    //e.Graphics.DrawString(Bars[doc.Offset].ProductCategory, NewfontBold, Brushes.Black, x + 152, PositionY);
                    //e.Graphics.DrawString(Bars[doc.Offset].ProductType, NewfontBold, Brushes.Black, x + 184, PositionY);
                    //PositionY += Newfont.Height;
                    //e.Graphics.DrawString(" | ", NewfontBold, Brushes.Black, x + 191, PositionY);
                    //e.Graphics.DrawString(Bars[doc.Offset].StyleNo, Newfont, Brushes.Black, x + 198, PositionY);
                    // PositionY += Newfont.Height;
                    string BarcodeText = "";
                    if (Bars[doc.Offset].Barcode != "" && Bars[doc.Offset].Barcode != null)
                    {
                        BarcodeText = Bars[doc.Offset].Barcode;
                    }
                    //else
                    //{
                    //    BarcodeText = GetTopValue();
                    //    string Gen_No = BarcodeText.Substring(2, 12);
                    //    int Item_ID = Bars[doc.Offset].Item_ID;
                    //    UpdatePrintedBarcode(Item_ID, Gen_No, BarcodeText);
                    //    BindGrid();
                    //    //****** if quantity based ********//

                    //    for (int i = 0; i < Bars.Count; i++)
                    //    {
                    //        if (Bars[i].Item_ID == Item_ID)
                    //        {
                    //            Bars[i].Barcode = BarcodeText;
                    //        }
                    //    }

                    //    //**************************************//                      
                    //}
                    System.Drawing.Image img = GenerateBarcode(BarcodeText);
                    e.Graphics.DrawImage(img, x + 132, y + (Newfont.Height) * 2);
                    string PrintBarcode = "* " + BarcodeText + " *";
                    StringFormat f = new StringFormat();
                    f.Alignment = StringAlignment.Near;
                    f.LineAlignment = StringAlignment.Near;
                    int LabelX = 0;
                    int LabelY = 0;
                    LabelX = (int)x + 134 + (img.Width / 2) - PrintBarcode.Length;
                    LabelY = (int)(y + (Newfont.Height) * 2 + img.Height);
                    f.Alignment = StringAlignment.Center;
                    e.Graphics.DrawString("* " + BarcodeText + " *", font, Brushes.Black, new RectangleF((float)(x + 126), (float)LabelY, (float)img.Width, (float)font.Height), f);
                    doc.Offset += 1;
                    y += lineHeight;
                }
                #endregion

                #region IF BRAND IS GLIZMORE

                else if (Bars[doc.Offset].BrandName == "Glizmore")
                {
                    float PositionY = 0f;
                    System.Drawing.Font Newfont = new System.Drawing.Font("Calibri", 6, FontStyle.Bold);
                    System.Drawing.Font NewfontBold = new System.Drawing.Font("Calibri", 6, FontStyle.Bold);
                    e.Graphics.DrawString("MRP", NewfontBold, Brushes.Black, x + 32, PositionY);
                    e.Graphics.DrawString(" : ", NewfontBold, Brushes.Black, x + 57, PositionY);
                    for (int i = 0; i < 2; i++)
                    {
                        destfile = destfile.Substring(0, destfile.LastIndexOf("\\"));
                    }
                    string path = destfile;
                    PrivateFontCollection Rupees = new PrivateFontCollection();
                    string NewPath = Environment.CurrentDirectory;
                    if (File.Exists(NewPath + "\\Derewala.png"))
                    {
                        System.Drawing.Image bmp = Bitmap.FromFile(NewPath + "\\Derewala.png");
                        System.Drawing.Image newImage = ScaleImage(bmp, 30, 30);
                        e.Graphics.DrawImage(newImage, x, PositionY+2);
                    }
                    Rupees.AddFontFile(NewPath + "\\Rupee_Foradian.ttf");
                    e.Graphics.DrawString("` ", new System.Drawing.Font(Rupees.Families[0], 6, FontStyle.Bold), Brushes.Black, x + 67, PositionY);
                    e.Graphics.DrawString(decimal.Parse(Bars[doc.Offset].StoreMRP).ToString("N", CultureInfo.CreateSpecificCulture("hi-IN")), Newfont, Brushes.Black, x + 73, PositionY);

                    PositionY += Newfont.Height;
                    //e.Graphics.DrawString("Wt.", NewfontBold, Brushes.Black, x + 32, PositionY);
                    //e.Graphics.DrawString(" : ", NewfontBold, Brushes.Black, x + 57, PositionY);
                    //e.Graphics.DrawString(decimal.Parse(Bars[doc.Offset].GrossWt).ToString("0.000") + " gm.", font, Brushes.Black, x + 67, PositionY);
                    //PositionY += Newfont.Height;
                    e.Graphics.DrawString("Purity", NewfontBold, Brushes.Black, x + 32, PositionY);
                    e.Graphics.DrawString(" : ", NewfontBold, Brushes.Black, x + 57, PositionY);
                    e.Graphics.DrawString(Bars[doc.Offset].SilverPurity, NewfontBold, Brushes.Black, x + 67, PositionY);
                    //e.Graphics.DrawString(Bars[doc.Offset].SilverPurity, font, Brushes.Black, x + 67, PositionY);
                    int RightY = 0;
                    if (File.Exists(NewPath + "\\Glizmore.png"))
                    {
                        System.Drawing.Image bmp = Bitmap.FromFile(NewPath + "\\Glizmore.png");
                        System.Drawing.Image newImage = ScaleImage(bmp, 16, 16);
                        e.Graphics.DrawImage(newImage, x + 132, RightY);
                    }
                    //e.Graphics.DrawString("L/S : " + Bars[doc.Offset].Size, NewfontBold, Brushes.Black, x + 149, RightY);
                    //e.Graphics.DrawString(" | ", NewfontBold, Brushes.Black, x + 197, RightY);
                    //e.Graphics.DrawString(Bars[doc.Offset].Stone_Name, NewfontBold, Brushes.Black, x + 205, RightY);
                    e.Graphics.DrawString(Bars[doc.Offset].ProductCategory, NewfontBold, Brushes.Black, x + 149, RightY);
                    e.Graphics.DrawString(" ", NewfontBold, Brushes.Black, x + 180, RightY);
                    e.Graphics.DrawString(Bars[doc.Offset].ProductType, NewfontBold, Brushes.Black, x + 190, RightY);
                    RightY += Newfont.Height;
                    e.Graphics.DrawString(Bars[doc.Offset].BrandName, NewfontBold, Brushes.Black, x + 149, RightY);
                    e.Graphics.DrawString(" | ", NewfontBold, Brushes.Black, x + 180, RightY);
                    //e.Graphics.DrawString(Bars[doc.Offset].Color, NewfontBold, Brushes.Black, x + 187, RightY);
                    //e.Graphics.DrawString(" | ", NewfontBold, Brushes.Black, x + 196, RightY);
                    e.Graphics.DrawString(Bars[doc.Offset].StyleNo, NewfontBold, Brushes.Black, x + 187, RightY);
                    //e.Graphics.DrawString(Bars[doc.Offset].ProductCategory, NewfontBold, Brushes.Black, x + 152, PositionY);
                    //e.Graphics.DrawString(Bars[doc.Offset].ProductType, NewfontBold, Brushes.Black, x + 184, PositionY);
                    //PositionY += Newfont.Height;
                    //e.Graphics.DrawString(" | ", NewfontBold, Brushes.Black, x + 191, PositionY);
                    //e.Graphics.DrawString(Bars[doc.Offset].StyleNo, Newfont, Brushes.Black, x + 198, PositionY);
                    // PositionY += Newfont.Height;
                    string BarcodeText = "";
                    if (Bars[doc.Offset].Barcode != "" && Bars[doc.Offset].Barcode != null)
                    {
                        BarcodeText = Bars[doc.Offset].Barcode;
                    }
                    //else
                    //{
                    //    BarcodeText = GetTopValue();
                    //    string Gen_No = BarcodeText.Substring(2, 12);
                    //    int Item_ID = Bars[doc.Offset].Item_ID;
                    //    UpdatePrintedBarcode(Item_ID, Gen_No, BarcodeText);
                    //    BindGrid();
                    //    //****** if quantity based ********//

                    //    for (int i = 0; i < Bars.Count; i++)
                    //    {
                    //        if (Bars[i].Item_ID == Item_ID)
                    //        {
                    //            Bars[i].Barcode = BarcodeText;
                    //        }
                    //    }

                    //    //**************************************//                      
                    //}
                    System.Drawing.Image img = GenerateBarcode(BarcodeText);
                    e.Graphics.DrawImage(img, x + 132, y + (Newfont.Height) * 2);
                    string PrintBarcode = "* " + BarcodeText + " *";
                    StringFormat f = new StringFormat();
                    f.Alignment = StringAlignment.Near;
                    f.LineAlignment = StringAlignment.Near;
                    int LabelX = 0;
                    int LabelY = 0;
                    LabelX = (int)x + 134 + (img.Width / 2) - PrintBarcode.Length;
                    LabelY = (int)(y + (Newfont.Height) * 2 + img.Height);
                    f.Alignment = StringAlignment.Center;
                    e.Graphics.DrawString("* " + BarcodeText + " *", font, Brushes.Black, new RectangleF((float)(x + 126), (float)LabelY, (float)img.Width, (float)font.Height), f);
                    doc.Offset += 1;
                    y += lineHeight;
                }

                #endregion

                if (doc.Offset < Bars.Count)
                {
                    e.HasMorePages = true;
                }
                else
                {
                    doc.Offset = 0;
                }
            }
        }
        public static System.Drawing.Image ScaleImage(System.Drawing.Image image, int maxWidth, int maxHeight)
        {
            var ratioX = (double)maxWidth / image.Width;
            var ratioY = (double)maxHeight / image.Height;
            var ratio = Math.Min(ratioX, ratioY);

            var newWidth = (int)(image.Width * ratio);
            var newHeight = (int)(image.Height * ratio);

            var newImage = new Bitmap(newWidth, newHeight);
            Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
            return newImage;
        }
    }
}