MilesMorales MilesMorales - 1 month ago 26
C# Question

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

Answer

If you need execute a store 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 store procedure

if is necesary to use LinQ, try this

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