Hi
I have below code but data is not getting updated.Neither i am getting any error message
C#
try
{
using (SqlConnection con = new SqlConnection(Common.CommonFunction.cnn_Live))
{
SqlCommand cmd = new SqlCommand("Sp_Department", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@EntryNo", SqlDbType.Int).Value = hdfDeleteID.Value;
cmd.Parameters.Add("@Action", SqlDbType.NVarChar).Value = "D";
con.Open();
cmd.ExecuteNonQuery();
string message = Common.CommonFunction.recordDeletedSucessfully;
ShowMessage("Success", "Record Saved Successfully", "Success");
GetData();
}
}
catch (Exception ex)
{
ShowMessage("Oops...", ex.Message, "error");
}Stored Procedure
SQL
PROCEDURE [dbo].[sp_Department]
@Action VARCHAR(1)
,@EntryNo int = Null
,@Description VARCHAR(50) = NULL
,@ShortName VARCHAR(25) = NULL
AS
BEGIN
SET NOCOUNT ON;
--UPDATE
IF @Action = 'U'
BEGIN
UPDATE dbo.Department
SET Description = @Description, updatedon = GETDATE() WHERE docentry = @EntryNo
END
--DELETE
IF @Action = 'D'
BEGIN
UPDATE dbo.Department
SET status = 0 , updatedon = GETDATE() WHERE docentry = @EntryNo
END
ENDThanks

Gowtham CpRank #1,552 · 1K reputationPosted Aug 17, 2024, 3:44 AM EDT
Hello Ramco Ramco ,
Issue: The
DELETEblock was incorrectly using anUPDATEstatement to set the status to0instead of removing the record.Corrected Code:
PROCEDURE [dbo].[sp_Department] @Action VARCHAR(1), @EntryNo INT = NULL, @Description VARCHAR(50) = NULL, @ShortName VARCHAR(25) = NULL AS BEGIN SET NOCOUNT ON; -- UPDATE IF @Action = 'U' BEGIN UPDATE dbo.Department SET Description = @Description, updatedon = GETDATE() WHERE docentry = @EntryNo END -- DELETE IF @Action = 'D' BEGIN DELETE FROM dbo.Department WHERE docentry = @EntryNo END ENDRamco RamcoRank #462 · 5.4K reputationPosted Aug 17, 2024, 4:27 AM EDT
Hi Gowtham
I want to update only. I don't want to delete record.
Currently it is not updating
Thanks
David Madden8 reputationPosted Aug 17, 2024, 9:53 AM EDT
cmd.Parameters.Add("@EntryNo", SqlDbType.Int).Value = hdfDeleteID.Value;Check your line of code during execution in Debug to see if hdfDeleteID does have value, then, after stepping over that line, check Parameters to see if the expected entry is there and have the expected value. It is like that @EntryNo is not getting a value as expected and taking NULL as the value.