我收到一条奇怪的错误消息,上面写着
System.Data.dll 中发生了“System.Data.SqlClient.SqlException”类型的第一次机会异常
仍然无法找出原因,我正在构建一个带有注册和所有功能的 .NET 网站,它运行良好,直到我为 .NET 添加了两个参数registrationpart
。这是代码,希望有人能告诉我出了什么问题。我已经用谷歌搜索了答案,但无论如何似乎都无法正确解决。
public partial class UserPages_Register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnRegisterRegister_Click(object sender, EventArgs e)
{
//If passwords do not match, error
if (textPasswordRegister.Text != textPassword2Register.Text)
{
lblErrorRegister.Text = "Passwords must match.";
}
else
{
//If user has uploaded image it gets saved in folder
if (FileUpload1.HasFile)
{
string filename = Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(Server.MapPath("~/ProfileImages/") + filename);
textProfileImageRegister.Text = (("~/ProfileImages/") + filename);
}
else {
//Otherwise a standard avatar is used
textProfileImageRegister.Text = ("~/ProfileImages/Avatar.png");
//Data put in textboxes gets inserted into DB
SqlConnection con6 = new SqlConnection(ConfigurationManager.ConnectionStrings["jaklin11ConnectionString"].ConnectionString);
con6.Open();
string insCmd = "INSERT INTO [Users] (FirstName, LastName, Birthdate, Gender, Weight, Height, Email, Password, Town, AreaCode, ProfileImage) VALUES (@FirstName, @LastName, @Birthdate, @Gender, @Weight, @Height, @Email, @Password, @Town, @AreaCode, @ProfileImage)";
SqlCommand insertUser = new SqlCommand(insCmd, con6);
insertUser.Parameters.AddWithValue("@FirstName", textFirstNameRegister.Text);
insertUser.Parameters.AddWithValue("@LastName", textLastNameRegister.Text);
insertUser.Parameters.AddWithValue("@Birthdate", textBirthdateRegister.Text);
insertUser.Parameters.AddWithValue("@Gender", ddlGenderRegister.Text);
insertUser.Parameters.AddWithValue("@Weight", textWeightRegister.Text);
insertUser.Parameters.AddWithValue("@Height", textHeightRegister.Text);
insertUser.Parameters.AddWithValue("@Email", textEmailRegister.Text);
insertUser.Parameters.AddWithValue("@Password", textPasswordRegister.Text);
insertUser.Parameters.AddWithValue("@ProfileImage", textProfileImageRegister.Text);
insertUser.Parameters.AddWithValue("@Town", texttown.Text);
insertUser.Parameters.AddWithValue("@AreaCode", textareacode.Text);
try
{
insertUser.ExecuteNonQuery();
con6.Close();
Session["Email"] = textEmailRegister.Text; //User gets logged in
Response.Redirect("~/UserPages/Default.aspx"); //Send to startpage
}
catch (Exception)
{
lblErrorRegister.Text = "An error ocurred."; //Otherwise error
}