CoopIS
asked on
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\Test LoginInfo. 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(User Name, 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
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\Test
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
Membership.CreateUser(User
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
Did you tried to step up into the code execution and see if an exception is thrown and where?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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