我正在使用CreateUserWizard创建用户,但asp.net会自动将用户添加到ASP.NET数据库 我想在我的数据库和客户表中添加用户。 我已经尝试过这些代码作为同行,但没有发生任何事情
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();
}
请帮我弄清楚我的问题
答案 0 :(得分:0)
您需要查看您的web.config文件,并为默认成员资格提供程序提供不同的连接字符串。试试这个,但是你的连接字符串。
<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString="Data Source=myServer;Initial Catalog=myDB;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
请注意,您必须将所有成员资格和角色表从“ASP.NET数据库”复制到您的数据库以便登录等继续工作。
答案 1 :(得分:0)
您应该使用CreatedUser事件并调用方法将新客户插入到customers表中。
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
CreateCustomer(CreateUserWizard1.UserName, CreateUserWizard1.Email);
// Your code here
}
然后实施一种创建客户的方法:
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();
}
按照您的代码,它应该是这样的:
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);
}
您必须提供有效的客户ID。你的Sql Helper类中有任何方法可以生成它吗?