0

I am creating page for admin to view all user in the system database. I’m using gird view to retrieve all the users in membership table. The problem now is how can Admin edit, delete and update the changes made by the admin? When we want to configure the select statement, there's advance button which we can put some additional statements. The membership table in my SQL doesn’t have a primary key. How do I solve this? Much thanks.

benRollag
  • 1,219
  • 4
  • 16
  • 21
aemy
  • 57
  • 1
  • 4
  • 11
  • No primary key? Then your database is incorrectly settled up. Without primary key everything will be messed up, you will have hard time carrying out CRUD operations, low query performances, etc. Post you table structure and you will have better answers. – Yaroslav Jul 12 '12 at 06:51
  • It's got a primary key. It's that long ugly GUID field. – Theo Jul 12 '12 at 07:12

2 Answers2

0

Have a look at this tutorial, it does exactly what you ask http://www.codeproject.com/Articles/24085/Insert-Update-Delete-with-Gridview-simple-way

Ashwin Singh
  • 7,197
  • 4
  • 36
  • 55
0

That tutorial that Ashwin suggested looks way too involved for me. The direction I would take...

Store the username field in the datakey property of the gridview. And use the RowDeleting and RowUpdating events of the grid view...

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    e.Cancel = true; // cancel default action.

    // delete user myself.
    string user = e.Keys["username"].ToString(); // think that's the name of the field in database.
    Membership.DeleteUser(user);
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    e.Cancel = true; // cancel default action.

    // update user myself.

    var userToUpdate = new MembershipUser();

    // get new values with e.NewValues[] and fill out all properties of userToUpdate.

    Membership.UpdateUser(userToUpdate);
}

Calling methods from the membership object seems much easier in my opinion, and then you don't have to mess with the tables that asp.net generates which could mess something up if done incorrectly.

Theo
  • 2,609
  • 1
  • 26
  • 27