LINQ DataSource Insert

Last post 03-17-2008 4:09 PM by thomasjordan. 2 replies.

Sort Posts:

  • LINQ DataSource Insert

    03-17-2008, 9:03 AM

    I have a formview which is tied up to a LINQ datasource, both of which are created in the markup (aspx) page. I am able to insert records to my database without issue.

    The problem: I would like to timestamp the records as they are written into the database.

    A basic look at what my formview and datasource looks like:

    <asp:LinqDataSource id="linqdatasource1" runat="server" ContextTypeName="datacontext1" TableName="demotable" EnableInsert="true"></asp:LinqDataSource>

    <asp:FormView ID="formview1" runat="server" DataSourceID="linqdatasource1" DataKeyName="id" DefaultMode="insert">
    <InsertTemplate>
    <asp:TextBox id="textbox1" runat="server" text='<%#Bind("field1")' />
    <asp:Button id="button1" runat="server" CommandName="Insert" Text="Insert Record" />
    </InsertTemplate>
    </asp:FormView>

    Now, assume my table (demotable) has two fields, "field1 (nvarchar(50))" and "timestamp (datetime)". On insert of a record, I'd like to get a timestamp (i.e. System.DateTime.Now()) of the time the record went in. How do I go about adding this extra information to my insert when it isn't explicitly part of the user's input from the FormView?

     

  • Re: LINQ DataSource Insert

    03-17-2008, 3:51 PM
    Answer
    • Loading...
    • kimf
    • Joined on 02-16-2008, 9:44 AM
    • Posts 15

    You can add event handler either for the ListView OnItemInserting event or OnItemCommand event. In these event handlers you could for example call a stored procedure in your database and insert the data you want to insert. You don't have to send the datatime as an inparameter to the stored procedure, just call getutcdate or getdate in sql (the last one if you want local date and time), or just send it as an inparameter...

     Example of OnItemInserting event handler:protected void CarogInventoryReportListView_ItemInsert(Object sender, ListViewInsertEventArgs e)

    {

    TextBox tbComment = e.Item.FindControl("Comment_TextBox") as TextBox; string comment = String.IsNullOrEmpty(tbComment.Text) ? null : tbComment.Text;

    CargoInventoryDataContext cid = new CargoInventoryDataContext(WebConfigurationManager.ConnectionStrings["key"].ConnectionString);cid.ManualRegistration_Insert(comment, DateTime.UtcNow);

    CarogInventoryReport_ListView.InsertItemPosition = InsertItemPosition.None;

    }

    Example of OnItemCommand event handler:

    protected void ItemCommand(object sender, ListViewCommandEventArgs e)

    {

    if (e.CommandName == "Insert")

     {

     Control c = e.Item.FindControl("_TextBox") as TextBox;

    // Call your stored procedure...

     }

    }

     

    --Kim

  • Re: LINQ DataSource Insert

    03-17-2008, 4:09 PM

    I thought that this might be the case. In my particular situation, all I am using the formview for is inserting records. In this case, I fail to see its benefit if I am going to have to manually handle each of the input fields... I could just put the controls straight on the page and skip the formview all together.

     

Page 1 of 1 (3 items)
Microsoft Communities
Page view counter