0

I have been trying to make the build in stored procedure to work with Register.aspx. I'm not using the wizard part. Hence writing my own code. I don't understand where the error is. If I use exec stored procedure and send the same value it will execute in Management Studio. But in code it fails.

Here is the code that does the execution of the stored procedure:

using (SqlConnection myConnection = new SqlConnection  (ConfigurationManager.ConnectionStrings["connection"].ToString()))
{
    using (SqlCommand insertNewUsers = new SqlCommand("aspnet_Membership_CreateUser", myConnection))
    {
        insertNewUsers.CommandType = CommandType.StoredProcedure;

        insertNewUsers.Parameters.Add("@ApplicationName", SqlDbType.NVarChar, 256).Value = AppName;
        insertNewUsers.Parameters.Add("@UserName", SqlDbType.NVarChar, 256).Value = UserName.Text;
        insertNewUsers.Parameters.Add("@Password ", SqlDbType.NVarChar, 128).Value = goHashPassword;
        insertNewUsers.Parameters.Add("@PasswordSalt", SqlDbType.NVarChar, 128).Value = newSalt;
        insertNewUsers.Parameters.Add("@Email", SqlDbType.NVarChar, 256).Value = Email.Text;
        //  insertNewUsers.Parameters.Add(("@LoweredEmail"), SqlDbType.NVarChar, 256).Value = (Email.Text).ToLower();
        insertNewUsers.Parameters.Add("@PasswordQuestion", SqlDbType.NVarChar, 256).Value = DBNull.Value ;
        insertNewUsers.Parameters.Add("@PasswordAnswer", SqlDbType.NVarChar, 128).Value = DBNull.Value;              
        insertNewUsers.Parameters.Add("@IsApproved", SqlDbType.Bit).Value = 1;
        // insertNewUsers.Parameters.Add("@IsLockedOut", SqlDbType.NVarChar, 1).Value = 0;
        insertNewUsers.Parameters.Add("@CurrentTimeUtc", SqlDbType.DateTime).Value = DateTime.UtcNow.Date;
        insertNewUsers.Parameters.Add("@CreateDate", SqlDbType.DateTime).Value = DateTime.Today.ToLocalTime();
        insertNewUsers.Parameters.Add("@UniqueEmail", SqlDbType.Int).Value = 0;
        insertNewUsers.Parameters.Add("@PasswordFormat", SqlDbType.Int).Value = 0;

        pID = insertNewUsers.Parameters.Add("@return_value", SqlDbType.Int);
        pID.Direction = ParameterDirection.ReturnValue;

        try
        {
            if (insertNewUsers.Connection.State == ConnectionState.Closed)
            {
                insertNewUsers.Connection.Open();
            }

            insertNewUsers.ExecuteNonQuery();  // HERE IS WHERE IT FAILS

            rowCount = (Int32)insertNewUsers.Parameters["@return_value"].Value;
         } // END TRY
         catch (Exception ex)
         {
             ErrorMessage.Text = ex.StackTrace.ToString();
         }
         finally
         {
             myConnection.Dispose();
         }
     }
}

Now if I run build in stored procedure with following parameters it runs and adds a user and creates a membership

DECLARE @return_value int,
        @UserId uniqueidentifier

 EXEC @return_value = [dbo].[aspnet_Membership_CreateUser]
        @ApplicationName = N'/',
        @UserName = N'Tester',
        @Password = N'cUW3NXKEEjiNkxOjhLXwhHFQApQ=',
        @PasswordSalt = N'GYkMYFzkDn0vm4Li',
        @Email = N'[email protected]',
        @PasswordQuestion = NULL,
        @PasswordAnswer = NULL,
        @IsApproved = 1,
        @CurrentTimeUtc =  N'20131027',
        @CreateDate =   N'20131027' ,
        @UniqueEmail = 0,
        @PasswordFormat = 0,
        @UserId = @UserId OUTPUT

  SELECT @UserId as N'@UserId'

  SELECT 'Return Value' = @return_value
  GO

