473,473 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

GridView on Delete Method problem

Hi there,

I'm using ASP.NET 2.0 and SQL Server 2005 with VS 2005 Pro.

I have a Price page (my website require login) with GridView with the
following columns

PriceID, Amount, Approved, CrtdUser and Date
And Edit and Delete buttons

I created a function to retrive the current user

Protected Function GetUserName() As String
Return User.Identity.Name
End Function

And on SQLDatasource1 I added

Protected Sub SqlDataSource1_Updating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.SqlDataSourceCommandEven tArgs) Handles
SqlDataSource1.Updating
e.Command.Parameters("@CrtdUser").Value = GetUserName()
End Sub
I converted the CrtdUser into a template and changed the Field binding
from crtdUser to GetUserName() function

Also I created a Trigger for Update which basically insert updated records
into a log table from Inserted = NEW and deleted = Old

Everything works fine

I also created a trigger for Delete look like this
CREATE TRIGGER [dbo].[tr_Price_Delete]
ON [dbo].[Price]
AFTER DELETE
AS
BEGIN

SET NOCOUNT ON;
Insert into dbo.PriceArchive
Select
'New','D',
Price_ID,Amount,Store_ID,BSP,ALC_ID,GN_ID,Approved ,CrtdUser,GetDate()
From Deleted

END
Because the delete trigger is from deleted every times when user delete
record the log table populated with the original user name that create the
record

I also changed the label on the GridView with GetUserName() so now it show
only the current user name (overrite the acual record) but it dsen't
populate the table with current user name
The crtdUser show old values insted of new.

How do I retreived the current user on delete method whith this GridView
control?

Thanks,
Ed Dror

Jun 27 '08 #1
11 4045
Hello Ed,

I am trying to understand the logic of the Price page. Based on my
understanding, you are composing a Price table (GridView) that allows edit
and delete.
When a user edit an item in the table, the user's name (GetUserName()) will
be filled into its CrtdUser field, and the orginal CtrdUser value will be
backuped into a 'PriceArchive' DB table.
When the user click on the 'Delete' button, the CtrdUser value shows
currently wll be moved to the 'PriceArchive' table, and the original
CtrdUser value which was backuped in 'PriceArchive' DB table will be
restored to the CrtdUser field.
So your question is how to get the current user name that shows in the
Price GridView when users click on the 'Delete' button.
Is this right?

We can add the field CtrdUser into the DataKeyNames property of GridView,
and set the DeleteCommand property of the SqlDataSource as:
"DELETE FROM [PriceTable] WHERE CtrdUser = @CtrdUser"
The SqlDataSource itself knows how to handle @CtrdUser to retrieve its
current value. For more details, see the MSDN article:
http://msdn2.microsoft.com/en-us/library/z72eefad.aspx

Another approach is to remove the DeleteCommand from SqlDataSource,
register the RowDeleting event of the GridView, and in its event handler,
we can get the row item from e.RowIndex, and call the corresponding delete
command.

If you have any other concerns, or questions, feel free to let me know.

Regards,
Jialiang Ge (ji****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 27 '08 #2
Hi Ed Dror,

You're saying that you "converted the CrtdUser into a template and
changed the Field binding from crtdUser to GetUserName() function".

If I understand this correctly, you did something like this:

<asp:GridView ID="gvPrices" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblUser" Text="<%#
GetUserName() %>" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Now you expect that the value GetUserName() returns will be saved to
the database. Is my understanding correct?

The problem with this is that you really can't bind to your own
method. Only the Bind() method will automatically load from and save
fields to the DB. And you can only use it with fields in your
DataSource.

What you could do instead is handle the RowUpdating event

in the aspx file:

<asp:GridView ID="gvPrices" OnRowUpdating="gvPrices_RowUpdating"
runat="server">

in your codebehind:

Protected Sub gvPrices_RowUpdating(ByVal sender As Object, ByVal e As
GridViewUpdateEventArgs)
e.NewValues("CrtUser") = GetUserName()
End Sub

=========
Regards,
Steve
www.stkomp.com

Ed Dror wrote:
Hi there,

I'm using ASP.NET 2.0 and SQL Server 2005 with VS 2005 Pro.

I have a Price page (my website require login) with GridView with the
following columns

PriceID, Amount, Approved, CrtdUser and Date
And Edit and Delete buttons

I created a function to retrive the current user

Protected Function GetUserName() As String
Return User.Identity.Name
End Function

And on SQLDatasource1 I added

Protected Sub SqlDataSource1_Updating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.SqlDataSourceCommandEven tArgs) Handles
SqlDataSource1.Updating
e.Command.Parameters("@CrtdUser").Value = GetUserName()
End Sub
I converted the CrtdUser into a template and changed the Field binding
from crtdUser to GetUserName() function

