Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 19th, 2007, 09:32 AM
Authorized User
 
Join Date: Apr 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Migrating users¡¦ data into aspnet.mdf

Dears

NewUsers is a table (in the ASPNET.mdf) has UserName, Password, Email 3 fields.

I¡¦d like to copy all the data in NewUsers to the following tables

It should be

1. NewUsers.UserName => aspnet_users.UserName
2. NewUsers.Password => aspnet_membership. Password
3. NewUsers.Email => aspnet_membership.Email
4. and setting the newsletter <SubscriptionType> to ¡§Html¡¨ in the aspnet_Profile table

How to do it? With store procedure or need C# /VB.net code?

Thanks
 
Old April 20th, 2007, 10:23 AM
Authorized User
 
Join Date: Apr 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I did.
:)
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SqlConnection MyConnection;
            SqlCommand MyCommand;
            SqlDataReader MyReader;

            string cnnString = "your ConnectionString";
            MyConnection = new SqlConnection();
            MyConnection.ConnectionString = cnnString;

            MyCommand = new SqlCommand();
            MyCommand.CommandText = "SELECT [UserName], [password], [email] FROM [NewUser]";
            MyCommand.CommandType = CommandType.Text;
            MyCommand.Connection = MyConnection;


            MyCommand.Connection.Open();
            MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConne ction);


            while(MyReader.Read())
            {
                string theUserName = MyReader["UserName"].ToString();
                string thePassword =MyReader["password"].ToString() ;
                string theEmail = MyReader["email"].ToString();
                System.Web.Security.Membership.CreateUser(theUserN ame,thePassword,theEmail);

                ProfileCommon profile = this.Profile;
                profile = this.Profile.GetProfile(theUserName);

                profile.Preferences.Newsletter = (SubscriptionType)Enum.Parse(typeof(SubscriptionTy pe),"Html");
                profile.Save();

            } ;


            MyCommand.Dispose();
            MyConnection.Dispose();
        }
    }
 
Old April 21st, 2007, 10:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 917
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is handy code for anyone who wants to preload users. The Membership class will encrypt the passwords for you if you do it this way. Creating the Profile and setting it to Html is a nice touch.

People often need to create canned accounts for QA testers, or training accounts for user training. Anytime you create these common accounts you have to either delete them afterward, or change the passwords on them.

By the way, since you took extra effort to dispose the command and connection, I thought I'd point out a common misunderstanding in the Reader. The CommandBehavior.CloseConnection will only execute if ALL the records are actually read. If an exception were to occur, the connection would stay open. This isn't a big deal on this "one time" code, but it's nice the remember the importance of try/finally whenever you use a Reader in production code.

Eric






Similar Threads
Thread Thread Starter Forum Replies Last Post
User Accounts ASPNET.mdf pschulz ASP.NET 2.0 Basics 3 June 19th, 2008 12:41 PM
ASPNET account for ASPNET.MDF DB-Please help rsearing ASP.NET 2.0 Basics 13 November 6th, 2007 11:54 PM
Migrating Data Problem Charlie05 BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 3 March 28th, 2007 04:08 PM
localhost/aspnet access of the aspnetdb.mdf file rsearing ASP.NET 2.0 Basics 22 August 4th, 2006 02:28 PM
migrating data using FOR EACH? krstofer Classic ASP Databases 1 April 15th, 2004 03:48 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.