5
我想在asp.net中註冊一個使用C#的用戶,然後將他們的詳細信息放到我的數據庫中。一切似乎工作正常,但細節不進入數據庫。 這是我到目前爲止有:註冊後在數據庫中輸入用戶的詳細信息
protected void CreateUser_Click(object sender, EventArgs e)
{
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
var user = new ApplicationUser() { UserName = Email.Text, Email = Email.Text };
IdentityResult result = manager.Create(user, Password.Text);
if (result.Succeeded)
{
signInManager.SignIn(user, isPersistent: false, rememberBrowser: false);
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
manager.AddToRole(user.Id, "users");
string connectionString = WebConfigurationManager.ConnectionStrings["MyDatabaseConnection"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
string sql;
sql = "INSERT INTO users (username, title, gname, sname, dob, address, suburb, state, postcode, phone, email) VALUES (@UserName, @Title1, @FirstName, @FamilyName, @DateOfBirth, @Address, @Suburb, @State, @Postcode, @Phone, @Email)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@Username", Email.Text.Trim());
cmd.Parameters.AddWithValue("@Title1", Title1.Text.Trim());
cmd.Parameters.AddWithValue("@FirstName", FirstName.Text.Trim());
cmd.Parameters.AddWithValue("@FamilyName", FamilyName.Text.Trim());
cmd.Parameters.AddWithValue("@DateOfBirth", DateOfBirth.Text.Trim());
cmd.Parameters.AddWithValue("@Address", Address.Text.Trim());
cmd.Parameters.AddWithValue("@Suburb", Suburb.Text.Trim());
cmd.Parameters.AddWithValue("@State", State.Text.Trim());
cmd.Parameters.AddWithValue("@Postcode", Postcode.Text.Trim());
cmd.Parameters.AddWithValue("@Phone", Phone.Text.Trim());
cmd.Parameters.AddWithValue("@Email", Email.Text.Trim());
using (con)
{
con.Open();
int rowCount = cmd.ExecuteNonQuery();
if (rowCount > 0)
{
lblResult.Text = "Success. New User Registered";
}
}
}
else
{
ErrorMessage.Text = result.Errors.FirstOrDefault();
}
}
,並在web.config的網頁我有該連接字符串:
<connectionStrings>
<add name="MyDatabaseConnection" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\MyDatabaseConnection.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
任何想法,我做錯了嗎?任何幫助,將不勝感激。
您是否收到任何異常? –
不,它看起來像是正常工作,但詳細信息不會出現在數據庫中 – smurph
您是否調試過代碼?如果任何條件不符合,你是否被拋出了範圍? – Prabhat