Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 23rd, 2006, 10:22 AM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default ASPNET_Membership table

I'd like to be able to create/read/update/delete the aspnet_membership table programmatically using a gridview control.

has anyone got a tutorial or some code that will demonstrate this?

thanks.



 
Old October 23rd, 2006, 01:11 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi michaelcode,

Take a look at this MSDN article:

http://msdn.microsoft.com/library/de...memroleman.asp

While I find the UI a bit messy, the code behind it features some interesting tricks and techniques.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old October 26th, 2006, 11:33 AM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,
that's above my head at the moment.
I was thinking of something like

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        If Not Page.IsPostBack Then
            BindGrid()
        End If
    End Sub

    Sub BindGrid()
        Dim sSQL As String = "SELECT UserID, Email, IsApproved, IsLockedOut FROM aspnet_Membership;"
        Dim sConnString As String = ConfigurationManager.ConnectionStrings("Connection String").ConnectionString
        Dim Connection As New SQLConnection(sConnString)

        Dim DataAdapter As New SQLDataAdapter(sSQL, Connection)
        Dim DataSet As New DataSet()

        DataAdapter.Fill(DataSet, "Members")
        GridView1.DataSource = DataSet.Tables("Members").DefaultView
        GridView1.DataBind()
    End Sub

    Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs)

        GridView1.EditIndex = e.NewEditIndex
BindGrid()
    End Sub

    Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventA rgs)

        GridView1.EditIndex = -1
BindGrid()
    End Sub

    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs)
        Dim UserID As String
        Dim Email As String
        Dim sSQL As String

        'ID = Integer.Parse(GridView1.Rows(e.RowIndex).Cells(0). Text)
        UserID = GridView1.Rows(e.RowIndex).Cells(0).ToString()
        Email = CType(GridView1.Rows(e.RowIndex).Cells(1).Controls (0), TextBox).Text


        Dim sConnString As String = ConfigurationManager.ConnectionStrings("Connection String").ConnectionString
        Dim Connection As New SQLConnection(sConnString)
        Connection.Open()
        sSQL = "UPDATE aspnet_Membership set Email=@Email WHERE USERID=@USERID"
        Dim Command As New SqlCommand(sSQL, Connection)

        Command.Parameters.Add("@USERID", Data.SqlDbType.UniqueIdentifier)
        Command.Parameters("@USERID").Value = UserID

        Command.Parameters.Add("@Email", Data.SqlDbType.NVarChar)
        Command.Parameters("@Email").Value = Email


        Command.ExecuteNonQuery()
        Connection.Close()

        GridView1.EditIndex = -1
        BindGrid()
    End Sub

    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs)
        Dim UserID As String
        Dim sSQL As String

        'ID = Integer.Parse(GridView1.Rows(e.RowIndex).Cells(0). Text)
        UserID = GridView1.Rows(e.RowIndex).Cells(0).ToString
        Dim sConnString As String = ConfigurationManager.ConnectionStrings("Connection String").ConnectionString
        Dim Connection As New SqlConnection(sConnString)
        Connection.Open()
        sSQL = "DELETE * FROM aspnet_Membership WHERE UserID=@UserID"
        Dim Command As New SQLCommand(sSQL, Connection)

        Command.Parameters.Add("@USERID", Data.SqlDbType.UniqueIdentifier)
        Command.Parameters("@USERID").Value = UserID

        Command.ExecuteNonQuery()
        Connection.Close()

        GridView1.EditIndex = -1
        BindGrid()
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id=Head1 runat="server">
    <title>Gridview</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID=GridView1 runat="server" AutoGenerateColumns="False" OnRowEditing=GridView1_RowEditing OnRowCancelingEdit=GridView1_RowCancelingEdit OnRowUpdating=GridView1_RowUpdating DataKeyNames=UserID OnRowDeleting=GridView1_RowDeleting>
            <Columns>
             <asp:BoundField DataField="UserID" HeaderText="ID" Readonly="True" />
                <asp:BoundField DataField="Email" HeaderText="Email" />
                <asp:CommandField ShowEditButton="True" ShowDeleteButton="true" />
  </Columns>

        </asp:GridView>
    </div>
    </form>
</body>
</html>

the only problem is that I get an error message
Invalid cast from 'System.String' to 'System.Guid'.

thanks,
Michael.


 
Old October 26th, 2006, 02:58 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

The ID of the user is a GUID, not a String, so you need to declare UserId as a guid and cast accordingly.

That said, I wouldn't be messing around with the aspnet* tables directly. There's a lot going on that you may not oversee. For example, deleting a user means you also need to delete associated profile and other data.

Instead, look at the Membership class and API: http://msdn2.microsoft.com/en-us/lib...embership.aspx

The Membership class has convenient methods like DeleteUser that can take care of this for you automatically. That way, you can be sure that your code keeps running on future versions of the .NET framework as well.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old October 27th, 2006, 03:30 AM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar what I'm trying to do is have checkboxfields where I can approve or lockout the member using the IsApproved and IsLockedOut fields in the aspnet_membership table.

Maybe you know a easier way to do this.

cheers.

 
Old October 27th, 2006, 05:38 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Take another look at the MSDN documentation, this time at the MembershipUser class: http://msdn2.microsoft.com/en-us/lib...rshipuser.aspx

It's all there. With a few lines of code you can approve a member. First, use GetUser (UserName) on the Membership class to get the required user as a MembershipUser. Then set the IsApproved property on the user to True or False (depending on what you want it to be) and finally call UpdateUser on the Membership class again and pass it your user object to save the changes.

<plug>
  You'll find more of this in my latest book: ASP.NET 2.0 Instant Results.
</plug>

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old October 28th, 2006, 09:20 AM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks I'll give it a blast.







Similar Threads
Thread Thread Starter Forum Replies Last Post
Populate a List Box with Table Names & Table date hewstone999 Access VBA 1 February 27th, 2008 10:10 AM
Sort Table Columns by Table Header Attribute omrieliav XSLT 4 June 7th, 2006 01:05 AM
Can a Make Table Query produce a Linked table? kronik Access 5 May 16th, 2006 06:17 AM
(oracle 8i)Alter Table <table> coalesce partition combo Oracle 3 October 13th, 2004 09:35 AM
size of table (type table is table of number) MikoMax Oracle 1 November 19th, 2003 03:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.