Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
public void SaveProduct()
        {
            SqlConnection con = new SqlConnection(sqlcon);
            con.Open();

 SqlCommand cmd = new SqlCommand("Insert into tbl_Product (BillNo,Prod_Name,Price,Quantity,Sum) values ('" + txtBillNo.Text + "','" + txtprod1.Text + "','" + txtQnt1.Text + "','" + txtPriceUnit1.Text + "','" + txtPrice1.Text + "')," 
+ "('" + txtBillNo.Text + "','" + txtProd2.Text + "','" + txtQnt2.Text + "','" + txtPriceUnit2.Text + "','" + txtPrice2.Text + "')," 

+ "('" + txtBillNo.Text + "','" + txtProd3.Text + "','" + txtQnt3.Text + "','" + txtPriceUnit3.Text + "','" + txtPrice3.Text + "'),"

 + "('" + txtBillNo.Text + "','" + txtProd4.Text + "','" + txtQnt4.Text + "','" + txtPriceUnit4.Text + "','" + txtPrice4.Text + "')," 

+ "('" + txtBillNo.Text + "','" + txtProd5.Text + "','" + txtQnt5.Text + "','" + txtPriceUnit5.Text + "','" + txtPrice5.Text + "')," 

+ "('" + txtBillNo.Text + "','" + txtProd6.Text + "','" + txtQnt6.Text + "','" + txtPriceUnit6.Text + "','" + txtPrice6.Text + "'),"

 + "('" + txtBillNo.Text + "','" + txtProd7.Text + "','" + txtQnt7.Text + "','" + txtPriceUnit7.Text + "','" + txtPrice7.Text + "'),"

 + "('" + txtBillNo.Text + "','" + txtProd8.Text + "','" + txtQnt8.Text + "','" + txtPriceUnit8.Text + "','" + txtPrice8.Text + "'),"

 + "('" + txtBillNo.Text + "','" + txtProd9.Text + "','" + txtQnt9.Text + "','" + txtPriceUnit9.Text + "','" + txtPrice9.Text + "')," 

+ "('" + txtBillNo.Text + "','" + txtProd10.Text + "','" + txtQnt10.Text + "','" + txtPriceUnit10.Text + "','" + txtPrice10.Text + "')", con);
            if (cmd.ExecuteNonQuery() > 0)
            {
                totalCost();
            }
        }


This is a bill print project. for store maximum 10 record I take these text boxes. But when I want to store less than ten record I get error. So how can I modified this code to store less than 10 or maximum 10 records.
Posted
Updated 28-Aug-12 4:59am
v3
Comments
[no name] 28-Aug-12 10:52am    
No matter the number of records, you should never ever use string concatenation to build SQL queries. This is just an invitation to SQL injection attacks. And you are really trying to insert all of these values in to the 5 columns that you have defined?
[no name] 28-Aug-12 10:56am    
Why you don't use string.Format() ?
You can define an string variable then use it as parameter.
It is easier, isn't it?

I wouldn't bother looking at this code. I am sure that I am gonna get some hard words for this, but to be very frank this code look horrible.
Have you ever heard of SQL Injection[^]?

I would rewrite it to make use of stored procedure and better structure.
 
Share this answer
 
Please use parametrized queries, it's much safer and easier.

Your problem is that when you enter less than 10 records, some of the input textboxes are empty and you get invalid SQL query.
For each record you should first check if there is data in textbox and only then append data to the query.
 
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