Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 307475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:33:43+00:00 2026-05-12T07:33:43+00:00

I tried a lot on implementing a custom profile provider in ASP.NET MVC. I’ve

  • 0

I tried a lot on implementing a custom profile provider in ASP.NET MVC.
I’ve read lots and lots of tutorials, but I can’t find where my problem is. It’s pretty similar to Implementing Profile Provider in ASP.NET MVC.

But I’d like to create my own Profile Provider, so I wrote the following class that inherits from ProfileProvider:

public class UserProfileProvider : ProfileProvider
{
    #region Variables
    public override string ApplicationName { get; set; }
    public string ConnectionString { get; set; }
    public string UpdateProcedure { get; set; }
    public string GetProcedure { get; set; }
    #endregion

    #region Methods
    public UserProfileProvider()
    {  }

    internal static string GetConnectionString(string specifiedConnectionString)
    {
        if (String.IsNullOrEmpty(specifiedConnectionString))
            return null;

        // Check <connectionStrings> config section for this connection string
        ConnectionStringSettings connObj = ConfigurationManager.ConnectionStrings[specifiedConnectionString];
        if (connObj != null)
            return connObj.ConnectionString;

        return null;
    }
    #endregion

    #region ProfileProvider Methods Implementation
    public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
    {
        if (config == null)
            throw new ArgumentNullException("config");

        if (String.IsNullOrEmpty(name))
            name = "UserProfileProvider";

        if (String.IsNullOrEmpty(config["description"]))
        {
            config.Remove("description");
            config.Add("description", "My user custom profile provider");
        }

        base.Initialize(name, config);

        if (String.IsNullOrEmpty(config["connectionStringName"]))
            throw new ProviderException("connectionStringName not specified");

        ConnectionString = GetConnectionString(config["connectionStringName"]);

        if (String.IsNullOrEmpty(ConnectionString))
            throw new ProviderException("connectionStringName not specified");


        if ((config["applicationName"] == null) || String.IsNullOrEmpty(config["applicationName"]))
            ApplicationName = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath;
        else
            ApplicationName = config["applicationName"];

        if (ApplicationName.Length > 256)
            throw new ProviderException("Application name too long");

        UpdateProcedure = config["updateUserProcedure"];
        if (String.IsNullOrEmpty(UpdateProcedure))
            throw new ProviderException("updateUserProcedure not specified");

        GetProcedure = config["getUserProcedure"];
        if (String.IsNullOrEmpty(GetProcedure))
            throw new ProviderException("getUserProcedure not specified");
    }

    public override System.Configuration.SettingsPropertyValueCollection GetPropertyValues(System.Configuration.SettingsContext context, System.Configuration.SettingsPropertyCollection collection)
    {
        SettingsPropertyValueCollection values = new SettingsPropertyValueCollection();

        SqlConnection myConnection = new SqlConnection(ConnectionString);
        SqlCommand myCommand = new SqlCommand(GetProcedure, myConnection);
        myCommand.CommandType = CommandType.StoredProcedure;

        myCommand.Parameters.AddWithValue("@FirstName", (string)context["FirstName"]);

        try
        {
            myConnection.Open();
            SqlDataReader reader = myCommand.ExecuteReader(CommandBehavior.SingleRow);

            reader.Read();

            foreach (SettingsProperty property in collection)
            {
                SettingsPropertyValue value = new SettingsPropertyValue(property);

                if (reader.HasRows)
                {
                    value.PropertyValue = reader[property.Name];
                    values.Add(value);
                }
            }

        }
        finally
        {
            myConnection.Close();
            myCommand.Dispose();
        }

        return values;
    }

    public override void SetPropertyValues(System.Configuration.SettingsContext context, System.Configuration.SettingsPropertyValueCollection collection)
    {
        SqlConnection myConnection = new SqlConnection(ConnectionString);
        SqlCommand myCommand = new SqlCommand(UpdateProcedure, myConnection);
        myCommand.CommandType = CommandType.StoredProcedure;

        foreach (SettingsPropertyValue value in collection)
        {
            myCommand.Parameters.AddWithValue(value.Name, value.PropertyValue);
        }

        myCommand.Parameters.AddWithValue("@FirstName", (string)context["FirstName"]);

        try
        {
            myConnection.Open();
            myCommand.ExecuteNonQuery();
        }

        finally
        {
            myConnection.Close();
            myCommand.Dispose();
        }
    }

Here is my CreateProfile action in my Controller:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateProfile(string Username, string Password, string FirstName, string LastName)
{
    MembershipCreateStatus IsCreated = MembershipCreateStatus.ProviderError;
    MembershipUser user = null;

    user = Membership.CreateUser(Username, Password, "test@test.com", "Q", "A", true, out IsCreated);

    if (IsCreated == MembershipCreateStatus.Success && user != null)
    {
        ProfileCommon profile = (ProfileCommon)ProfileBase.Create(user.UserName);

        profile.FirstName = FirstName;
        profile.LastName = LastName;
        profile.Save();
    }

    return RedirectToAction("Index", "Home");
}

My procedure usp_GetUserProcedure is nothing special:

ALTER PROCEDURE [dbo].[usp_GetUserProcedure] 
-- Add the parameters for the stored procedure here
@FirstName varchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
SELECT * FROM dbo.Users WHERE FirstName = @FirstName
END

And my Web.Config file:

<profile enabled="true"
         automaticSaveEnabled="false"
         defaultProvider="UserProfileProvider"
         inherits="Test.Models.ProfileCommon">
<providers>
<clear/>
<add name="UserProfileProvider"
         type="Test.Controllers.UserProfileProvider"
         connectionStringName="ApplicationServices"
         applicationName="UserProfileProvider"
         getUserProcedure="usp_GetUserProcedure"
         updateUserProcedure="usp_UpdateUserProcedure"/>
</providers>
</profile>

But I always get this exception:

Procedure or function ‘usp_GetUserProcedure’ expects parameter ‘@FirstName’, which was not supplied.

Any thoughts on what I might be doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:33:43+00:00Added an answer on May 12, 2026 at 7:33 am

    The most likely cause is that

    myCommand.Parameters.AddWithValue("@FirstName", (string)context["FirstName"]);
    

    (string)context["FirstName"] is a null value. Even if you pass in a parameter to a sproc, if the value is null on a required parameter, then you will see this error. SQL Server (effectively) does not differentiate between a parameter not passed, and one passed with a null value.

    You’re seeing a SQL error. This is unrelated to MVC and MVC isn’t really causing your problem. Determine if null is a valid value context["FirstName"], and if so, change your function to accept null values. If not, find out why context["FirstName"] is null.

    Also, I don’t think this line is going to add your parameter names correctly (with the “@” prefix).

    myCommand.Parameters.AddWithValue(value.Name, value.PropertyValue);

    Also, since this is MVC, make sure you have a control named FirstName on the form posting to:

    public ActionResult CreateProfile(string Username, string Password, string FirstName, string LastName)
    

    It reads fields based on the name, not the ID

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.