Also I created a Trigger for Update which basically insert updated records
into a log table from Inserted = NEW and deleted = Old

Everything works fine

I also created a trigger for Delete look like this
CREATE TRIGGER [dbo].[tr_Price_Delete]
ON [dbo].[Price]
AFTER DELETE
AS
BEGIN

SET NOCOUNT ON;
Insert into dbo.PriceArchive
Select
'New','D',
Price_ID,Amount,Store_ID,BSP,ALC_ID,GN_ID,Approved ,CrtdUser,GetDate()
From Deleted

END
Because the delete trigger is from deleted every times when user delete
record the log table populated with the original user name that create the
record

I also changed the label on the GridView with GetUserName() so now it show
only the current user name (overrite the acual record) but it dsen't
populate the table with current user name
The crtdUser show old values insted of new.

How do I retreived the current user on delete method whith this GridView
control?

Thanks,
Ed Dror
Jun 27 '08 #3
Jialiang ,

when you look at the GridView all the record is the current user name so the
statement
Delete from Price where CrtdUser = @CrtdUser will update all the records and
this is somthing we don't want
Also it will not show in the PriceLog table the current user name it will
show the original user name

I'm thinking to convert the delete button to template and OnClick event call
Stored Proc that update the PriceTable before it got deleted

Or in the GridView_RowDeleted ...
Call usp_UpdateCurrentUser
End Sub
And see what happend

What do you think of that?

Thanks,

Ed Dror

"Jialiang Ge [MSFT]" <ji****@online.microsoft.comwrote in message
news:Ic**************@TK2MSFTNGHUB02.phx.gbl...
Hello Ed,

I am trying to understand the logic of the Price page. Based on my
understanding, you are composing a Price table (GridView) that allows edit
and delete.
When a user edit an item in the table, the user's name (GetUserName())
will
be filled into its CrtdUser field, and the orginal CtrdUser value will be
backuped into a 'PriceArchive' DB table.
When the user click on the 'Delete' button, the CtrdUser value shows
currently wll be moved to the 'PriceArchive' table, and the original
CtrdUser value which was backuped in 'PriceArchive' DB table will be
restored to the CrtdUser field.
So your question is how to get the current user name that shows in the
Price GridView when users click on the 'Delete' button.
Is this right?

We can add the field CtrdUser into the DataKeyNames property of GridView,
and set the DeleteCommand property of the SqlDataSource as:
"DELETE FROM [PriceTable] WHERE CtrdUser = @CtrdUser"
The SqlDataSource itself knows how to handle @CtrdUser to retrieve its
current value. For more details, see the MSDN article:
http://msdn2.microsoft.com/en-us/library/z72eefad.aspx

Another approach is to remove the DeleteCommand from SqlDataSource,
register the RowDeleting event of the GridView, and in its event handler,
we can get the row item from e.RowIndex, and call the corresponding delete
command.

If you have any other concerns, or questions, feel free to let me know.

