Click here to Skip to main content
14,548,222 members
Rate this:
Please Sign up or sign in to vote.
I have a combobox whose item is as below:
40 Barcodes per sheet (A4) (1.799" X 1.003")
30 Barcodes per sheet (2.625" X 1")
24 Barcodes per sheet (A4) (2.48" X 1.334")
20 Barcodes per sheet (4" X 1")
18 Barcodes per sheet (A4) (2.5" X 1.835")
14 Barcodes per sheet (A4) (4" X 1.33")
12 Barcodes per sheet (A4) (2.5" X 2.834")
10 Barcodes per sheet (A4) (4" X 2")

I want to print the crystal report depending upon the item selected from combobox.How to do that ?

What I have tried:

public void generateBarcode()
                string cs =ConfigurationManager.ConnectionStrings["abc"].ConnectionString;
                SqlConnection con = new SqlConnection(cs);
                string query = "SELECT * FROM products WHERE id='" + txtProductId.Text + "'";
                for (int i = 1; i < int.Parse(txtNoOfCopies.Text); i++)
                    query += "UNION ALL SELECT * FROM products WHERE id= '" + int.Parse(txtProductId.Text) + "'";
                SqlDataAdapter sda = new SqlDataAdapter(query, con);
                DataSet ds = new DataSet();
                sda.Fill(ds, "products");
                crystalReportViewer1.ReportSource = crystal;
            catch (SqlException sqlEx)
            catch (Exception ex)

        private void btnGenerateBarcode_Click(object sender, EventArgs e)
Updated 22-Dec-19 21:49pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Not like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
A perfectly valid "delete the table" command
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And for your other problem, start here: How to print crystal report on different paper size - Google Search[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100