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

Importing user data from a csv file into asp.net membership database

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:
000130,abcd123$,me@me.com
000137,abcd123$,me2@me.com


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
                    dt.Rows.Add(row)
                End If
                line = sr.ReadLine()
            Loop
        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

            Next

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,

Thanks
0
CoopIS
Asked:
CoopIS
1 Solution
 
BardobraveCommented:
Did you tried to step up into the code execution and see if an exception is thrown and where?
0
 
Alan WarrenCommented:
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.

Alan
0
 
CoopISAuthor Commented:
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
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

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