Regards,
Jialiang Ge (ji****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

Jun 27 '08 #4
Steve,

The GridView working fine on Update method, the problem is with Delete
method
Even the GridView CrtdUser set as current user when you delete record
It show the original User Name in the PriceLog table
Because the Trigger AfterDelete uses the Select from deleted not from
inserted

Thanks,
Ed Dror

<wi*****@googlemail.comwrote in message
news:d4**********************************@c65g2000 hsa.googlegroups.com...
Hi Ed Dror,

You're saying that you "converted the CrtdUser into a template and
changed the Field binding from crtdUser to GetUserName() function".

If I understand this correctly, you did something like this:

<asp:GridView ID="gvPrices" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblUser" Text="<%#
GetUserName() %>" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Now you expect that the value GetUserName() returns will be saved to
the database. Is my understanding correct?

The problem with this is that you really can't bind to your own
method. Only the Bind() method will automatically load from and save
fields to the DB. And you can only use it with fields in your
DataSource.

What you could do instead is handle the RowUpdating event

in the aspx file:

<asp:GridView ID="gvPrices" OnRowUpdating="gvPrices_RowUpdating"
runat="server">

in your codebehind:

Protected Sub gvPrices_RowUpdating(ByVal sender As Object, ByVal e As
GridViewUpdateEventArgs)
e.NewValues("CrtUser") = GetUserName()
End Sub

=========
Regards,
Steve
www.stkomp.com

Ed Dror wrote:
>Hi there,

I'm using ASP.NET 2.0 and SQL Server 2005 with VS 2005 Pro.

I have a Price page (my website require login) with GridView with the
following columns

PriceID, Amount, Approved, CrtdUser and Date
And Edit and Delete buttons

I created a function to retrive the current user

Protected Function GetUserName() As String
Return User.Identity.Name
End Function

And on SQLDatasource1 I added

Protected Sub SqlDataSource1_Updating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.SqlDataSourceCommandEve ntArgs) Handles
SqlDataSource1.Updating
e.Command.Parameters("@CrtdUser").Value = GetUserName()
End Sub
I converted the CrtdUser into a template and changed the Field binding
from crtdUser to GetUserName() function

Also I created a Trigger for Update which basically insert updated
records
into a log table from Inserted = NEW and deleted = Old

Everything works fine

I also created a trigger for Delete look like this
CREATE TRIGGER [dbo].[tr_Price_Delete]
ON [dbo].[Price]
AFTER DELETE
AS
BEGIN

SET NOCOUNT ON;
Insert into dbo.PriceArchive
Select
'New','D',
Price_ID,Amount,Store_ID,BSP,ALC_ID,GN_ID,Approve d,CrtdUser,GetDate()
From Deleted

END
Because the delete trigger is from deleted every times when user delete
record the log table populated with the original user name that create
the
record

I also changed the label on the GridView with GetUserName() so now it
show
only the current user name (overrite the acual record) but it dsen't
populate the table with current user name
The crtdUser show old values insted of new.

How do I retreived the current user on delete method whith this GridView
control?

Thanks,
Ed Dror

Jun 27 '08 #5
Hello Ed,

Sorry for my misunderstanding of the scenario in my initial response.

I think we can use RowDeleting event, instead RowDeleted which fires after
a delected command is executed.

1. Add a command field in the gridview columns collection:
<asp:CommandField ShowDeleteButton="True">
<ItemStyle HorizontalAlign="Left" />
</asp:CommandField>
2. Register the RowDeleting event of the GridView
3. In the event handler, we get the current selected row in the gridview
with the help of e.RowIndex, retrieve the information we need from the row
then do the delete operation. In the end, we call the bind the gridview to
show the updated data. Here is an example:

protected void tblUser_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int userId = int.Parse(tblUser.DataKeys[e.RowIndex].Value.ToString());
Status result = DAOFactory.GetSysUserDAO().Delete(userId); //our
delete operation
//retrieve the new data
DataTable tb = .....;
// bind to the gridview
tblUser.DataSource = tb;
}

Hope it helps
Regards,
Jialiang Ge (ji****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================

Jun 27 '08 #6
Hello Ed,

Sorry for my misunderstanding of the scenario in my initial response.

I think we can use RowDeleting event, instead RowDeleted which fires after
a delected command is executed.

1. Add a command field in the gridview columns collection:
<asp:CommandField ShowDeleteButton="True">
<ItemStyle HorizontalAlign="Left" />
</asp:CommandField>
2. Register the RowDeleting event of the GridView
3. In the event handler, we get the current selected row in the gridview
with the help of e.RowIndex, retrieve the information we need from the row
then do the delete operation. In the end, we call the bind the gridview to
show the updated data. Here is an example:

protected void tblUser_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int userId = int.Parse(tblUser.DataKeys[e.RowIndex].Value.ToString());
Status result = DAOFactory.GetSysUserDAO().Delete(userId); //our
delete operation
//retrieve the new data
DataTable tb = .....;
// bind to the gridview
tblUser.DataSource = tb;
}

