Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello

I have a stored procedure in which i have a delete statement as

delete from table where id=@id

now i want to know how to delete a row in c# asp.net.

what is the code for this.
Plz help
Posted
Comments
RDBurmon 13-Jun-12 9:29am    
Thanks Everyone who replied to this thread , So Saurabh, I think you have got enough responses and you should be able to mark it as your answer and close the thread. Please do so.

Either execute the stored procedure:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("myProcedureName", con))
        {
        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.AddWithValue("@Param1", "My Parameter 1 value");
        com.Parameters.AddWithValue("@Param2", "My Parameter 1 value");
        com.Parameters.AddWithValue("@Param3", "My Parameter 1 value");
        com.ExecuteNonQuery();
        }
    }


or just execute it as code:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("DELETE FROM table WHERE id=@ID", con))
        {
        com.Parameters.AddWithValue("@ID", idToDelete);
        com.ExecuteNonQuery();
        }
    }
 
Share this answer
 
Comments
Code 89 13-Jun-12 2:03am    
+5!
Refer this code:
C#
SqlConnection connection = new System.Data.SqlClient.SqlConnection(strConnections);
string Command = "select * from tableName WHERE ID = " + txtID.Text.Trim() + "";
SqlDataAdapter dataAdapter2 = new SqlDataAdapter(Command, strConnections);
System.Data.SqlClient.SqlCommand oCommand = new System.Data.SqlClient.SqlCommand();
connection.Open();
SqlTransaction transaction = connection.BeginTransaction();
oCommand.Connection = connection;
oCommand.Transaction = transaction;
oCommand.CommandText = "deleteVendPymnt";
oCommand.CommandType = CommandType.StoredProcedure;
dataAdapter2.UpdateCommand = oCommand;
dataAdapter2.UpdateCommand.Transaction = transaction;
try
{
 oCommand.ExecuteNonQuery();
 transaction.Commit();
 connection.Close();
 MessageBox.Show("Delete succeed!", "Delete option", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
catch
{
 transaction.Rollback();
 MessageBox.Show("Concurrency error!!!", "Delete option", MessageBoxButtons.OK,  MessageBoxIcon.Stop);
}


Also refer similar answer:
Database Insert and Delete with Stored Procedure[^]
 
Share this answer
 
Comments
Code 89 13-Jun-12 2:03am    
5!
Prasad_Kulkarni 13-Jun-12 2:35am    
Thank you Code 89
Manas Bhardwaj 13-Jun-12 4:08am    
5ed!
Prasad_Kulkarni 13-Jun-12 4:50am    
Thank you Manas!
 
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