metropia
asked on
adding logging capabilities to update, insert, delete events in details view
Hello,
I am working on a web form that uses a details view.
It is functional using it without any customization.
I would like to add some logging when the user performs any crud functionality.
I would like to ask for some help, and/ or example on how to execute vb.net functions when the links on the details view are clicked.
I am working on a web form that uses a details view.
It is functional using it without any customization.
I would like to add some logging when the user performs any crud functionality.
I would like to ask for some help, and/ or example on how to execute vb.net functions when the links on the details view are clicked.
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" InsertText="Insert" UpdateText="Update" DeleteText="Delete" ButtonType="Link" />
You need to create a handler for this events on the backend code. Use on ItemCommand instead for it. Look at here
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.itemcommand(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.itemcommand(v=vs.110).aspx
ASKER
thank you Jesus.
should it look like this then:
asp
should it look like this then:
asp
OnItemCommand="DetailsViewCommandEventHandler"
OnItemInserting="dvRecipeItem_ItemInserting"
OnItemInserted="dvRecipeItem_ItemInserted"
code behind: Public Event ItemCommand As DetailsViewCommandEventHandler
Private Sub dvRecipeItem_ItemInserted(sender As Object, e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles dvRecipeItem.ItemInserted
End Sub
Private Sub dvRecipeItem_ItemInserting(sender As Object, e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles dvRecipeItem.ItemInserting
End Sub
ASKER
I tried also:
But not compiling.
Would you be able to tell me what I am missing?
OnItemCommand="dvRecipeItem_CommandEventHandler"
OnItemInserting="dvRecipeItem_ItemInserting"
OnItemInserted="dvRecipeItem_ItemInserted">
Private Sub dvRecipeItem_CommandEventHandler(source As Object, e As DetailsViewCommandEventHandler)
End Sub
Private Sub dvRecipeItem_ItemInserted(sender As Object, e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles dvRecipeItem.ItemInserted
End Sub
Private Sub dvRecipeItem_ItemInserting(sender As Object, e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles dvRecipeItem.ItemInserting
End Sub
But not compiling.
Would you be able to tell me what I am missing?
ASKER
Ok this works:
OnItemCommand="dvRecipeIte m_ItemComm andEventHa ndler"
Protected Sub dvRecipeItem_ItemCommandEv entHandler (sender As [Object], e As DetailsViewCommandEventArg s)
If e.CommandName = "Insert" Then
End If
End Sub
OnItemCommand="dvRecipeIte
Protected Sub dvRecipeItem_ItemCommandEv
If e.CommandName = "Insert" Then
End If
End Sub
That's the correct way to do it. You control all the commands on the OnItemCommand Event Handler
Select Case e,CommandName
Case "Insert"
' Your procedure for insert
Case "Delete"
'Your process for deleting
...
End Select
ALso you can do a small Procedure that do the log and call it like this
Public Sub RegisterFuction(Cmd as string, Rcd as String)
'Do the process to save the User,Date and command that execute and also the record that affect where Rcd will be the Id of the affected record and Cmd the command that was executed
End Sub
and then in the OnItemCommand handler before the Case
Dim Record as string=Your Record Viewing
RegisterFunction(e.Command Name.ToStr ing,Record )
Select Case e,CommandName
Case "Insert"
' Your procedure for insert
Case "Delete"
'Your process for deleting
...
End Select
Select Case e,CommandName
Case "Insert"
' Your procedure for insert
Case "Delete"
'Your process for deleting
...
End Select
ALso you can do a small Procedure that do the log and call it like this
Public Sub RegisterFuction(Cmd as string, Rcd as String)
'Do the process to save the User,Date and command that execute and also the record that affect where Rcd will be the Id of the affected record and Cmd the command that was executed
End Sub
and then in the OnItemCommand handler before the Case
Dim Record as string=Your Record Viewing
RegisterFunction(e.Command
Select Case e,CommandName
Case "Insert"
' Your procedure for insert
Case "Delete"
'Your process for deleting
...
End Select
ASKER
i have a question related to what i am trying to do Jesus, do you know how to access the current record on the detilas view?
I tried this:
If Not IsNothing(Me.dvRecipeItem. Rows(0).Fi ndControl( "ItemNumbe r"))
But it always shows as Nothing. I think that is not how you get the value needed.
Thank you for your help.
I tried this:
If Not IsNothing(Me.dvRecipeItem.
But it always shows as Nothing. I think that is not how you get the value needed.
Thank you for your help.
You need to add datakeynames=[id] as an attribute to your detailsview (id is whatever your primary key is). Like this more or less
and then
Dim temp as string=ItemsDetailsView.Da taKey.Valu e.ToString ()
//or
Dim ID as string= ItemsDetailsView.DataKey(0 ).ToString ()
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False" DataKeyNames="DocumentID" DataSourceID="SqlDataSource1" OnPageIndexChanged="DetailsView1_PageIndexChanged" DataMember="DefaultView">
and then
Dim temp as string=ItemsDetailsView.Da
//or
Dim ID as string= ItemsDetailsView.DataKey(0
ASKER
I have one DataKeyNames:
But I need the values of these databound fields:
Like:
DataKeyNames="Id"
But I need the values of these databound fields:
<asp:BoundField DataField="ItemNumber" HeaderText="Item Number" SortExpression="ItemNumber" ApplyFormatInEditMode="True" />
<asp:BoundField DataField="ModifiedBy" HeaderText="Modified By" SortExpression="ModifiedBy" ApplyFormatInEditMode="True" />
Do I have to add more DataKeyNames?Like:
DataKeyNames="Id"
DataKeyNames="ItemNumber"
DataKeyNames="ModifiedBy"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Jesus,
I have been trying to make this work but I am not getting the values I need.
All am a looking right now is to obtain the value from ItemNumber, and ModifiedBy which I believe are row 1, and 4, cells 1 respectively.
Can you look at my code and tell me what I am doing wrong? Perhaps I am refering the row, cell incorrectly?
I really need to get this done :/
Any help would be great.
I have been trying to make this work but I am not getting the values I need.
All am a looking right now is to obtain the value from ItemNumber, and ModifiedBy which I believe are row 1, and 4, cells 1 respectively.
Can you look at my code and tell me what I am doing wrong? Perhaps I am refering the row, cell incorrectly?
I really need to get this done :/
Any help would be great.
<Fields>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" ApplyFormatInEditMode="True" />
<asp:BoundField DataField="ItemNumber" HeaderText="Item Number" SortExpression="ItemNumber" ApplyFormatInEditMode="True" />
<asp:BoundField DataField="VersionNumber" HeaderText="VersionNumber" SortExpression="VersionNumber" ApplyFormatInEditMode="True" />
<asp:BoundField DataField="ModifiedDateTime" HeaderText="Modified Date Time" SortExpression="ModifiedDateTime" ApplyFormatInEditMode="True" />
<asp:BoundField DataField="ModifiedBy" HeaderText="Modified By" SortExpression="ModifiedBy" ApplyFormatInEditMode="True" />
Protected Sub dvRecipeItem_ItemCommandEventHandler(sender As [Object], e As DetailsViewCommandEventArgs)
If e.CommandName = "Insert" Then
If Not IsNothing(Me.dvRecipeItem.Rows(1).Cells(1).Text.ToString()) Then
' Log Insert
Dim strItemNumber As String = Me.dvRecipeItem.Rows(1).Cells(1).Text.ToString()
Dim strModifiedBy As String = Me.dvRecipeItem.Rows(4).Cells(1).Text.ToString()
Dim myAppLog As New AppLogClass(inEventCode:="Recipe-Insert", inItemCode:=strItemNumber, inDetailCode:=Membership.GetUser.UserName, inDescription:="Item Inserted By: " & strModifiedBy, inNote:=strModifiedBy)
myAppLog.Save()
End If
End If
End Sub
ASKER
Hello,
I think I understand what I have to do, in theory.
By declaring the DataKeyNames, I can use them to query the entity model and get the entire record, is this correct?
Dim strItemNumber As String = Me.dvRecipeItem.DataKey(1) .ToString( ) -- ID
Dim strModifiedBy As String = Me.dvRecipeItem.DataKey(2) .ToString( ) -- ItemNumber
I would like to ask for your help with the syntax of the code that queries the entity model to get the record using the DataKeys.
Would you be able to help me with this?
Thank you much
I think I understand what I have to do, in theory.
By declaring the DataKeyNames, I can use them to query the entity model and get the entire record, is this correct?
Dim strItemNumber As String = Me.dvRecipeItem.DataKey(1)
Dim strModifiedBy As String = Me.dvRecipeItem.DataKey(2)
If e.CommandName = "Update" Then
If Not IsNothing(Me.dvRecipeItem.DataKey(1).ToString()) Then
' Log Update
Dim strItemNumber As String = Me.dvRecipeItem.DataKey(1).ToString()
Dim strModifiedBy As String = Me.dvRecipeItem.DataKey(2).ToString()
Dim myAppLog As New AppLogClass(inEventCode:="Recipe-Update", inItemCode:=strItemNumber, inDetailCode:=Membership.GetUser.UserName, inDescription:="Item Updated By: " & strModifiedBy, inNote:=strModifiedBy)
myAppLog.Save()
End If
End If
I would like to ask for your help with the syntax of the code that queries the entity model to get the record using the DataKeys.
Would you be able to help me with this?
Thank you much
In this code that you got her
Your getting the values from different rows
Dim strItemNumber As String = Me.dvRecipeItem.Rows(1).Ce lls(1).Tex t.ToString ()
Dim strModifiedBy As String = Me.dvRecipeItem.Rows(4).Ce lls(1).Tex t.ToString ()
and I suppose that you want from the same row but different cell
Dim strItemNumber As String = Me.dvRecipeItem.Rows(1).Ce lls(1).Tex t.ToString ()
Dim strModifiedBy As String = Me.dvRecipeItem.Rows(1).Ce lls(4).Tex t.ToString ()
or no ?? Also remember that the index begins in 0 and not in 1, the first cell will have the index(0)
also as I said to you before you can get it from the displayed text like
Dim txtMessageTitle as TextBox = TryCast(detailsViewName.Fi ndControl( "txtMessag eTitle"), TextBox)
Can you post an image of your detail view right now ??
If e.CommandName = "Insert" Then
If Not IsNothing(Me.dvRecipeItem.Rows(1).Cells(1).Text.ToString()) Then
' Log Insert
Dim strItemNumber As String = Me.dvRecipeItem.Rows(1).Cells(1).Text.ToString()
Dim strModifiedBy As String = Me.dvRecipeItem.Rows(4).Cells(1).Text.ToString()
Dim myAppLog As New AppLogClass(inEventCode:="Recipe-Insert", inItemCode:=strItemNumber, inDetailCode:=Membership.GetUser.UserName, inDescription:="Item Inserted By: " & strModifiedBy, inNote:=strModifiedBy)
myAppLog.Save()
End If
End If
Your getting the values from different rows
Dim strItemNumber As String = Me.dvRecipeItem.Rows(1).Ce
Dim strModifiedBy As String = Me.dvRecipeItem.Rows(4).Ce
and I suppose that you want from the same row but different cell
Dim strItemNumber As String = Me.dvRecipeItem.Rows(1).Ce
Dim strModifiedBy As String = Me.dvRecipeItem.Rows(1).Ce
or no ?? Also remember that the index begins in 0 and not in 1, the first cell will have the index(0)
also as I said to you before you can get it from the displayed text like
Dim txtMessageTitle as TextBox = TryCast(detailsViewName.Fi
Can you post an image of your detail view right now ??
ASKER
Jesus,
This is my current code. I am not using Row, Cells anymore, I am using DataKeyNames,
I want to be able to query the entity model using the DataKey to get the record and then save it in the log table.
Thisis my idea, but it is not working so far:
The inNote is the column in the data table that will contain the entire record:
inNote:=myRecipeRecord.ToS tring()
I am getting a message:
Dim myRecipeRecord = myContext.RefineRecipe.Whe re("it.Id= " & Me.dvRecipeItem.DataKey(1) .ToString( )).First
System.InvalidOperationExc eption: Sequence contains no elements
Do you have any insight as to why this error? is my query using wrong syntax?
This is my current code. I am not using Row, Cells anymore, I am using DataKeyNames,
I want to be able to query the entity model using the DataKey to get the record and then save it in the log table.
Thisis my idea, but it is not working so far:
Dim myContext As New OLTPEntities
Dim myRecipeRecord = myContext.RefineRecipe.Where("it.Id=" & Me.dvRecipeItem.DataKey(1).ToString()).First
'Update (log 1 row in AppLog with list of new field values in Notes field, Example: “ID=1, ItemNumber=12204, etc.”)
If e.CommandName = "Update" Then
If Not IsNothing(Me.dvRecipeItem.DataKey(1).ToString()) Then
' Log Update
Dim strItemNumber As String = Me.dvRecipeItem.DataKey(1).ToString()
Dim strModifiedBy As String = Me.dvRecipeItem.DataKey(2).ToString()
Dim myAppLog As New AppLogClass(inEventCode:="Recipe-Update", inItemCode:=strItemNumber, inDetailCode:=Membership.GetUser.UserName, inDescription:="Item Updated By: " & strModifiedBy, inNote:=myRecipeRecord.ToString())
myAppLog.Save()
End If
End If
The inNote is the column in the data table that will contain the entire record:
inNote:=myRecipeRecord.ToS
I am getting a message:
Dim myRecipeRecord = myContext.RefineRecipe.Whe
System.InvalidOperationExc
Do you have any insight as to why this error? is my query using wrong syntax?
ASKER
I modified this line:
to:
and the erros went away, and I am able to write to database, but the value being passed is:
System.Data.Objects.Object Query`1[Re cipe.Refin eRecipe]
instead that the actual values.
Dim myRecipeRecord = myContext.RefineRecipe.Where("it.Id=" & Me.dvRecipeItem.DataKey(1).ToString()).First
to:
Dim myRecipeRecord = myContext.RefineRecipe.Where("it.Id=" & Me.dvRecipeItem.DataKey(0).ToString())
and the erros went away, and I am able to write to database, but the value being passed is:
System.Data.Objects.Object
instead that the actual values.
Do a break point on this line
Dim myRecipeRecord = myContext.RefineRecipe.Whe re("it.Id= " & Me.dvRecipeItem.DataKey(0) .ToString( ))
and try to Cast the values on each properties for myRecipeRecord on the debugger to see which item returns the values
Dim myRecipeRecord = myContext.RefineRecipe.Whe
and try to Cast the values on each properties for myRecipeRecord on the debugger to see which item returns the values
ASKER
I added FirstOrDefault after ToString:
Dim myRecipeRecord = myContext.RefineRecipe.Whe re("it.Id= " & Me.dvRecipeItem.DataKey(0) .ToString( )).FirstOr Default()
I can see the values in the watch window. Then I get to the part that saves the record to the log table:
inNote:=myRecipeRecord.ToS tring()
When I go to the data table and see what got saved, I see only this:
Recipe.RefineRecipe
What happened to the values? How do I save those as a single record in the column Note?
Dim myRecipeRecord = myContext.RefineRecipe.Whe
I can see the values in the watch window. Then I get to the part that saves the record to the log table:
inNote:=myRecipeRecord.ToS
When I go to the data table and see what got saved, I see only this:
Recipe.RefineRecipe
What happened to the values? How do I save those as a single record in the column Note?
On the Breakpoint, when the program pass over step by step, select myRecipeRecord from the whole
Dim myRecipeRecord = myContext.RefineRecipe.Whe re("it.Id= " & Me.dvRecipeItem.DataKey(0) .ToString( )).FirstOr Default()
and then hit Shift+F9 (quick watch) and look for the values between the properties and you will get how to cast/Trycast or diretcast your expression to get the value
Dim myRecipeRecord = myContext.RefineRecipe.Whe
and then hit Shift+F9 (quick watch) and look for the values between the properties and you will get how to cast/Trycast or diretcast your expression to get the value
ASKER
I see the Recipe.RefineRecipe is:
(New System.Linq.SystemCore_Enu merableDeb ugView(Of Recipe.RefineRecipe)(myRec ipeRecord) ).Items(0)
and then every column appears as:
(New System.Linq.SystemCore_Enu merableDeb ugView(Of Recipe.RefineRecipe)(myRec ipeRecord) ).Items(0) ._Agitator EmptySpeed
(New System.Linq.SystemCore_Enu merableDeb ugView(Of Recipe.RefineRecipe)(myRec ipeRecord) ).Items(0) ._BulkTank Temp
and so on.
Do I have to specify every one of the columns? I am sorry this is the first time I am working with entity framework and I am having trouble understanding it.
(New System.Linq.SystemCore_Enu
and then every column appears as:
(New System.Linq.SystemCore_Enu
(New System.Linq.SystemCore_Enu
and so on.
Do I have to specify every one of the columns? I am sorry this is the first time I am working with entity framework and I am having trouble understanding it.
I think that you can specify the index only like myRecipeRecord.Items(0).It em(0) or something like that
ASKER
It is not saving the record. It only saves:
Recipe.RefineRecipe
Recipe.RefineRecipe
ASKER
Open in new window
and then on the code behind:
Open in new window
but i get an error:
BC30456: 'dvRecipeItem_ItemInserted