Hope it helps
Regards,
Jialiang Ge (ji****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================

Jun 27 '08 #7
Hi Ed Dror,

I'm still not 100% sure I understand your problem. You are talking
about a PriceLog table, but your trigger is defined on PriceArchive.
Is the PriceLog table for updated records?

If you need to have some kind of parameter to pass to a trigger, there
are a couple of options.

You could use a global temporary table as follows:

CREATE TABLE ##CurrentUser(UserName varchar(50));

Then, in your RowDeleting event, you can update this table to hold the
current user name. In your trigger, you will be able to say:

Insert into dbo.PriceArchive
Select 'New','D', Price_ID,Amount,Store_ID,BSP,ALC_ID,GN_ID,Approved ,
(
select top 1 UserName
from ##CurrentUser
),
GetDate()
From Deleted

You can do the same with SQL Server's context_info(), which is
probably a better option even.

But I believe writing your own stored procs to handle the auditing for
deleted records is the safest way to go.

===========
Regards,
Steve
www.stkomp.com

On Apr 16, 5:43 pm, "Ed Dror" <e...@andrewlauren.comwrote:
Steve,

The GridView working fine on Update method, the problem is with Delete
method
Even the GridView CrtdUser set as current user when you delete record
It show the original User Name in the PriceLog table
Because the Trigger AfterDelete uses the Select from deleted not from
inserted

Thanks,
Ed Dror

<wisc...@googlemail.comwrote in message

news:d4**********************************@c65g2000 hsa.googlegroups.com...
Hi Ed Dror,
You're saying that you "converted the CrtdUser into a template and
changed the Field binding from crtdUser to GetUserName() function".
If I understand this correctly, you did something like this:
<asp:GridView ID="gvPrices" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblUser" Text="<%#
GetUserName() %>" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Now you expect that the value GetUserName() returns will be saved to
the database. Is my understanding correct?
The problem with this is that you really can't bind to your own
method. Only the Bind() method will automatically load from and save
fields to the DB. And you can only use it with fields in your
DataSource.
What you could do instead is handle the RowUpdating event
in the aspx file:
<asp:GridView ID="gvPrices" OnRowUpdating="gvPrices_RowUpdating"
runat="server">
in your codebehind:
Protected Sub gvPrices_RowUpdating(ByVal sender As Object, ByVal e As
GridViewUpdateEventArgs)
e.NewValues("CrtUser") = GetUserName()
End Sub
=========
Regards,
Steve
www.stkomp.com
Ed Dror wrote:
Hi there,
I'm using ASP.NET 2.0 and SQL Server 2005 with VS 2005 Pro.
I have a Price page (my website require login) with GridView with the
following columns
PriceID, Amount, Approved, CrtdUser and Date
And Edit and Delete buttons
I created a function to retrive the current user
Protected Function GetUserName() As String
Return User.Identity.Name
End Function
And on SQLDatasource1 I added
Protected Sub SqlDataSource1_Updating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.SqlDataSourceCommandEven tArgs) Handles
SqlDataSource1.Updating
e.Command.Parameters("@CrtdUser").Value = GetUserName()
End Sub
I converted the CrtdUser into a template and changed the Field binding
from crtdUser to GetUserName() function
Also I created a Trigger for Update which basically insert updated
records
into a log table from Inserted = NEW and deleted = Old
Everything works fine
I also created a trigger for Delete look like this
CREATE TRIGGER [dbo].[tr_Price_Delete]
ON [dbo].[Price]
AFTER DELETE
AS
BEGIN
SET NOCOUNT ON;
Insert into dbo.PriceArchive
Select
'New','D',
Price_ID,Amount,Store_ID,BSP,ALC_ID,GN_ID,Approved ,CrtdUser,GetDate()
From Deleted
END
Because the delete trigger is from deleted every times when user delete
record the log table populated with the original user name that create
the
record
I also changed the label on the GridView with GetUserName() so now it
show
only the current user name (overrite the acual record) but it dsen't
populate the table with current user name
The crtdUser show old values insted of new.
How do I retreived the current user on delete method whith this GridView
control?
Thanks,
Ed Dror
Jun 27 '08 #8
Jialiang,

