I have this stored procedure that inserted a record into a table and returned the column value IDENTITYwith Output Inserted.KID:
ALTER procedure [dbo].[InsertNode]
(
@FName nvarchar(50),
@Lname nvarchar(50),
@CDesc nvarchar(max),
@ParentID int
)
as
begin
insert into Chart (FName , Lname ,CDesc, ParentID )
Output Inserted.KID
values (@FName, @Lname, @CDesc, @ParentID)
end
C # code:
public void InsertNode(string FName, string LName, string cDesc, int pid)
{
try
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("InsertNode", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FName", FName);
cmd.Parameters.AddWithValue("@Lname", LName);
cmd.Parameters.AddWithValue("@CDesc", cDesc);
cmd.Parameters.AddWithValue("@ParentID", pid);
con.Open();
Int32 retVal = cmd.ExecuteNonQuery();
}
}
}
catch (Exception Ex)
{
Response.Write( "ERROR: Unable to save data !!</br>" + Ex.Message);
}
Response.Write( "Data Saved Successfully!");
}
Is it right and safe?
Now, how to get the value OUTPUTfrom a stored procedure in asp.net (C #)?
source
share