1

I am creating a stock management application for a jewelry shop. I have completed adding and selling of stock without barcodes. Now they want me to integrate it with barcodes.

When I add stock, a barcode should be printed like so: enter image description here

And the entry which I am making is like so:

enter image description here

And as soon as I hit the save button the stock will be added to the database. How can I write code for the barcode to get it print?

The code which I am using till now is here:

    public partial class FrmAddNewItem : Form
{
    public static bool isEditMode = false;
    private void btnReset_Click(object sender, EventArgs e)
    {
        addComboItems();
        Utility.resetForm(groupBox1);
        isEditMode = false;
        btnStartInvoice.Enabled = true;
        btnSave.Text = "Save";
        generateBarCode();
    }

    private void generateBarCode()
    {
        Random random = new Random();
        String barCode = random.Next(1000000000, int.MaxValue).ToString();
        String strCmd = "select itemCode from tblItemDetail where itemCode='" + barCode + "'";
        while (DBUtil.isRecordExist(strCmd))
        {
            barCode = random.Next(1000000000, int.MaxValue).ToString();
            strCmd = "select itemCode from tblItemDetail where itemCode='" + barCode + "'";
        }
        txtBarCode.Text = barCode;
        lblBarCode.Text = barCode;
        lblBarCode.Font = new Font("Barcode Font", 48);
    }