It seems that there is no connection between what you see on the Grid and
what you get from
SQL server

How come the code

Protected Sub SqlDataSource1_Updating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.SqlDataSourceCommandEven tArgs) Handles
SqlDataSource1.Updating
e.Command.Parameters("@CrtdUser").Value = GetUserName()
End Sub

Workd for update but not for delete

Remember I have a simple trigger After Delete that will select from deleted
into PriceLOG table
So no matter how we manipulate the DataGrid (Screen View) it always select
from Deleted

Now I though that befor delete I will call Update procedure but this will
generate an extra record

There is no way to overrite the Original UserName when you delete a raw?

Thanks,
Ed Dror
"Jialiang Ge [MSFT]" <ji****@online.microsoft.comwrote in message
news:B8*****************@TK2MSFTNGHUB02.phx.gbl...
Hello Ed,

Sorry for my misunderstanding of the scenario in my initial response.

I think we can use RowDeleting event, instead RowDeleted which fires after
a delected command is executed.

1. Add a command field in the gridview columns collection:
<asp:CommandField ShowDeleteButton="True">
<ItemStyle HorizontalAlign="Left" />
</asp:CommandField>
2. Register the RowDeleting event of the GridView
3. In the event handler, we get the current selected row in the gridview
with the help of e.RowIndex, retrieve the information we need from the row
then do the delete operation. In the end, we call the bind the gridview to
show the updated data. Here is an example:

protected void tblUser_RowDeleting(object sender, GridViewDeleteEventArgs
e)
{
int userId =
int.Parse(tblUser.DataKeys[e.RowIndex].Value.ToString());
Status result = DAOFactory.GetSysUserDAO().Delete(userId); //our
delete operation
//retrieve the new data
DataTable tb = .....;
// bind to the gridview
tblUser.DataSource = tb;
}

