Click here to Skip to main content
15,917,731 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code in button1 click event

C#
if (cn.State == ConnectionState.Closed) cn.Open();
            cm.Connection = cn;
            foreach (ListViewItem item in listView1.Items)
            {
                item.Selected = true;

            }
            ListViewItem lvi2 = listView1.SelectedItems[0];
            string tableName = lvi2.SubItems[5].Text;
            string sql = "UPDATE " + tableName + " SET [itmcount] = [itmcount] - @itmcount1 WHERE [itmcode] = @itmcode1 AND [itmcount] IS NOT NULL";
            cm.CommandText = sql;
            foreach (ListViewItem item in listView1.Items)
            {
                SqlParameter par_ItmCount = new SqlParameter("@itmcount1", SqlDbType.Int);
                par_ItmCount.Value = int.Parse(item.SubItems[3].Text);
                cm.Parameters.Add(par_ItmCount);

                SqlParameter par_ItmCode = new SqlParameter("@itmcode1", SqlDbType.NVarChar);
                par_ItmCode.Value = item.Text;
                cm.Parameters.Add(par_ItmCode);
                {
                    cm.ExecuteNonQuery();
                    
                }
                
            }


as u can see my tablename is not a Parameter so how to change this code to sql stored PROCEDURE to prevent sql injection ?
Posted
Comments
PIEBALDconsult 8-Nov-15 17:14pm    
I recommend redesigning your database to avoid having to do that.

There are ways to do this but I don't understand why you need multiple tables with same structure.

You can achieve such requirement by using sp_executesql[^] Your stored procedure should look something like following-

Check these articles and try writing the stored proc yourself and if you face any problem, come back here and ask with what you have tried.

Using sp_executesql[^]
Sp_executesql - TSQL Tutorial[^]

Hope, it helps :)
 
Share this answer
 
Comments
Ahmed Zoeil 8-Nov-15 15:58pm    
thanks for your reply but I still can't get it

can u show me what code should I I use for my stored procedure and what code should I use for button1 click plz i'm stuck for 2 days :)
Send your ListView data as a Table parameter and update it on SQL
Please follow the link for how to create User Defined Table in SQL
http://www.c-sharpcorner.com/UploadFile/4d9083/insert-and-update-in-sql-using-user-defined-table-type-and-x/[^]
 
Share this answer
 
This looks like a poor database design, but if you have to do it, you need to properly validate the table name, and use sp_executesql[^] to execute the query:
SQL
CREATE PROC dbo.usp_UpdateItemCount
(
    @TableName    sysname,
    @ItmCode1     nvarchar(50), -- TODO: Set the correct size here
    @ItmCount1    int
)
As
BEGIN
DECLARE @RealTableName sysname;
DECLARE @Statement nvarchar(max);
    
    SET NOCOUNT ON;
    
    SELECT
        @RealTableName = name
    FROM
        sys.tables
    WHERE
        name = @TableName
    ;
    
    If @@ROWCOUNT = 0 RAISERROR('Table "%s" was not found.', 16, 1, @TableName);
    
    SET @Statement = N'UPDATE ' + QUOTENAME(@RealTableName) 
    + N' SET [itmcount] = [itmcount] - @itmcount1'
    + N' WHERE [itmcode] = @itmcode1 AND [itmcount] IS NOT NULL';
    
    EXEC sp_executesql @Statement, 
        -- TODO: Set the correct size for this parameter as well
        N'@ItmCode1 nvarchar(50), @ItmCount1 int',
        @ItmCode1 = @ItmCode1,
        @ItmCount1 = @ItmCount1
    ;
END
 
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