    private void addComboItems()
    {
        cmboMakingType.Items.Clear();
        cmboStoneRateType.Items.Clear();
        cmboVendorName.Items.Clear();
        cmboVendorId.Items.Clear();

        cmboMakingType.Items.Add(Constants.COMBO_DEFAULT);
        cmboStoneRateType.Items.Add(Constants.COMBO_DEFAULT);
        cmboVendorName.Items.Add(Constants.COMBO_DEFAULT);
        cmboVendorId.Items.Add(Constants.COMBO_DEFAULT);

        cmboMakingType.Items.Add("Pc");
        cmboMakingType.Items.Add("Gm");
        cmboMakingType.Items.Add("Tot");

        cmboStoneRateType.Items.Add("Ct");
        cmboStoneRateType.Items.Add("Pc");
        cmboStoneRateType.Items.Add("Tot");

        String strCmd = "select fullName from tblMemberDetail where isActive='TRUE' and ucase(memberType)='VENDOR'";
        String[] strVendorsName = DBUtil.getAllRecords(strCmd);
        strCmd = "select memberId from tblMemberDetail where isActive='TRUE' and ucase(memberType)='VENDOR'";
        String[] strVendorsId = DBUtil.getAllRecords(strCmd);
        if (strVendorsName != null)
        {
            foreach (String strName in strVendorsName)
                cmboVendorName.Items.Add(strName);
            foreach (String strId in strVendorsId)
                cmboVendorId.Items.Add(strId);
        }
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        String strMsg = "";
        if (txtItemCode.Text.Trim() == "")
        {
            strMsg = "Please enter the Item Code of the item to be added. And please try again.";
            MessageBox.Show(strMsg, "Add New Item", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return;
        }
        String strCmd = "select itemName from tblItemCodeDetail where itemCode='" + txtItemCode.Text + "'";
        strMsg = "";
        if (!DBUtil.isRecordExist(strCmd))
        {
            strMsg = "Item code does not exist. Please enter a valid item code and try again.";
            MessageBox.Show(strMsg, "Add New Item", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            txtItemCode.SelectAll();
            txtItemCode.Focus();
            return;
        }
        foreach (Control ctrl in groupBox1.Controls)
        {
            if (ctrl is TextBox && ctrl.Text == "")
                ctrl.Text = "0";
        }
        if (cmboMakingType.SelectedIndex == 0 || cmboStoneRateType.SelectedIndex == 0)
        {
            strMsg = "Please select the making type and the stone rate type from the dropdown menu.";
            MessageBox.Show(strMsg, "Add New Item", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return;
        }
        if (cmboVendorName.SelectedIndex == 0)
        {
            strMsg = "Please select the vendor from whom you are purchasing this item.";
            MessageBox.Show(strMsg, "Add New Item", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            cmboVendorName.Focus();
            return;
        }
        strMsg = "Are you sure all the details provided are correct and you want to add/update this item to the stock?";
        if (MessageBox.Show(strMsg, "Add / Update New Item", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            return;
        if (isEditMode)
            doUpdate();
        else
            doAdding();
        btnReset_Click(null, null);
    }

    private void doUpdate()
    {
        String[] commands = { "update tblItemDetail set originalTunch='" + txtOriginalTunch.Text + "', purchaseTunch='" + txtPurchaseTunch.Text + "', itemCode='" + txtItemCode.Text + "', grossWeight='" + txtGrossWeight.Text + "', making='" + txtMaking.Text + "', makingType='" + cmboMakingType.Text + "', quantity='" + txtPieces.Text + "', stoneWeight='" + txtStoneWeight.Text + "', stoneRate='" + txtStoneRate.Text + "', stoneRateType='" + cmboStoneRateType.Text + "', stoneQuantity='" + txtStonePieces.Text + "', vendorId='" + cmboVendorId.Text + "', purchaseRate='" + txtPurchaseRate.Text + "', 'TRUE', 'NONE')" };
        String strMsg = "";
        if (!DBUtil.executeCommands(commands))
        {
            strMsg = "There occured some error while updating existing item to the stock. Please re-check all the details and try again later.\nIf the problem persists please contact service provider.";
            MessageBox.Show(strMsg, "Update existing Item", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return;
        }
        strMsg = "Item updated successfully to the stock.";
        MessageBox.Show(strMsg, "Update existing Item", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    private void doAdding()
    {
        String[] commands = { "Insert into tblItemDetail values('" + txtBarCode.Text + "', '" + txtOriginalTunch.Text + "', '" + txtPurchaseTunch.Text + "', '" + txtItemCode.Text + "', '" + txtGrossWeight.Text + "', '" + txtMaking.Text + "', '" + cmboMakingType.Text + "', '" + txtPieces.Text + "', '" + txtStoneWeight.Text + "', '" + txtStoneRate.Text + "', '" + cmboStoneRateType.Text + "', '" + txtStonePieces.Text + "', '" + cmboVendorId.Text + "', '" + txtPurchaseRate.Text + "', 'TRUE', 'NONE', '" + invoiceNumber + "', '" + invoiceDate + "')" };
        String strMsg = "";
        if (!DBUtil.executeCommands(commands))
        {
            strMsg = "There occured some error while adding new item to the stock. Please re-check all the details and try again later.\nIf the problem persists please contact service provider.";
            MessageBox.Show(strMsg, "Add New Item", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return;
        }
        strMsg = "New Item added successfully to the stock.";
        MessageBox.Show(strMsg, "Add New Item", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    private void FrmAddNewItem_Load(object sender, EventArgs e)
    {
        btnReset_Click(sender, e);
    }
}

I just need help to get a printout of that barcode, so that they can stick this barcode on their items. They will have barcode printer.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Ravi Sharma
  • 23
  • 1
  • 7

1 Answers1

0

Ravi,

You can show the barcode on a form, and use Print Form technique to print out the barcode!

See below links:
Printing Windows Form in C#
Print Forms C# without API
MSDN: How to Print Windows Form

UPDATE:
These links may help too:
What's the best way to get the default printer in .NET
How do I set the windows default printer in C#?

Community
  • 1
  • 1
  • are you sure that it will b printed in the barcode printer in the format which i have shown above..>? – Ravi Sharma Dec 01 '12 at 07:14
  • @RaviSharma Barcode once generated is simply an image, you might need to set the image size to fit barcode printer's need... But when you will be printing the form you will have complete flexibility and you can play with it easily! – Arsalan Adam Khatri Dec 01 '12 at 07:16
  • So you mean to say that i will make a form without border or titlebar.. and then it will ask user about the printer and then they will choose the correct barcode printer from which the print is to b taken out. because there ofcourse will b more than one printer, like one for billing and one for reports and one for barcode.. – Ravi Sharma Dec 01 '12 at 07:20
  • Yes exactly, do you want to automate the process and not let the user to select printer? – Arsalan Adam Khatri Dec 01 '12 at 07:22
  • M mean it will be good if for every item added it will automatically detect the crrect printer, bcause asking for printer every time will be bit uncomfortable for user.. As i think – Ravi Sharma Dec 01 '12 at 07:24
  • I guess, you can overcome the Print Dialog if you use PrintDialog.Print() function... But you need to set Barcode Printer as default, it uses default printer to print if we use .Print() function and this way will not ask the user to select Printer – Arsalan Adam Khatri Dec 01 '12 at 07:27
  • I hope you have solved my problem.. my barcode is getting priting on OnNote.. now i just have to check it with barcode printer and then it will be okay.. thanks.. If any problem will occur then i will ask again – Ravi Sharma Dec 01 '12 at 08:23