Click here to Skip to main content
15,891,762 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more: , +
Hi All, Any help would be greatly appreciated on this problem.
Here is my current environment:
ASP.NET 4.0
VS 2010
C#
Entity Framework
SQL 2008

Lets say I have a simple table in SQL:
SQL
CREATE TABLE [dbo].[BillingFee](
    [BillingFeeID] [int] IDENTITY(1,1) NOT NULL,
    [BeginDate] [datetime] NULL,
    [BillFee1] [float] NULL,
    [BillFee2] [float] NULL,
    [BillFee3] [float] NULL,
    [LastUpdate] [datetime] NOT NULL,
    [LastUpdateBy] [varchar](50) NOT NULL)
 ON [PRIMARY]


In my VS project, using the entity framework, I have an entity that represents the table. All update, delete & insert actions are as provided by the entity framework (no stored procs are used).
On my aspx page I have an Entity Data Source with Update enabled.
On my aspx page I have a gird view early bound to the entity data source (mentioned above). This gridview allows for updates to some of the fields.

The following fields are displayed in the grid view:
[BeginDate, BillFee1, BillFee2, BillFee3, LastUpdate, LastUpdateBy]
The following fields are updateable in the grid view:
[BillFee1, BillFee2, BillFee3]

What I would like to do:
Grab the current users credentials and time (<-- no problem) and insert them into the LastUpdateBy and LastUpdate fields respectively whenever an update action is performed.

Result:
The SQL table is updated and my griview displays the last person and time the row was updated.

I have found a few examples that come close but I am unsure if they apply to the entity framework. Any help would be appreciated.
Posted
Updated 8-Apr-11 8:49am
v2

Got it.
The gridview:
XML
<asp:GridView ID="BillingFeeGridView" runat="server" AllowPaging="True" OnRowUpdating="BillingFeeGridView_OnRowUpdating"
AllowSorting="True" DataSourceID="BillingFeeEntityDataSource" AutoGenerateColumns="False" DataKeyNames="BillingFeeID">
    <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:BoundField DataField="BeginDate" HeaderText="Begin Date"
            ReadOnly="True" SortExpression="BeginDate" DataFormatString="{0:d}" />
        <asp:BoundField DataField="LastUpdate" DataFormatString="{0:d}"
            HeaderText="Last Update" ReadOnly="True" SortExpression="LastUpdate" />
        <asp:TemplateField HeaderText="Last Update By" SortExpression="LastUpdateBy">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("LastUpdateBy") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Label ID="LastUpdateByLabel" runat="server" Text='<%# Eval("LastUpdateBy") %>'></asp:Label>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="BillingFee1" HeaderText="BillingFee1"
            SortExpression="BillingFee1" DataFormatString="{0:c}" />
        <asp:BoundField DataField="BillingFee2" HeaderText="BillingFee2"
            SortExpression="BillingFee2" DataFormatString="{0:c}" />
        <asp:BoundField DataField="BillingFee3" HeaderText="BillingFee3"
            SortExpression="BillingFee3" DataFormatString="{0:c}" />
    </Columns>
</asp:GridView>



The code behind:
C#
protected void BillingFeeGridView_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
    e.NewValues["LastUpdateBy"] = User.Identity.Name;
    e.NewValues["LastUpdate"] = DateTime.Now;
}



At least in this simple case it works perfectly. Thanks again Mika for your input.
 
Share this answer
 
v2
Comments
thatraja 8-Apr-11 23:42pm    
5! for your effort to solved your issue.
If you want to get the user information in Sql Server, you'll have to use the actual end-user credentials when logging in. If this is accomplished you can use the CURRENT_USER function as default in the column. So this comes back to the connection string and the (possibly impersonated) caller credentials.

Another way is that you add the user information into the database before the DML operation. One way to do this is to:
- add user info to a table
- execute the commands
- delete user info from the table
- commit/rollback

This way the user info table is empty all the time but in the middle of a transaction there's relevant data which you can use for example in a trigger. However, be aware that if you use this approach, this table will become a hot-spot in the database.
 
Share this answer
 
Comments
B-LO 8-Apr-11 15:41pm    
Thanks Mika let me clarify a bit. The users will not add any new rows via the datagrid. They will simply update what already exists in the grid/table (adding new rows will take place in SSIS). For each new row created, in SSIS, a dummy user name will be used like "SysUsr". So by the time the updates occur in the data grid there will be an existing value in the LastUpdateBy field. I want to update this value programtically (with current user) on the insert event (OnRowUpdating?) of the gridview. Getting the credentials in not an issue, I'm having problems updating the gridview field which in turn will update the Entity which in turn will update the table.
Wendelius 8-Apr-11 16:14pm    
Ok, I see. If I understood correctly, this is basically a 'client' side problem. Could you simply use the RowUpdated event (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowupdated.aspx) and set overwrite the desired user information to the underlying data source in there (based on the credentials you already have).
B-LO 8-Apr-11 16:40pm    
Well... I was hoping to update the value before the save happened. That way I can utilize the entity framework and not make another call to the database. I'm really close with the OnRowUpdating event and e.newvalues. I'll post if I find a way to make it work. Lastly I do appreciate your suggestions. It is helping me to think things out a bit more :).
Wendelius 8-Apr-11 16:45pm    
Thanks. You could also use RowUpdating event which would occur before the save. I think this would give you an earlier insertion point to the data. Basically the same event you will raise with OnRowUpdating.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900