Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
try
            {
                if (txtDesc1.Text == "")
                {
                }
                else
                {
                    con1 = new SqlDbConnect();
                    strSQL = "insert into sample (id, description, dosage, qty, selling, amount)";
                    strSQL += "select '" + labelTransID.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtDesc1.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtDosage1.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtQty1.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtSelling1.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtAmount1.Text.Replace("'", "''") + "'";
                    con1.SqlQuery(strSQL);
                    con1.NonQueryEx();
                    con1.Close();
                }

                if (txtDesc2.Text == "")
                {
                }
                else
                {
                    con2 = new SqlDbConnect();
                    strSQL = "insert into sample (id, description, dosage, qty, selling, amount)";
                    strSQL += "select '" + labelTransID.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtDesc2.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtDosage2.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtQty2.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtSelling2.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtAmount2.Text.Replace("'", "''") + "'";
                    con2.SqlQuery(strSQL);
                    con2.NonQueryEx();
                    con2.Close();
                }
                if (txtDesc3.Text == "")
                {
                }
                else
                {
                    con3 = new SqlDbConnect();
                    strSQL = "insert into sample (id, description, dosage, qty, selling, amount)";
                    strSQL += "select '" + labelTransID.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtDesc3.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtDosage3.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtQty3.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtSelling3.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtAmount3.Text.Replace("'", "''") + "'";
                    con3.SqlQuery(strSQL);
                    con3.NonQueryEx();
                    con3.Close();
                }
                if (txtDesc4.Text == "")
                {
                }
                else
                {
                    con4 = new SqlDbConnect();
                    strSQL = "insert into sample (id, description, dosage, qty, selling, amount)";
                    strSQL += "select '" + labelTransID.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtDesc4.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtDosage4.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtQty4.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtSelling4.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtAmount4.Text.Replace("'", "''") + "'";
                    con4.SqlQuery(strSQL);
                    con4.NonQueryEx();
                    con4.Close();
                }
                if (txtDesc5.Text == "")
                {
                }
                else
                {
                    con5 = new SqlDbConnect();
                    strSQL = "insert into sample (id, description, dosage, qty, selling, amount)";
                    strSQL += "select '" + labelTransID.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtDesc5.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtDosage5.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtQty5.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtSelling5.Text.Replace("'", "''") + "'";
                    strSQL += ", '" + txtAmount5.Text.Replace("'", "''") + "'";
                    con5.SqlQuery(strSQL);
                    con5.NonQueryEx();
                    con5.Close();
                }
            }
            catch
            {
                MessageBox.Show("Successfully Saved", "Completed !", MessageBoxButtons.OK);
            }


here is my code for saving...problem is when the other textboxes are empty. won't save to sql database. is there any other approach to minimize this lot of codes.ty
Posted
Updated 16-Apr-14 1:29am
v2
Comments
Mike Meinz 16-Apr-14 7:32am    
Please use the SQLParameter class to remove the potential for SQL Injection attacks against your software. By taking data directly from TextBoxes and inserting it into dynamically created SQL INSERT statements, you provide a path for an attacker to damage your web site by altering or deleting data.

Try running this example and discover the mistakes in your code:
C#
using System;

public class Program
{
    public static void Main()
    {
        string str = "we're a nation";
        string str1 = str.Replace("'","''");
        Console.WriteLine(str);
        Console.WriteLine(str1);
    }
}

You also should
Use Parameterized queries to prevent SQL Injection Attacks in SQL Server
[^]
 
Share this answer
 
v3
I suggest you, that first made xml of all data which you want to insert on front end, then send it into procedure and after that you can insert it from that xml.
 
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