Hi guys,
I'm still new to .NET and your answers to my questions have been so helpful. I have another question yet again, however. This time my question has to do with sql server and handling it with code behind. I am creating a stored procedure which is supposed to check if a record exists in the table and if it does, return a value so that I may tell the user that the record already exists. If it does not exist, and insert statement is executed.
Here is my stored procedure:
@UserID VARCHAR(50),
@PlayerID VARCHAR(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
IF EXISTS (SELECT 1 FROM FB_UserSelections WHERE PlayerID = @PlayerID)
RETURN 0
ELSE
INSERT INTO FB_UserSelections (UserID, PlayerID) VALUES (@UserID, @PlayerID)
RETURN 1
END
GO
Am I correctly returning a value? If so how do I handle this in the code behind so that I may set a label to visible invisible? Here is what I am attempting in the code behind:
String Pid = Request.QueryString["PlayerID"];
String UserId = Request.QueryString["UserID"];
using (SqlConnection con = new SqlConnection(constr))
{
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "FB_CheckIfDrafted";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserID", UserId);
cmd.Parameters.AddWithValue("@PlayerID", Pid);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
LBl_DraftedPrompt.Visible = true;
I want to store the returned value of the SP in a variable in the code behind so that I could do an if/else statement to either display Lbl_DraftedPrompt or not. Thank you for your time and help.
EDIT
I have been testing my SP and it seems to work as I want because PlayerID can only exist once in the FB_UserSelections table. The only thing i'm having trouble with now is handling the RETURNED 1 or 0 in the code behind. But if you see any issues with what you see above, please let me know.