简体   繁体   中英

How to move customer information from ASP.NET database to your database?

I'm using CreateUserWizard for creating an user but asp.net automatically add user to ASP.NET database I want to add user in my database and in my customer table. I have tried these code as peer below but nothing happened

private MembershipUser _CurrentUser = null;
    private MembershipUser CurrentUser
    {
        get
        {
            if (_CurrentUser == null)
            {
                _CurrentUser = Membership.GetUser(HttpContext.Current.User.Identity.Name);
            }
            return _CurrentUser;
        }
    }


    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    {

        ProfileCommon p = (ProfileCommon)ProfileCommon.Create(CreateUserWizard1.UserName, true);

              p.FirstName = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("FirstName")).Text;
        p.LastName = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("LastName")).Text;
        p.ContactNumber = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ContactNumber")).Text;

        // Save profile - must be done since we explicitly created it
        p.Save();
    }

Please help me to figure out my problem

You need to look at your web.config file and provide a different connection string for the default membership provider. Try this, but with your connection string.

<connectionStrings>
    <remove name="LocalSqlServer"/>
    <add name="LocalSqlServer" connectionString="Data Source=myServer;Initial Catalog=myDB;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

Note that you will have to copy the all membership and role tables from the "ASP.NET database" to your database for your login etc. to continue working.

You should use the CreatedUser event and call a method to insert a new customer to your customers table.

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
    CreateCustomer(CreateUserWizard1.UserName, CreateUserWizard1.Email);
    // Your code here
}

And then implement a method to create the customer:

public void CreateCustomer(string userName, string email)
{
    const string insertCustomerCommand = "INSERT INTO Customers (userName, email) VALUES (@userName, @email)";

    var connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"];
    var sqlConnection = new SqlConnection(connectionString.ToString());
    var sqlCommand = new SqlCommand(insertCustomerCommand, sqlConnection);

    sqlCommand.Parameters.Add("@userName", SqlDbType.NVarChar).Value = userName;
    sqlCommand.Parameters.Add("@email", SqlDbType.NVarChar).Value = email;

    // Eventually wrap in a try catch statement to handle any sql exceptions.
    sqlCommand.ExecuteNonQuery();
}

Following your code, it should be something like this:

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
    var customer = new Customer();
    customer.InsertCustomer(CUSTOMER_ID,                // Provide a unique customer Id
                            TextboxFirstName.Text,      // Provider the control that holds the first name
                            TextboxLastName.Text,       // Provider the control that holds the last name
                            TextboxContactNumber.Text,  // Provider the control that holds the contact number
                            CreateUserWizard1.Email,    
                            CreateUserWizard1.UserName);
}

You must provide a valid customer Id. Do have any method in your Sql Helper class to generate this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM