473,883 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
+ 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.N ame
End Function

And on SQLDatasource1 I added

Protected Sub SqlDataSource1_ Updating(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.SqlD ataSourceComman dEventArgs) Handles
SqlDataSource1. Updating
e.Command.Param eters("@CrtdUse r").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.PriceArchiv e
Select
'New','D',
Price_ID,Amount ,Store_ID,BSP,A LC_ID,GN_ID,App roved,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 4088
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****@microsof t.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:TemplateFi eld>
<ItemTemplate >
<asp:Label ID="lblUser" Text="<%#
GetUserName() %>" runat="server" />
</ItemTemplate>
</asp:TemplateFie ld>
</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_RowUpd ating"
runat="server">

in your codebehind:

Protected Sub gvPrices_RowUpd ating(ByVal sender As Object, ByVal e As
GridViewUpdateE ventArgs)
e.NewValues("Cr tUser") = 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.N ame
End Function

And on SQLDatasource1 I added

Protected Sub SqlDataSource1_ Updating(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.SqlD ataSourceComman dEventArgs) Handles
SqlDataSource1. Updating
e.Command.Param eters("@CrtdUse r").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.PriceArchiv e
Select
'New','D',
Price_ID,Amount ,Store_ID,BSP,A LC_ID,GN_ID,App roved,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_RowDel eted ...
Call usp_UpdateCurre ntUser
End Sub
And see what happend

What do you think of that?

Thanks,

Ed Dror

"Jialiang Ge [MSFT]" <ji****@online. microsoft.comwr ote in message
news:Ic******** ******@TK2MSFTN GHUB02.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****@microsof t.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*****@google mail.comwrote in message
news:d4******** *************** ***********@c65 g2000hsa.google groups.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:TemplateFi eld>
<ItemTemplate >
<asp:Label ID="lblUser" Text="<%#
GetUserName() %>" runat="server" />
</ItemTemplate>
</asp:TemplateFie ld>
</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_RowUpd ating"
runat="server">

in your codebehind:

Protected Sub gvPrices_RowUpd ating(ByVal sender As Object, ByVal e As
GridViewUpdateE ventArgs)
e.NewValues("Cr tUser") = 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.N ame
End Function

And on SQLDatasource1 I added

