0

我收到一条奇怪的错误消息,上面写着

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
            }
4

3 回答 3

1

既然你说你有一个SqlException,我猜最可能的原因是 INSERT 命令失败,可能是因为重复的行 - 你插入的任何列都有 UNIQUE 约束吗?

例如,如果btnRegister单击 两次,就会发生这种情况。

我认为您在发布的答案中对 catch 块所做的更改不会产生任何影响。通常,最好使用完整的堆栈跟踪记录异常详细信息,并向最终用户显示面向用户的错误消息,而不是:

catch (Exception ex)
{
    lblErrorRegister.Text = ex.Message; //Otherwise error
}

你应该使用

catch(Exception ex)
{
    log(ex.ToString()); // call your favourite logging library
    lblErrorRegister.Text = "An error occurred"; // or something more friendly
}

日志记录ex.ToString()比仅仅ex.Message获得其他有用信息(例如堆栈跟踪)要好。

于 2013-07-27T13:21:30.813 回答
0
  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
        }

将其更改为:

  try
        {
            insertUser.ExecuteNonQuery();
            con6.Close();
            Session["Email"] = textEmailRegister.Text; //User gets logged in
            Response.Redirect("~/UserPages/Default.aspx"); //Send to startpage

        }
        catch (Exception ex)
        {
            lblErrorRegister.Text = ex.Message; //Otherwise error
        }

尽管这是一个微小的变化,但它使程序再次顺利运行。有时 .NET 真的很奇怪……

于 2013-07-27T11:26:36.143 回答
0
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$     
ConnectionStrings:jaklin11ConnectionString %>" 
SelectCommand="SELECT [Activity], [Distance], [Date], [Time], [WorkoutID] FROM [Workout] 
WHERE ([Email] = @Email) ORDER BY [Date] DESC">
<SelectParameters>
<asp:SessionParameter Name="Email" SessionField="Email" Type="String" />
</SelectParameters>
</asp:SqlDataSource>

更改了数据源的 CSS 代码,错误在于它没有条件。所以我添加了 WHERE Email = @Email

于 2013-08-28T08:24:55.413 回答