Stored procedure or function expects parameter which is not supplied
        Posted  
        
            by 
                user2920046
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by user2920046
        
        
        
        Published on 2013-10-31T10:13:33Z
        Indexed on 
            2013/11/01
            9:54 UTC
        
        
        Read the original article
        Hit count: 234
        
I am trying to insert data into a SQL Server database by calling a stored procedure, but I am getting the error
Procedure or function 'SHOWuser' expects parameter '@userID', which was not supplied.
My stored procedure is called "SHOWuser". I have checked it thoroughly and no parameters is missing.
My code is:
public void SHOWuser(string userName, string password, string emailAddress, List preferences)
    {
        SqlConnection dbcon = new SqlConnection(conn);
        try
        { 
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = dbcon;
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "SHOWuser";
            cmd.Parameters.AddWithValue("@userName", userName);
            cmd.Parameters.AddWithValue("@password", password);
            cmd.Parameters.AddWithValue("@emailAddress", emailAddress);
            dbcon.Open();
            int i = Convert.ToInt32(cmd.ExecuteScalar());
            cmd.Parameters.Clear();
            cmd.CommandText = "tbl_pref";
            foreach (int preference in preferences)
            {
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@userID", Convert.ToInt32(i));
                cmd.Parameters.AddWithValue("@preferenceID", Convert.ToInt32(preference));
                cmd.ExecuteNonQuery();
            }
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            dbcon.Close();
        }
and the stored procedure is:
ALTER PROCEDURE [dbo].[SHOWuser] -- Add the parameters for the stored procedure here ( @userName varchar(50), @password nvarchar(50), @emailAddress nvarchar(50) ) AS BEGIN INSERT INTO tbl_user(userName,password,emailAddress) values(@userName,@password,@emailAddress) select tbl_user.userID,tbl_user.userName,tbl_user.password,tbl_user.emailAddress, stuff((select ',' + preferenceName from tbl_pref_master inner join tbl_preferences on tbl_pref_master.preferenceID = tbl_preferences.preferenceID where tbl_preferences.userID=tbl_user.userID FOR XML PATH ('')),1,1,' ' ) AS Preferences from tbl_user SELECT SCOPE_IDENTITY(); END
Pls help, Thankx in advance...
© Stack Overflow or respective owner