我对 ASP.NET 比较陌生。我正在做一个网站项目,想使用内置数据库 ASPNETDB 并链接到几个自定义数据库。问题是当我尝试实现 CreateUserWizard 工具时,将用户创建到 aspnet_users 和其他相关表中,但没有将值输入到自定义表中。下面是我在 UserCreated 事件处理程序中使用的源代码以及向导的屏幕截图。请指导我如何进行。我只需要填充表格。
public partial class Admin_TeacherRegister : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void CreateTeacherWizard_CreatedUser(object sender, EventArgs e)
{
Roles.AddUserToRole(CreateTeacherWizard.UserName, "Teacher");
WizardStep createWizard = CreateTeacherWizard.FindControl("newTeacher") as WizardStep;
MembershipUser newUser = Membership.GetUser(CreateTeacherWizard.UserName);
Guid newUserId = (Guid)newUser.ProviderUserKey;
TextBox fname = createWizard.FindControl("txt_fname") as TextBox;
TextBox lname = createWizard.FindControl("txt_lname") as TextBox;
DropDownList dept = createWizard.FindControl("deptDropDown") as DropDownList;
string connectionString = ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString"].ConnectionString;
string insertSql = "INSERT INTO UserProfiles(UserId, FirstName, LastName,DeptID) VALUES(@UserId, @Fname, @Lname, @Dept)";
using (SqlConnection myConnection = new SqlConnection(connectionString))
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand(insertSql, myConnection);
myCommand.Parameters.AddWithValue("@UserId", newUserId);
myCommand.Parameters.AddWithValue("@Fname", fname.Text.Trim());
myCommand.Parameters.AddWithValue("@Lname", lname.Text.Trim());
myCommand.Parameters.AddWithValue("@Dept", dept.SelectedValue);
myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
用户向导非常简单。除了默认字段外,它还有三个字段,FirstName、LastName 和一个 dropDownList。除了userId之外的这些字段,这是aspnet_user和我的自定义表之间的共享键AdditionalDetails需要插入