0

I've been getting the error

System.Data.SqlClient.SqlException: Procedure or function 'GetActiveComissions' expects parameter '@param1', which was not supplied.

on the code line da.Fill(dt). When I try to bind data to my GridView based on a stored procedure, clearly supplying the parameter.

Code:

ApplicationDbContext context = new ApplicationDbContext();
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
string user = User.Identity.GetUserId();

string storedprocedure = "GetActiveComissions";
SqlConnection cn = GetSqlCon.GetCon();

DataTable dt = new DataTable();

SqlCommand cmd = new SqlCommand(storedprocedure, cn); 
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@param1", user);

SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);

GridViewActiveComissions.DataSource = dt;
GridViewActiveComissions.DataBind();

Could I be passing the parameter in a wrong way?

The stored procedure:

CREATE PROCEDURE [dbo].[GetActiveComissions]
    @param1 nvarchar(128)
AS
    SELECT 
        Comissions.CreationDate, Comissions.Accepted, 
        Comissions.BicycleModel, Comissions.BicycleType, 
        Comissions.YearOfAquisition, Comissions.Details, 
        AspNetUsers.WorkshopName 
    FROM 
        Comissions 
    INNER JOIN 
        AspNetUsers ON Comissions.WorkshopId = AspNetUsers.Id 
                    AND Comissions.Accepted = 1 
                    AND Comissions.ClientId = @param1
    RETURN 0

I've tested the stored procedure separately and it does work as intended, given the parameter is provided.

1 Answer 1

2

remove '@' from parameter name, must be like this

cmd.Parameters.AddWithValue("param1", user);
Sign up to request clarification or add additional context in comments.

6 Comments

cmd.Parameters.Add("param1", user);
Comissions.ClientId, varchar or INT?
Same with that one.
It's a nvarchar(128)
check "user" for value, maybe "user" is empty or null
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.