Last post Aug 29, 2014 11:43 PM by pharmkitteh
Aug 27, 2014 09:53 PM|pharmkitteh|LINK
I have been trying for days to figure out how to bulk load users into the ASP.NET security tables. After googling and experimenting I have the code below. I've been testing it to get out the bugs but now that no errors display, I don't see any new
rows in the users or membership tables. Can anyone tell me where I'm going wrong? I am a newbie at .NET coding.
Here is the code I have so far in the code behind:
Partial Class Management_BulkInsertUsersToSecurity
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Dim conn As New SqlConnection
Dim cmd As New SqlCommand
conn.ConnectionString = "Data Source=pediatrics02\sea12;Initial Catalog=Quant;User ID=FakeBigUser;Password=FakePW!"
cmd.Connection = conn
cmd.CommandText = "Select * FROM physician2"
Dim dr As SqlDataReader = cmd.ExecuteReader()
If dr.HasRows Then
'Dim PhysicianID As Guid.NewGUID()
'Dim PhysicianID As Guid.NewGUID().ToString()
'Dim PhysicianID As New Guid(dr("PhysicianID").ToString())
Dim email As String = dr("email").ToString()
Dim password As String = dr("userpassword").ToString()
' Dim applicationID As String = dr('4344BA01-282B-4091-9206-CA6A8A9BCC16').ToString
Dim status As System.Web.Security.MembershipCreateStatus
Dim aGUID As String = dr("aGUID").ToString()
System.Web.Security.Membership.CreateUser(email, password, email, Nothing, Nothing, True, _
Catch ex As Exception
Thanks in advance for your help!
Here is what I have for a simple form to test:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="BulkInsertUsersToSecurity.aspx.vb" Inherits="Management_BulkInsertUsersToSecurity" %>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Bulk Load New Users (physicians)!" OnClick="Button1_Click" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:QuantumConnectionString3 %>" SelectCommand="SELECT * FROM [Physician2] WHERE ([Email] IS NOT NULL)"></asp:SqlDataSource>
Aug 29, 2014 05:29 AM|George Hua - MSFT|LINK
Thanks for posting in ASP.NET forum.
According to your description, you want to add users in to Membership.
First, have you debugged code to check whether dr.HasRows is true or not?
Membership.CreateUser Method (String, String, String, String, String, Boolean, Object, MembershipCreateStatus) will return a status parameter indicating that the user was successfully created or the reason the user creation failed. You could check the result.
Third, please note while using this method:
CreateUser adds a new user to the data store and returns a
MembershipUser object for the newly created user. If the user creation fails, you can retrieve a
MembershipCreateStatus value from the status output parameter that indicates why user creation failed. You can specify a unique identifier for the user, such as a primary key value for a database, by using the
CreateUser method will return null if
password is an empty string or
null, username is an empty string or
null or contains a comma (,),
passwordQuestion is not null and contains an empty string, or
passwordAnswer is not null and contains an empty string.
Once a membership user has been created and you have a reference to a
MembershipUser object for that user, you can modify the settings for that user with the
MembershipUser public methods and by setting the property values of the
MembershipUser object and then passing the
MembershipUser object to the
If a user already exists in the data source for the application, you can obtain a
MembershipUser object for the existing user with the
Aug 29, 2014 11:43 PM|pharmkitteh|LINK
Thank you George for your detailed answer. This is very useful.