Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi to all!

i have a table with
item1
item2
item3
item4
item5

from c sharp i have to delete one record by passing two variables
i make it like this problem is of and, i use comma still there is error can u tell me how to handle it

("DELETE FROM [database].[table] WHERE item1=" + Int1+ " and "item2="+Int2)
Posted

Try this:

C#
string delquery="Delete * From [database].[table] Where item1 = ? And item2 = ?";

OleDbCommand cmd = new OleDbCommand(delquery, conn);

cmd.CommandType = CommandType.Text;

    cmd.Parameters.AddWithValue("item1", Int1);

    cmd.Parameters.AddWithValue("item2", Int2);

    conn.Open();

    cmd.ExecuteNonQuery();


remember always use parametrized query or stored procedures to avoid sql-injection attacks.

hope it helps :)

for further queries comment here!
 
Share this answer
 
Comments
RaviRanjanKr 8-Sep-11 3:23am    
My 5+
Uday P.Singh 8-Sep-11 3:24am    
thanks Ravi :)
nagendrathecoder 8-Sep-11 3:28am    
Good answer, my 5
Uday P.Singh 8-Sep-11 5:34am    
thanks nagendra:)
If items are string datatype then you need to add values within single quotes in query like this:
DELETE FROM [database].[table] WHERE item1='" + Int1+ "' and item2='"+Int2+"'"

Try it.
 
Share this answer
 
Comments
Uday P.Singh 8-Sep-11 3:11am    
yes, but what about sql-injection? that's why voted 4!
nagendrathecoder 8-Sep-11 3:15am    
Thats right.
kami124 8-Sep-11 3:14am    
no i voted 5 to u
nagendrathecoder 8-Sep-11 3:15am    
Thanks Kami. But uday's point is also valid.
You must tackle SQL injections. :)
Use following code:

("DELETE FROM [database].[table] WHERE item1='" + Int1+ "' and item2='"+Int2+"')

and i like to recommend you that delete records using "primary key"
 
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