I have a stored procedure:
ALTER PROCEDURE [dbo].[pr_Tbl_Test_Insert]
@guidid uniqueidentifier output,
@sname nvarchar(50)
AS
-- INSERT a new row in the table.
INSERT [dbo].[Tbl_Test]
(
[id],
[name]
)
VALUES
(
ISNULl(@guidid, (newid())),
@sname
)
I need the id
in C# and put it output
in c#:
cmd.Parameters.AddWithValue("@guidid",_id);//_id is SqlGuid
cmd.Parameters.AddWithValue("@sname", "mehdi");
cmd.ExecuteNonQuery();
MessageBox.Show(_id.ToString());
but messagebox show the null value!!
How can I return the id?
I changed it to:
ALTER PROCEDURE [dbo].[pr_Tbl_Test_Insert]
@guidid uniqueidentifier output,
@sname nvarchar(50)
AS
DECLARE @NewID UNIQUEIDENTIFIER
SET @NewID = newid();
-- INSERT a new row in the table.
INSERT [dbo].[Tbl_Test]([id], [name]) VALUES(@NewID, @sname);
SET @guidid = @NewID
and C#
SqlParameter outparam = cmd.Parameters.Add("@guidid",SqlDbType.UniqueIdentifier);
outparam.Direction = ParameterDirection.Output;
cmd.Parameters.AddWithValue("@sname", "mehdi");
cmd.ExecuteNonQuery();
MessageBox.Show(_id.Value.ToString());
but it doesn't return anything
See Question&Answers more detail:os