user911275
user911275

Reputation:

how to retrieve values from Session object in mvc3?

I have mvc 3 application in which in session object I'm taking all values that is needed to me as parameter to execute stored procedure.

If the userAction is update then execute stored procedure.

public ActionResult Index(string userAction) 
{ 
   if(Session["Mappings"] != null)            
        ViewData["Message"] = "Mapping web grid"; 

    if (Session["PricingSecurities"] == null) 
        Session["PricingSecurities"] = objRepository.GetPricingSecurityID(); 
    if (Session["Cusips"] == null) 
        Session["Cusips"] = objRepository.GetCUSIP(); 

    SecurityMappingModel objModel = null; 
    mappings = (List<SecurityMappingModel>)Session["Mappings"]; 

    objModel = new SecurityMappingModel(); 


    if (userAction == "Update" ) 
    { 
        //please tell me how can i take values from Session[Mappings] and pass it to stored procedure? 
        //i'm trying this code
        //foreach (var item in Session)
        //{objModel.Cusips = (List<SelectListItem>)Session["Mappings"];
        //I did function import here (import my sp in model)using EF name            
        //ExecuteMappingsp.
        //dbContext.ExecuteMappingsp(need to pass parameter);
        //} //i know wrong code but how to fix this??
        // PLEASE HELP ME TO RETRIEVE SESSION VALUES
        return RedirectToAction("Index"); 
    } 
    objModel.PricingSecirities = (List<SelectListItem>)Session["PricingSecurities"]; 
    objModel.Cusips = (List<SelectListItem>)Session["Cusips"]; 
    ViewBag.Mappings = mappings; 
    return View(objModel); 
} 

How can I take values from Session[Mappings] and pass it to stored procedure?

Upvotes: 0

Views: 1269

Answers (1)

dknaack
dknaack

Reputation: 60468

Description

Entity Framework Code First does not support stored procedure calls at the moment. The only way to do this, at the moment, is to use the SqlCommand. (System.Data.SqlClient namespace)

Sample

// ....
if (userAction == "Update")
{
    // Create SqlCommand with your stored procedure name and sql connectionstring
    SqlCommand cmd = new SqlCommand("dbo.StoredProcedureName", new SqlConnection("ConnectionString"));
    // set the command type to StoredProcedure
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    // Add Parameters
    cmd.Parameters.Add("@ParameterName", Session[Mappings]);
    // execute the stored procedure
    cmd.ExecuteNonQuery();

    return RedirectToAction("Index");
} 

More Information

Update

I can't know your Model and if it is code first, database first or shema first because you don't provide that information. But maybe this helps

foreach (var item in (List<SelectListItem>)Session["Mappings"])
{
   dbContext.ExecuteMappingsp(PassInTheParametersYouNeedFromYourItem)
} 

Upvotes: 1

Related Questions