Protected Sub SqlDataSource1_ Updating(ByVal sender As Object, ByVal e As
System.Web.UI. WebControls.Sql DataSourceComma ndEventArgs) Handles
SqlDataSource1 .Updating
e.Command.Param eters("@CrtdUse r").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.PriceArchiv e
Select
'New','D',
Price_ID,Amoun t,Store_ID,BSP, ALC_ID,GN_ID,Ap proved,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 misunderstandin g 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:CommandFie ld ShowDeleteButto n="True">
<ItemStyle HorizontalAlign ="Left" />
</asp:CommandFiel d>
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_RowDele ting(object sender, GridViewDeleteE ventArgs e)
{
int userId = int.Parse(tblUs er.DataKeys[e.RowIndex].Value.ToString ());
Status result = DAOFactory.GetS ysUserDAO().Del ete(userId); //our
delete operation
//retrieve the new data
DataTable tb = .....;
// bind to the gridview
tblUser.DataSou rce = 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****@microsof t.com.

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

Jun 27 '08 #6
Hello Ed,

Sorry for my misunderstandin g 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:CommandFie ld ShowDeleteButto n="True">
<ItemStyle HorizontalAlign ="Left" />
</asp:CommandFiel d>
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_RowDele ting(object sender, GridViewDeleteE ventArgs e)
{
int userId = int.Parse(tblUs er.DataKeys[e.RowIndex].Value.ToString ());
Status result = DAOFactory.GetS ysUserDAO().Del ete(userId); //our
delete operation
//retrieve the new data
DataTable tb = .....;
// bind to the gridview
tblUser.DataSou rce = 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****@microsof t.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(U serName 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.PriceArchiv e
Select 'New','D', Price_ID,Amount ,Store_ID,BSP,A LC_ID,GN_ID,App roved,
(
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...@andrewlau ren.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...@google mail.comwrote in message

news:d4******** *************** ***********@c65 g2000hsa.google groups.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:TemplateFi eld>
<ItemTemplate >
<asp:Label ID="lblUser" Text="<%#
GetUserName() %>" runat="server" />
</ItemTemplate>
</asp:TemplateFie ld>
</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_RowUpd ating"
runat="server">
in your codebehind:
Protected Sub gvPrices_RowUpd ating(ByVal sender As Object, ByVal e As
GridViewUpdateE ventArgs)
e.NewValues("Cr tUser") = 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.N ame
End Function
And on SQLDatasource1 I added
Protected Sub SqlDataSource1_ Updating(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.SqlD ataSourceComman dEventArgs) Handles
SqlDataSource1. Updating
e.Command.Param eters("@CrtdUse r").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.PriceArchiv e
Select
'New','D',
Price_ID,Amount ,Store_ID,BSP,A LC_ID,GN_ID,App roved,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.W ebControls.SqlD ataSourceComman dEventArgs) Handles
SqlDataSource1. Updating
e.Command.Param eters("@CrtdUse r").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.comwr ote in message
news:B8******** *********@TK2MS FTNGHUB02.phx.g bl...
Hello Ed,

Sorry for my misunderstandin g 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:CommandFie ld ShowDeleteButto n="True">
<ItemStyle HorizontalAlign ="Left" />
</asp:CommandFiel d>
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_RowDele ting(object sender, GridViewDeleteE ventArgs
e)
{
int userId =
int.Parse(tblUs er.DataKeys[e.RowIndex].Value.ToString ());
Status result = DAOFactory.GetS ysUserDAO().Del ete(userId); //our
delete operation
//retrieve the new data
DataTable tb = .....;
// bind to the gridview
tblUser.DataSou rce = 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****@microsof t.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.StoresArchi ve
Select
'New','D', Store_ID,Store_ Name,CrtdUser,G etDate()
From Deleted

To

Insert into dbo.StoresArchi ve
Select
'New','D', Store_ID,Store_ Name,'DeletedUs er',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 UpdateStoreArch ive() As Integer
Dim con As New SqlConnection(c onString)
Try
Dim updateString As String = "Update StoresArchive Set CrtdUser
= '" & User.Identity.N ame & "' Where CrtdUser='Delet edUser' "
Dim cmd As New SqlCommand(upda teString, con)
con.Open()
cmd.ExecuteNonQ uery()
con.Close()
Catch ex As Exception
ErrorMessage.Te xt = ex.Message.ToSt ring
End Try
End Function

And
Protected Sub GridView1_RowDe leted(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.Grid ViewDeletedEven tArgs) Handles
GridView1.RowDe leted
Call UpdateStoreArch ive()
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*****@google mail.comwrote in message
news:9a******** *************** ***********@e39 g2000hsf.google groups.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(U serName 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.PriceArchiv e
Select 'New','D', Price_ID,Amount ,Store_ID,BSP,A LC_ID,GN_ID,App roved,
(
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...@andrewlau ren.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...@googl email.comwrote in message

news:d4******* *************** ************@c6 5g2000hsa.googl egroups.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:TemplateFi eld>
<ItemTemplate >
<asp:Label ID="lblUser" Text="<%#
GetUserName() %>" runat="server" />
</ItemTemplate>
</asp:TemplateFie ld>
</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_RowUpd ating"
runat="server">
in your codebehind:
Protected Sub gvPrices_RowUpd ating(ByVal sender As Object, ByVal e As
GridViewUpdateE ventArgs)
e.NewValues("Cr tUser") = 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.N ame
End Function
>And on SQLDatasource1 I added
> Protected Sub SqlDataSource1_ Updating(ByVal sender As Object, ByVal e
As
System.Web.UI. WebControls.Sql DataSourceComma ndEventArgs) Handles
SqlDataSource1 .Updating
e.Command.Param eters("@CrtdUse r").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.PriceArchiv e
Select
'New','D',
Price_ID,Amoun t,Store_ID,BSP, ALC_ID,GN_ID,Ap proved,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

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

Similar topics

3
13765
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. Everyone wants to do a java confirmation box when a user clicks the delete button. Fair enough, basic user design rules state that you should always confirm a delete action. There is also a consensus that the best way to do this is a template...
7
5478
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 loop through the filtered GridView to identify the selected rows and assemble some XML to be sent to a stored proc. The problem I have is that when looping through the GridView, it doesn't
1
6398
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 that seems to solve the problem. I have tried a few things: - adding a button, setting it's command name to Delete. Problem with this is that the CommandArgument value does not get set to the SelectedIndex of the GridView. Instead it has the...
6
28186
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 RowDataBound event to set the commandargument of the delete button. The event is being fired and works fine. When I press the delete button, the RowCommand event is not firing! and neither is the RowDeleting (the button's commandName is...
3
2776
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 be fairly easy cos it's a build in feature of the gridview but alas... When the user clicks delete only one row on one table needs to be deleted thus I made a new query in my Datatable (DeleteissueQuery) that excpects an id
1
8583
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 it doesn't seem to work at all. Below is my ASP.NET page, and further below, my VB.NET method that I am trying to call: <div id="admin-faq" class="page"> <h2>Site FAQs (Frequently Asked Questions)</h2>
2
3256
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 help! I have update, add adapter calls on the objectdatasource working well, however I have an issue with the delete call. In ASP.NET 2.0 I see that invisible columns are now no longer sent across in viewstate (something about security) and...
4
1817
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"> <EditItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton> &nbsp;<asp:LinkButton ID="LinkButton2" runat="server"
0
4291
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 want to have my parameter names in those methods have to be "original_Parametername" or even if I change the OldValuesParameterFormatString to get rid of the "original" I would still have just "Parametername". I think most people user parameters...
0
9942
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11148
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10419
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7974
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7133
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5801
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5994
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4618
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4223
muto222
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.