BrooklynSon
BrooklynSon

Reputation: 2103

DataContext.ExecuteQuery parameter issues

I get the following error:

Procedure or function 'procTestReport' expects parameter '@StartDate', which was not supplied.

When I execute the following code:

String[] args = new String[2]{StartDate.ToShortDateString(), EndDate.ToShortDateString()};

lst = dbContext.ExecuteQuery<Summary>("procTestReport", args).ToList<Summary>();

Are the arguments I pass supposed to be presented in a different way? From the following link it seems like I'm using it correctly: https://msdn.microsoft.com/en-us/library/bb361109.aspx

Upvotes: 0

Views: 3345

Answers (2)

RaZoDiuM
RaZoDiuM

Reputation: 223

If you need to execute a stored procedure you can use instead:

SqlConnection con = new SqlConnection("your ConnectionString");

SqlCommand cmd = new SqlCommand("your SP_Name", con);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new SqlParameter("param name", "param value"));
cmd.Parameters.Add(new SqlParameter("param2 name", "param2 value"));
.
.
.
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
con.Close();

SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);

return dt;

dt contains the return of the stored procedure.

If is necessary to use LinQ, try this

dataContext.ExecuteCommand("EXEC usp_SomeProcedure {0}, {1}, {2}", param1, param2, param3);

Upvotes: 0

Tom Regan
Tom Regan

Reputation: 3851

You need to declare the parameters in your sql statement. For example:

lst = dbContext.ExecuteQuery<Summary>("EXEC dbo.procTestReport @StartDate={0}, @EndDate={1}", args).ToList<Summary>();

Upvotes: 5

Related Questions