1.11M Members

Create Users from SQL Table

 
0
 

I am building a new website and migrating all users off a CMS (ExpressionEngine) that uses MYSql. I have all of the users imported into a MS SQL staging table (except passwords). I am trying to create members in asp.net membership by looping through the users in the staging table. I can get the first user to be created but I get the error "Object reference not set to an instance of an object". If I run it twice, I get the message "The user "Joe" has is already in role 'members'" I am not too smart at this and I am trying to figure out why it won't loop. I am sure it is something simple I am not seeing. Any ideas would be greatly appreciated.
Here is my code:

Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.Security
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Partial Public Class gridview_updating
    Inherits System.Web.UI.Page
    Protected Sub btnCreate_Click(sender As Object, e As EventArgs)
        Dim conn As SqlConnection = Nothing
        Dim connString As String = ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString
        conn = New SqlConnection(connString)
        Dim cmd As New SqlCommand("SELECT Username, email, password FROM Staging_table", conn)
        conn.Open()
        Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
        Dim dt As New DataTable()
        dt.Load(dr)

        For i As Integer = 0 To dt.Rows.Count - 772
            Dim strUserName As String = dt.Rows(i)("UserName").ToString()
            Dim strEmail As String = dt.Rows(i)("email").ToString()
            Dim strPassword As String = dt.Rows(i)("Password").ToString()
            Create_User(strUserName, strEmail, strPassword)
            'On Error Resume Next
        Next
        conn.Close()
    End Sub

    Protected Sub Create_User(strUserName As String, strEmail As String, strPassword As String)
        Dim status As MembershipCreateStatus
        Dim user As MembershipUser = Membership.CreateUser(strUserName, strEmail, strPassword, Nothing, Nothing, False, _
         status)

        Roles.AddUserToRole(strUserName, "Members")

        Dim myObject As MembershipUser = Membership.GetUser(strEmail)
        Dim strUserId As String = myObject.ProviderUserKey.ToString()
    End Sub
End Class
 
0
 

Object reference not set to an instance of an object

On what line do you get this error?

 
0
 

Sorry - I meant to put that in.

Line 40: Dim strUserId As String = myObject.ProviderUserKey.ToString()

Thanks

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: