The stored procedure does not insert values ​​into the table

I saved a procedure in which the updatevalues are first in the table UserSignUpand then insertin the table UserKeyPoints, but my procedure does not execute.

Here is my stored procedure:

CREATE PROC [dbo].[proc_getActivationCode] @ActivationCode VARCHAR(1000)=''
AS
  BEGIN
      IF EXISTS(SELECT ActivationCode
                FROM   UserSignUp
                WHERE  ActivationCode = @ActivationCode
                       AND Activate = 'False')
        BEGIN
            DECLARE @UserId INT

            SET @userid= (SELECT AutoID
                          FROM   UserSignUp
                          WHERE  ActivationCode = @ActivationCode)

            UPDATE UserSignUp
            SET    Activate = 'Confirm Code'
            WHERE  ActivationCode = @ActivationCode

            INSERT INTO UserKeyPoints
                        (KeyPoints,
                         UserId)
            VALUES      (500,
                         @userid)

            SELECT 1
        END
      ELSE
        BEGIN
            SELECT 2
        END
  END 

Here is the C # code where I execute my stored procedure.

if (Request.QueryString["token"] != null)
{
    Label1.Text = Request.QueryString["token"];
    con.Open();
    SqlCommand cmd = new SqlCommand("proc_getActivationCode1", con);
    cmd.Parameters.AddWithValue("@ActivationCode", Request.QueryString["token"].ToString());
    SqlDataReader dr = cmd.ExecuteReader();
    DataTable dt = new DataTable();
    dt.Load(dr);
    dr.Close();
    con.Close();

    if (dt.Rows[0][0].ToString() == "1")
    {
        //Label1.Text = "You are confirmed successfully. Please Click here for Login: ";
        SendEmail objMail = new SendEmail();

    }
    else
    {
        Label1.Text = "You are already confirmed.";
    }
}

When I execute this code, it starts the procedure without insertand update, and on my page .aspxI get the output Label1ieYou are already confirmed.

Can anyone advise me where I am going wrong?

+4
source share
1 answer

, , CommandType, StoredProcedure.
, .

SqlCommand cmd = new SqlCommand("proc_getActivationCode1", con);
cmd.CommandType = CommandType.StoredProcedure;

Martin Smith, , CommandType @ActivationCode

, ExecuteScalar SqlDataAdapter, datatable

SqlCommand cmd = new SqlCommand("proc_getActivationCode1", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ActivationCode", Request.QueryString["token"].ToString());
object result = cmd.ExecuteScalar();
if(result != null)
{
    int resultValue = Convert.ToInt32(result);
    if (resultValue == 1)
        SendEmail objMail = new SendEmail();
    else
        Label1.Text = "You are already confirmed.";
}
+4

All Articles