Click here to Skip to main content
15,919,479 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
SqlConnection sqlCon = new SqlConnection(str);

        //Call the statement to delete
        SqlCommand sqlCmd = new SqlCommand("Delete from Transfer  Where empno='" + txt_Empno.Text + "'", sqlCon);
        sqlCmd.CommandType = System.Data.CommandType.Text;



I need to include a second field in the delete command please assist


say Trans_No is a field with text name txt_Trans

Please include it in the delete command above

What I have tried:

I have revisited my earlier codes
Posted
Updated 6-Aug-16 3:05am
v3

try this

C#
SqlConnection sqlCon = new SqlConnection(str);
            SqlCommand sqlCmd = new SqlCommand("Delete from Transfer  Where empno=@empno and Trans_No=@tranno", sqlCon);
            sqlCmd.Parameters.Add("@empno", txt_Empno.Text);
            sqlCmd.Parameters.Add("@tranno", txt_Trans.Text);
            sqlCmd.CommandType = System.Data.CommandType.Text;

note: your code is Vulnerable to SQL Injection[^]
always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]
 
Share this answer
 
First off, don;t do that. Do not 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. Use Parametrized queries instead.
Second, to delete based on two clauses is simple - just use an AND:
SQL
DELETE FROM MyTable WHERE Column1 = 1 AND Column2 = 2
 
Share this answer
 
Use AND OR where needed :
SQL
Delete from Transfer Where empno = "value1" and trans_no = "value2"
 
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