What I am trying to do
I have a stored procedure in SQL Server that takes in a first and last name as parameters and outputs an ID. The goal is to execute this stored procedure and send it to my database on save.
What I have done
I have imported my stored procedure via the model browser and made a function import. I selected a collection of scalar string because i read this ("Scalars: it means stored procedure will return single value of selected type like binary, Boolean, byte etc. )
This is the code that I am working on right now but I do not think I am on the right even after making many variations.
public ActionResult Create([Bind(Include="id,firstname,lastname")] school_client school_client)
{
using (var ctx = new invent_stuEntities())
{
var idParam = new SqlParameter {
ParameterName = "@firstname", school_client };
var idParamB = new SqlParameter {
ParameterName = "@lastname",
Value = school_client.lastname };
//Get student id based on first and last name
var studentlist = ctx.Database.SqlQuery<Assign_studentid>("exec Assign_studentid @firstname,@lastname", idParam, idParamB).ToList<Assign_studentid>();
if (ModelState.IsValid)
{
school_client.clientid = studentlist; // Send to database
// school_client.clientid = "TEST001";
db.school_client.Add(school_client);
db.SaveChanges();
return RedirectToAction("Index");
}
};
return View(housing_client);
}
The problem
I cannot get to pass the result of my stored procedure into my database like how I hardcoded it here
school_client.clientid = "TEST001";
Would like some assistance in editing my code so that my procedure can create run on insert.
Thanks
Aucun commentaire:
Enregistrer un commentaire