• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

vb.net membersip - finding newest member

I am using vb.nets membership system in visual studio.

I would like to get the name of the newest member but im not sure how to do it as i have no idea how to access the db with the users name in!
0
runnerjp2005
Asked:
runnerjp2005
  • 3
  • 2
1 Solution
 
1303gunCommented:
Hello,

That can be done with the attached code.

Don't forget to add the server info on the connection string.

If you need any help using the code, just ask :)

Function GetNewestMemberName() As String
        Dim conn As New SqlConnection("Data Source=SERVERNAME;Initial Catalog=DBNAME;User ID=USERID;Password=PASSWORD")
        Dim comm As New SqlCommand("SELECT aspnet_Users.UserName FROM aspnet_Users INNER JOIN aspnet_Membership ON aspnet_Membership.UserId = aspnet_Users.UserId ORDER BY aspnet_Membership.CreateDate DESC", conn)
        conn.Open()
        Dim name As String = comm.ExecuteScalar
        conn.Close()
        Return name
    End Function

Open in new window

0
 
runnerjp2005Author Commented:
I Have not set up a connection for it as visual studio did this for me ?

 Dim conn As New SqlConnection("Data Source=SERVERNAME;Initial Catalog=DBNAME;User ID=USERID;Password=PASSWORD")


so what would i add here?
0
 
1303gunCommented:
Hello.

Open your web.config file and look for the "<connectionStrings>" section. Below it, there is a node that starts with something like this: <add name="LocalSqlServer" [...]. Well, copy the name of your connection string.

Now you replace this code:

Dim conn As New SqlConnection("Data Source=SERVERNAME;Initial Catalog=DBNAME;User ID=USERID;Password=PASSWORD")

With this:

Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("CONNETIONSTRINGNAME").ConnectionString)

Where you replace CONNECTIONSTRINGNAME with the one you found on the connection string. For example, if the name found on the connection string is LocalSqlServer, my code would look like this:

Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString)
0
 
1303gunCommented:
Just correcting a part of the text:

[...] Where you replace CONNECTIONSTRINGNAME with the one you found on the web.config file. [...]
0
 
runnerjp2005Author Commented:
top notch stuff!!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now