I have a SQL like this;
select B.LoweredUserName
from USER_BAYI A, aspnet_Users B, aspnet_Membership C
where B.UserId = C.UserId
AND B.UserName = A.USERNAME
It's getting all username in my membership database.
I want to user Gridview's delete properties for delete this users from my membership database.
My delete commands in sqldatasource is;
DeleteCommand="DELETE FROM aspnet_Users
WHERE LoweredUserName = @LoweredUserName" ondeleted="SqlDataSource1_Deleted"
protected void SqlDataSource1_Deleted(object sender, SqlDataSourceStatusEventArgs e)
{
DbCommand cmd = e.Command;
string username = cmd.Parameters["@LoweredUserName"].Value.ToString();
Membership.DeleteUser(username);
}
But this doesn't even compile. Where am I doing wrong? Or can you show me a way for doing this?
Set the DataKeyNames on the GridView like this: DataKeyNames="ProviderUserKey"
and set the OnDeleteCommand to some method likie this: OnDeleteCommand="gvUser_DeleteCommand"
.
Then in the code behind implement the onDelete method like this:
protected void rgUser_DeleteCommand(object source, GridCommandEventArgs e)
{
GridDataItem gdiItem = (GridDataItem)e.Item;
try
{
string strUserKey = gdiItem.GetDataKeyValue("ProviderUserKey").ToString();
Guid guiUserKey = new Guid(strUserKey);
string strUserName = Membership.GetUser(guiUserKey).UserName.ToString();
Membership.DeleteUser(strUserName);
}
catch (Exception ex)
{
//handle exception
}
}
Just for more info, you are binding to the wrong event on the SqlDataSource. The OnDeleted
event is fired after the record is deleted, and you need the OnDeleting
event because it is fired before the record is deleted. In the OnDeleting
event you can add your custom delete code and then cancel the event, but you can't do theat in the OnDeleted
event.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.