locked
Anyone know how to insert/update records in asp.net using entity framework, stored procedures and formview? RRS feed

  • Question

  • Hello

     

    Does anyone know how to update/insert records in asp.net using entity framework, formview and sprocs? I have my sprocs created and mapped to the entity model in VS. I dont know how to connect the pieces in the code behind to fire the insert/update with the user input.

     

    here is my insert click event:

     

     

    <asp:LinkButton ID="btnCompanyInsert" runat="server" CommandName="Insert" onclick="btnCompanyInsert_Click"
           CausesValidation="true" Text="insert" ></asp:LinkButton>
    

     


    here is my code behind:

     

     

    protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
     {
      //jpEntities myEntities = new jpEntities();
      jpCompany myCompany = (jpCompany)e.Values;
    
      // State/DropDownList Logic
      DropDownList myDropDownList = (DropDownList)FormView1.FindControl("ddlStateIDInsert");
    
      myCompany.StateID = myDropDownList.SelectedIndex;
    
      // Logo/FileUpload save and validation logic
      FileUpload myFileUpload = (FileUpload)FormView1.FindControl("fuLogoInsert");
    
      if (myFileUpload.HasFile)
      {
       if (myFileUpload.FileName.ToLower().EndsWith(".jpg"))
       {
        // Logo/FileUpload save
        string virtualFolder = "~/Images/";
        string physicalFolder = Server.MapPath(virtualFolder);
        string fileName = Guid.NewGuid().ToString();
        string extension = System.IO.Path.GetExtension(myFileUpload.FileName);
    
        myFileUpload.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));
        myCompany.Logo = virtualFolder + fileName + extension;
       }
       else
       {
        // Logo/FileUpload validation
        CustomValidator myCustomValidator = (CustomValidator)FormView1.FindControl("cvLogoInsert");
        myCustomValidator.IsValid = false;
        e.Cancel = true;
       }
      }
    
      //myEntities.jpCompanies.AddObject(myCompany);
      //myEntities.SaveChanges();
     }
    

    protected void btnCompanyInsert_Click(object sender, EventArgs e)
     {  
      jpEntities myEntities = new jpEntities();
      
      jpCompany myCompany = new jpCompany();
    
      myEntities.jpCompanies.AddObject(myCompany);
    
      myEntities.SaveChanges();    
     }
    

     


    here is my insert sproc:

     

     

    USE [jp]
    GO
    /****** Object: StoredProcedure [dbo].[CompanyInsert] Script Date: 07/15/2011 08:32:28 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    -- =============================================
    -- Author:		Todd Schleuger
    -- Create date: 
    -- Description:	
    -- =============================================
    ALTER PROCEDURE [dbo].[CompanyInsert] 
    	-- Add the parameters for the stored procedure here
    	@name nvarchar(50),
    	@address1 nvarchar(50),
    	@address2 nvarchar(50),
    	@city nvarchar(50),
    	@stateid integer,
    	@zip nvarchar(9),
    	@phone nvarchar(10),
    	@website nvarchar(50),
    	@logo nvarchar(50),
    	@active bit,
    	@notes nvarchar(max)
    AS
    BEGIN
    	-- SET NOCOUNT ON added to prevent extra result sets from
    	-- interfering with SELECT statements.
    	SET NOCOUNT ON;
    
     -- Insert statements for procedure here
     	insert into jpCompanies(name,address1, address2, city, stateid, zip, phone, website, logo, active, notes)
    		 values(@name, @address1, @address2, @city, @stateID, @zip, @phone, @website, @logo, @active, @notes);
    		 
    	select SCOPE_IDENTITY() as NewCompanyId;
    END
    
    
    
    


     


    Friday, July 15, 2011 1:32 PM

Answers

  • Pretty Straightforward

     

    http://blogs.msdn.com/b/adonet/archive/2008/03/26/stored-procedure-mapping.aspx

    Friday, July 15, 2011 2:33 PM

All replies

  • Pretty Straightforward

     

    http://blogs.msdn.com/b/adonet/archive/2008/03/26/stored-procedure-mapping.aspx

    Friday, July 15, 2011 2:33 PM
  • How's it going now? Was your question solved?

    Please feel free to let us know if you need any further support.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, July 18, 2011 8:38 AM