whisk
whisk

Reputation: 655

Stored Procedure call in MVC 4 EF5 Not returning the correct data

I'm calling a stored procedure that is creating a randomly generated temp password for new users for a internal system. I have and call other stored procedures and they work just fine. For some reason this one is returning "System.Data.Objects.ObjectResult1[System.String]" when it should be return something like this, UtUdHUVx7fCu.

I have tried changing <usp_GeneratePassword_Result> in the Context.cs to just <string> but that gives me the same error.

SP

ALTER PROCEDURE [dbo].[usp_GeneratePassword]

@PasswordLength INT

AS
BEGIN

DECLARE @SourceString VARCHAR(100)
SET @SourceString = 'abcdefhkmnpqrstuvwxyzABCDEFHJKMNPQRSTUVWXYZ23456789'

SELECT (
    SELECT  TOP (@PasswordLength)
            SUBSTRING(@SourceString, 1 + (CAST(ROUND(RAND(CHECKSUM(NEWID())) * 1000, 0) AS INT) % LEN(@SourceString)), 1)
    FROM    sysobjects
    FOR     XML PATH('')
) AS GeneratedPassword


END

Controller

    private AdventureWorksEntities db = new AdventureWorksEntities();
    ^^ this is at the top 

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Register(mvcUser model)
    {
        if (ModelState.IsValid)
        {

            var pass = db.usp_GeneratePassword(10);
            model.UserPassword = pass.ToString();

            db.mvcUsers.Add(model);
            db.SaveChanges();
            ModelState.Clear();
            model = null;

        }

        // If we got this far, something failed, redisplay form
        return RedirectToAction("index","Home");
    }

Model

    public partial class usp_GeneratePassword_Result
{
    public string GeneratedPassword { get; set; }
}

Context.cs

        public virtual ObjectResult<usp_GeneratePassword_Result> GeneratePassword(Nullable<int> passwordLength)
    {
        var passwordLengthParameter = passwordLength.HasValue ?
            new ObjectParameter("PasswordLength", passwordLength) :
            new ObjectParameter("PasswordLength", typeof(int));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<usp_GeneratePassword_Result>("GeneratePassword", passwordLengthParameter);
    }

Image for reference again if i am missing anything please let me know.

At break point

Upvotes: 1

Views: 350

Answers (1)

poohbear
poohbear

Reputation: 452

Your information is getting put into a list so you might want to try something like this.

 if (ModelState.IsValid)
    {

            var pass = db.usp_GeneratePassword(10).ToList();
            model.UserPassword = pass[0];                

            db.mvcUsers.Add(model);
            db.SaveChanges();
            ModelState.Clear();
            model = null;

    }

Upvotes: 1

Related Questions