Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
public void generateBarcode()
        {
            try
            {
                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");
                crystal.SetDataSource(ds);
                crystalReportViewer1.ReportSource = crystal;
            }
            catch (SqlException sqlEx)
            {
                MessageBox.Show(sqlEx.Message.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

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

1 solution

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:
SQL
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:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
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[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900