Importing user data from a csv file into membership database

Posted on 2012-09-10
Last Modified: 2012-09-11
I have a csv file with a few hundred records containing UserName, Password, and Email.  I would like to import this data to my membership database in ASP.NET, instead of adding each user individually using the wizard in the configuration manager.  

My test file:

My code:

'creates and loads data table
        Dim dt As New DataTable()
        Dim line As String = Nothing
        Dim i As Integer = 0

         Using sr As StreamReader = File.OpenText("P:\ASP\TestLoginInfo.txt")
                 line = sr.ReadLine()
            Do While line IsNot Nothing
                   Dim data() As String = line.Split(","c)
                If data.Length > 0 Then
                    If i = 0 Then
                        For Each item In data
                            dt.Columns.Add(New DataColumn())
                        Next item
                        i += 1
                    End If
                    Dim row As DataRow = dt.NewRow()
                    row.ItemArray = data
                End If
                line = sr.ReadLine()
        End Using

'adds users to membership table
            For Each row As DataRow In dt.Rows
                Dim UserName As String = row(0).ToString()
                Dim Password As String = row(1).ToString()
                Dim Email As String = row(2).ToString()
                If Membership.FindUsersByName(UserName) Is Nothing Then
                    Membership.CreateUser(UserName, Password, Email)
                End If


Everything seems to work fine, but when I check the aspnet_membership table, nothing has been added.  I'm also unable to log in.  

Is there something that I am missing in my code?

Any help is appriciated,

Question by:CoopIS
    LVL 19

    Expert Comment

    Did you tried to step up into the code execution and see if an exception is thrown and where?
    LVL 26

    Accepted Solution

    Hi CoopIS,
    does you membership provider require a secret question and answer (Membership.RequiresQuestionAndAnswer)?

    If it does, I suspect the Membership.CreateUser Method may be failing for that reason.


    Author Comment

    I had a try catch set up where it adds users to the table, and no error messages were returned (the status code was 0 which equals success).

     I currently don't have the membership part in my web.config file.  I was having problems with it, since I do not have sql server management studio installed right now.  

    Thank you Alan, that worked out perfectly.  I added the extra code in that link you gave me, and it added the users to the membership table.

    Thank you again,

    Coop IS

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
    Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now