How do I insert data into my linq datacontext using a stored procedure?

Posted on 2009-04-16
Last Modified: 2013-11-11
I have two 3.5 listview controls on seperate pages. When a user clicks a link from the first listview I am passing an int ID variable (TrustID) through the query string to populate the second listview. I want to be able to insert new data into this list view using a stored procedure within my datacontext. I have attached the stored procedure (code below) to my datacontext and declared it within the insert properties of the appropriate table within my datacontext. I keep getting an out of range datetime error upon insert so I'm not too sure if my insert stored procedure is written correctly? "TrustID" is the foreign key in the TrustAccounts table used to pull through data attached to another table.
ALTER PROCEDURE proc_InsertTrustAccount


	@TrustID int,

	@DueDate datetime,

	@FrequencyCode int,

	@ActionCode int,

	@Notes nvarchar(MAX)



	Insert into TrustAccounts (TrustID, DueDate, FrequencyCode, ActionCode, Notes)

	Values (@TrustID, @DueDate, @FrequencyCode, @ActionCode, @Notes)


Open in new window

Question by:Shepwedd
    1 Comment

    Accepted Solution

    I've actually now managed to solve my problem as I wasn't passing the date value (along with others) through my listviews ItemInserting event (code attached) thus previously my controls weren't actually data bound resulting in null values being inserted hence the error.
    protected void lvAccounts_ItemInserting(object sender, ListViewInsertEventArgs e)
            TextBox tbDueDate = (TextBox)lvAccounts.InsertItem.FindControl("DueDateInsertTextBox");
            e.Values["DueDate"] = tbDueDate.Text;
            DropDownList ddlFrequencyCode = (DropDownList)lvAccounts.InsertItem.FindControl("FrequencyInsertDDL");
            e.Values["FrequencyCode"] = ddlFrequencyCode.SelectedValue;
            DropDownList ddlActionCode = (DropDownList)lvAccounts.InsertItem.FindControl("ActionInsertDDL");
            e.Values["ActionCode"] = ddlActionCode.SelectedValue;

    Open in new window


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now