Hope it helps
Regards,
Jialiang Ge (ji****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no
rights.
=================================================

Jun 27 '08 #9
Steve,
I fix it
I changed the trigger After Deleted from
Insert into dbo.StoresArchive
Select
'New','D', Store_ID,Store_Name,CrtdUser,GetDate()
From Deleted

To

Insert into dbo.StoresArchive
Select
'New','D', Store_ID,Store_Name,'DeletedUser',GetDate()
From Deleted

So I Overrite the default name of the CrtdUser to whatever name I want

Now on the ASP.NET VB page I wrote function

Public Function UpdateStoreArchive() As Integer
Dim con As New SqlConnection(conString)
Try
Dim updateString As String = "Update StoresArchive Set CrtdUser
= '" & User.Identity.Name & "' Where CrtdUser='DeletedUser' "
Dim cmd As New SqlCommand(updateString, con)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Catch ex As Exception
ErrorMessage.Text = ex.Message.ToString
End Try
End Function

And
Protected Sub GridView1_RowDeleted(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewDeletedEventArgs ) Handles
GridView1.RowDeleted
Call UpdateStoreArchive()
End Sub

So every time you delete a record the trigger will insert into Archive table
from deleted but will force to change the name column to a dummy name
Then on raw_deleted you call function that will update the raw with the
current user name on dummy name that you just created.

Thanks,
Ed Dror

<wi*****@googlemail.comwrote in message
news:9a**********************************@e39g2000 hsf.googlegroups.com...
Hi Ed Dror,

I'm still not 100% sure I understand your problem. You are talking
about a PriceLog table, but your trigger is defined on PriceArchive.
Is the PriceLog table for updated records?

If you need to have some kind of parameter to pass to a trigger, there
are a couple of options.

You could use a global temporary table as follows:

CREATE TABLE ##CurrentUser(UserName varchar(50));

Then, in your RowDeleting event, you can update this table to hold the
current user name. In your trigger, you will be able to say:

Insert into dbo.PriceArchive
Select 'New','D', Price_ID,Amount,Store_ID,BSP,ALC_ID,GN_ID,Approved ,
(
select top 1 UserName
from ##CurrentUser
),
GetDate()
From Deleted

You can do the same with SQL Server's context_info(), which is
probably a better option even.

But I believe writing your own stored procs to handle the auditing for
deleted records is the safest way to go.

===========
Regards,
Steve
www.stkomp.com

On Apr 16, 5:43 pm, "Ed Dror" <e...@andrewlauren.comwrote:
>Steve,

The GridView working fine on Update method, the problem is with Delete
method
Even the GridView CrtdUser set as current user when you delete record
It show the original User Name in the PriceLog table
Because the Trigger AfterDelete uses the Select from deleted not from
inserted

Thanks,
Ed Dror

<wisc...@googlemail.comwrote in message

news:d4**********************************@c65g200 0hsa.googlegroups.com...
Hi Ed Dror,
You're saying that you "converted the CrtdUser into a template and
changed the Field binding from crtdUser to GetUserName() function".
If I understand this correctly, you did something like this:
<asp:GridView ID="gvPrices" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblUser" Text="<%#
GetUserName() %>" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Now you expect that the value GetUserName() returns will be saved to
the database. Is my understanding correct?
The problem with this is that you really can't bind to your own
method. Only the Bind() method will automatically load from and save
fields to the DB. And you can only use it with fields in your
DataSource.
What you could do instead is handle the RowUpdating event
in the aspx file:
<asp:GridView ID="gvPrices" OnRowUpdating="gvPrices_RowUpdating"
runat="server">
in your codebehind:
Protected Sub gvPrices_RowUpdating(ByVal sender As Object, ByVal e As
GridViewUpdateEventArgs)
e.NewValues("CrtUser") = GetUserName()
End Sub
=========
Regards,
Steve
www.stkomp.com
Ed Dror wrote:
Hi there,
>I'm using ASP.NET 2.0 and SQL Server 2005 with VS 2005 Pro.
>I have a Price page (my website require login) with GridView with the
following columns
>PriceID, Amount, Approved, CrtdUser and Date
And Edit and Delete buttons
>I created a function to retrive the current user
>Protected Function GetUserName() As String
Return User.Identity.Name
End Function
>And on SQLDatasource1 I added
> Protected Sub SqlDataSource1_Updating(ByVal sender As Object, ByVal e
As
System.Web.UI.WebControls.SqlDataSourceCommandEve ntArgs) Handles
SqlDataSource1.Updating
e.Command.Parameters("@CrtdUser").Value = GetUserName()
End Sub
>I converted the CrtdUser into a template and changed the Field binding
from crtdUser to GetUserName() function
>Also I created a Trigger for Update which basically insert updated
records
into a log table from Inserted = NEW and deleted = Old
>Everything works fine
>I also created a trigger for Delete look like this
CREATE TRIGGER [dbo].[tr_Price_Delete]
ON [dbo].[Price]
AFTER DELETE
AS
BEGIN
> SET NOCOUNT ON;
Insert into dbo.PriceArchive
Select
'New','D',
Price_ID,Amount,Store_ID,BSP,ALC_ID,GN_ID,Approve d,CrtdUser,GetDate()
From Deleted
>END
>Because the delete trigger is from deleted every times when user
delete
record the log table populated with the original user name that create
the
record
>I also changed the label on the GridView with GetUserName() so now it
show
only the current user name (overrite the acual record) but it dsen't
populate the table with current user name
The crtdUser show old values insted of new.
>How do I retreived the current user on delete method whith this
GridView
control?
>Thanks,
Ed Dror

Jun 27 '08 #10
Jialiang,

I fix it
I changed the trigger After Deleted

From

Insert into dbo.StoresArchive
Select
'New','D', Store_ID,Store_Name,CrtdUser,GetDate()
From Deleted

To

Insert into dbo.StoresArchive
Select
'New','D', Store_ID,Store_Name,'DeletedUser',GetDate()
From Deleted

So I Overrite the default name of the CrtdUser to whatever name I want

Now on the ASP.NET VB page I wrote function

Public Function UpdateStoreArchive() As Integer
Dim con As New SqlConnection(conString)
Try
Dim updateString As String = "Update StoresArchive Set CrtdUser
= '" & User.Identity.Name & "' Where CrtdUser='DeletedUser' "
Dim cmd As New SqlCommand(updateString, con)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Catch ex As Exception
ErrorMessage.Text = ex.Message.ToString
End Try
End Function

And (On Grid view event)

Protected Sub GridView1_RowDeleted(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewDeletedEventArgs ) Handles
GridView1.RowDeleted
Call UpdateStoreArchive()
End Sub

So every time you delete a record the trigger will insert into Archive table
from deleted but will force to change the name column to a dummy name
Then on raw_deleted you call function that will update the raw with the
current user name on dummy name that you just created.

Thanks,
Ed Dror
"Jialiang Ge [MSFT]" <ji****@online.microsoft.comwrote in message
news:QU****************@TK2MSFTNGHUB02.phx.gbl...
Hello Ed,

Sorry for my misunderstanding of the scenario in my initial response.

I think we can use RowDeleting event, instead RowDeleted which fires after
a delected command is executed.

1. Add a command field in the gridview columns collection:
<asp:CommandField ShowDeleteButton="True">
<ItemStyle HorizontalAlign="Left" />
</asp:CommandField>
2. Register the RowDeleting event of the GridView
3. In the event handler, we get the current selected row in the gridview
with the help of e.RowIndex, retrieve the information we need from the row
then do the delete operation. In the end, we call the bind the gridview to
show the updated data. Here is an example:

protected void tblUser_RowDeleting(object sender, GridViewDeleteEventArgs
e)
{
int userId =
int.Parse(tblUser.DataKeys[e.RowIndex].Value.ToString());
Status result = DAOFactory.GetSysUserDAO().Delete(userId); //our
delete operation
//retrieve the new data
DataTable tb = .....;
// bind to the gridview
tblUser.DataSource = tb;
}

Hope it helps
Regards,
Jialiang Ge (ji****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no
rights.
=================================================

Jun 27 '08 #11
Thank you, Ed, for sharing the resolution with us.

Have a good day!

Jialiang Ge (ji****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================

Jun 27 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
7
by: Ken | last post by:
Hi All - I have a filtered GridView. This GridView has a check box in the first column. This check box is used to identify specific rows for delete operations. On the button click event I...
1
by: JasonK | last post by:
I would like to move the Delete button such that it displays one time in the footer row, rather than on every row. I've seen lots of questions asked on the subject around the net, but no answer...
6
by: Kevin Attard | last post by:
I am using a GridView inside a UserControl which has a template column for deleting the rows. Before databinding the gridview i am attaching the RowCommand and RowDataBound event. I am using the...
3
by: tarscher | last post by:
Hi all, I have a grid that contains 7 columns from 3 tables (3 unique keys, 4 normal fields). I show this 7 columns on the gridview. I now want to add edit and delete functionality. This should...
1
by: Barry L. Camp | last post by:
Hi all, Wondering if someone can help with a nagging problem I am having using a GridView and an ObjectDataSource. I have a simple situation where I am trying to delete a row from a table, but...
2
by: rgparkins | last post by:
So, I've bitten the bullet and am converting some of my asp.net 1.1 sites to asp.net 2.0, now after many issues I have come to a stop with the objectdatasource and gridviews and maybe someone can...
4
by: Jonathan Wood | last post by:
Greetings, I have a GridView control. For a couple of reasons, I ended up with a TemplateField to contain my Edit and Delete links, something like this: <asp:TemplateField ShowHeader="False">...
0
by: steve | last post by:
I have been fighting with trying to update a GridView for a while. I don't want to use the "built-in" way to do it because I am using business layer methods for updating and deleteing and I don't...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.