I have checked datatype with the table and is fine. I'm not sure about the datetime stamp. on manual execution, i just have to give the 'yyyymmdd' like i have specified but in c# i have specified to be as seen in code. Hope to find some help on this

7
  • ".. But in Code it fails." How it's failing? Which error message you receive? And why don't you use the Membership API instead?
    – Oscar
    Aug 27, 2013 at 20:19
  • Put a breakpoint inside your catch and see what exception is being caught, if any. Aug 27, 2013 at 20:23
  • i capture a stacktrace error and it show like this at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at MaintainSuperUsers.Account.Register.CreateUserButton_Click(Object sender, EventArgs e) in \\DTCNAS-SCFM003\C_MTGNCL_Users\A104002\My Documents\Visual Studio 2010\Projects\MaintainSuperUsers\MaintainSuperUsers\Account\Register.aspx.cs:line 163
    – Ira
    Aug 27, 2013 at 20:26
  • 2
    You really shouldn't be using the stored procs.. they're an implementation detail. You should be using the Membership API, because different Membership api's use different database implementations, you can't depend on this. This is why the API exists. Aug 27, 2013 at 20:56
  • 1
    I agree. Use the API. "Not gonna learn anything"....There is business logic code in the API you're not seeing.......thus why the API exists. Aug 27, 2013 at 21:02

1 Answer 1

1

~Technically, you're missing a parameter in the DotNet code. (outputUserIdParam seen below)

~Practically, you should use the MembershipProvider API. You are re-inventing the wheel with your implementation.

            insertNewUsers.CommandType = CommandType.StoredProcedure;

            insertNewUsers.Parameters.Add("@ApplicationName", SqlDbType.NVarChar, 256).Value = AppName;
            insertNewUsers.Parameters.Add("@UserName", SqlDbType.NVarChar, 256).Value = UserName;
            insertNewUsers.Parameters.Add("@Password ", SqlDbType.NVarChar, 128).Value = pwd;
            insertNewUsers.Parameters.Add("@PasswordSalt", SqlDbType.NVarChar, 128).Value = string.Empty;
            insertNewUsers.Parameters.Add("@Email", SqlDbType.NVarChar, 256).Value = Email;
            //  insertNewUsers.Parameters.Add(("@LoweredEmail"), SqlDbType.NVarChar, 256).Value = (Email.Text).ToLower();
            insertNewUsers.Parameters.Add("@PasswordQuestion", SqlDbType.NVarChar, 256).Value = DBNull.Value;
            insertNewUsers.Parameters.Add("@PasswordAnswer", SqlDbType.NVarChar, 128).Value = DBNull.Value;
            insertNewUsers.Parameters.Add("@IsApproved", SqlDbType.Bit).Value = 1;
            // insertNewUsers.Parameters.Add("@IsLockedOut", SqlDbType.NVarChar, 1).Value = 0;
            insertNewUsers.Parameters.Add("@CurrentTimeUtc", SqlDbType.DateTime).Value = DateTime.UtcNow.Date;
            insertNewUsers.Parameters.Add("@CreateDate", SqlDbType.DateTime).Value = DateTime.Today.ToLocalTime();
            insertNewUsers.Parameters.Add("@UniqueEmail", SqlDbType.Int).Value = 0;
            insertNewUsers.Parameters.Add("@PasswordFormat", SqlDbType.Int).Value = 0;

            // Create parameter with Direction as Output (and correct name and type)
            SqlParameter outputUserIdParam = new SqlParameter("@UserId", SqlDbType.UniqueIdentifier)
            {
                Direction = ParameterDirection.Output
            };

            insertNewUsers.Parameters.Add(outputUserIdParam);


            pID = insertNewUsers.Parameters.Add("@return_value", SqlDbType.Int);
            pID.Direction = ParameterDirection.ReturnValue;
1
  • Appreciate your help on this. I have marked your answer as correct. Thanks once again to stackoverflow people who have come to help beginners.
    – Ira
    Aug 28, 2013 at 1:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.