How to can we get the row details of the row getting detete in vb. Net

Diksha dudi 126 Reputation points
2022-04-21T04:15:37.607+00:00

I want to get the row details of this query

194945-screenshot-2022-04-21-09-44-21-04-99c04817c0de5652.jpg

Developer technologies | ASP.NET | Other
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 78,236 Reputation points Volunteer Moderator
    2022-04-21T17:14:35.14+00:00

    first you should fix the sql injection Security flaw in your code.

    you should really use triggers rather than code to handle cascading deletes. but you can use the output clause to get the key fields

        var query = @" 
            delete orders 
            where prod_id =@pid and user_id=@uid 
            output deleted.Id 
        "; 
    
        using (var conn = new SqlConnection(strcon)) 
        { 
            conn.Open(); 
            var cmd = new SqlCommand(query, conn); 
            cmd.Parameters.AddWithValue("@pid", prodid); 
            cmd.Parameters.AddWithValue("@uid", user_id); 
            using (var reader = cmd.ExecuteReader()) 
            { 
                while(reader.HasRows) 
                { 
                    Console.WriteLine(reader.GetSqlInt32(0)); 
                } 
            } 
        } 
    } 
    

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2022-04-21T10:57:43.403+00:00

    Now i want the detail of the column datetime of the row which is going to be deleted. So that i can delete from another table which has the same column

    Seems like a very simply solution. Execute a "SELECT" query to get the record then do the "DELETE". However, needing a DateTime value to delete another records sounds like a design bug.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.