asp.net测验应用程序中受影响的行

时间:2014-03-15 12:34:38

标签: asp.net sql-server

一切正常,但按完成后结束测验以查看结果显示此消息

enter image description here

代码:

public partial class results : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ArrayList al = (ArrayList)Session["AnswerList"];

        if (al == null)
        {
            Response.Redirect("default.aspx");
        }

        resultGrid.DataSource = al;
        resultGrid.DataBind();

        // Save the results into the database.
        if (IsPostBack == false)
        {
            // Calculate score
            double questions = al.Count;
            double correct = 0.0;


            for (int i = 0; i < al.Count; i++)
            {
                Answer a = (Answer)al[i];
                if (a.Result == Answer.ResultValue.Correct)
                    correct++;
            }

            double score = (correct / questions) * 100;

            SqlDataSource userQuizDataSource = new SqlDataSource();
            userQuizDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            userQuizDataSource.InsertCommand = "INSERT INTO [UserQuiz] ([QuizID], [DateTimeComplete], [Score], [UserName]) VALUES (@QuizID, @DateTimeComplete, @Score, @UserName)";

            userQuizDataSource.InsertParameters.Add("QuizID", Session["QuizID"].ToString());
            userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now.ToString());
            userQuizDataSource.InsertParameters.Add("Score", score.ToString());
            userQuizDataSource.InsertParameters.Add("UserName", User.Identity.Name);

            int rowsAffected = userQuizDataSource.Insert();
            if (rowsAffected == 0)
            {
                // Let's just notify that the insertion didn't
                // work, but let' s continue on ...
                errorLabel.Text = "There was a problem saving your quiz results into our database.  Therefore, the results from this quiz will not be displayed on the list on the main menu.";


            }

        }


    }

    protected void resultGrid_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlDataSource1.FilterExpression = "QuestionOrder=" + resultGrid.SelectedValue;
    }

    void Page_PreInit(object sender, EventArgs e)
    {
        if (Profile.IsAnonymous == false)
        {
            if (Profile.Theme != "")
            {
                Page.Theme = Profile.Theme;
            }
        }
    }

}

public partial class results : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ArrayList al = (ArrayList)Session["AnswerList"];

        if (al == null)
        {
            Response.Redirect("default.aspx");
        }

        resultGrid.DataSource = al;
        resultGrid.DataBind();

        // Save the results into the database.
        if (IsPostBack == false)
        {
            // Calculate score
            double questions = al.Count;
            double correct = 0.0;


            for (int i = 0; i < al.Count; i++)
            {
                Answer a = (Answer)al[i];
                if (a.Result == Answer.ResultValue.Correct)
                    correct++;
            }

            double score = (correct / questions) * 100;

            SqlDataSource userQuizDataSource = new SqlDataSource();
            userQuizDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            userQuizDataSource.InsertCommand = "INSERT INTO [UserQuiz] ([QuizID], [DateTimeComplete], [Score], [UserName]) VALUES (@QuizID, @DateTimeComplete, @Score, @UserName)";

            userQuizDataSource.InsertParameters.Add("QuizID", Session["QuizID"].ToString());
            userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now.ToString());
            userQuizDataSource.InsertParameters.Add("Score", score.ToString());
            userQuizDataSource.InsertParameters.Add("UserName", User.Identity.Name);

            int rowsAffected = userQuizDataSource.Insert();
            if (rowsAffected == 0)
            {
                // Let's just notify that the insertion didn't
                // work, but let' s continue on ...
                errorLabel.Text = "There was a problem saving your quiz results into our database.  Therefore, the results from this quiz will not be displayed on the list on the main menu.";


            }

        }


    }

    protected void resultGrid_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlDataSource1.FilterExpression = "QuestionOrder=" + resultGrid.SelectedValue;
    }

    void Page_PreInit(object sender, EventArgs e)
    {
        if (Profile.IsAnonymous == false)
        {
            if (Profile.Theme != "")
            {
                Page.Theme = Profile.Theme;
            }
        }
    }

}

1 个答案:

答案 0 :(得分:1)

在此定义您的查询,看起来DateTimeComplete参数可能应该是DateTime值:

userQuizDataSource.InsertCommand = "INSERT INTO [UserQuiz] ([QuizID], [DateTimeComplete], [Score], [UserName]) VALUES (@QuizID, @DateTimeComplete, @Score, @UserName)";

但是这里将其定义为string值:

userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now.ToString());

如错误所述,它不应​​该是string,而应该是DateTime。不幸的是,您似乎正在使用SqlDataSourceParameterCollection WebForms对象,这些对象非常过时,而且非常糟糕。特别是它似乎需要为值提供string,使整个事情变得脆弱(正如您正在经历的那样)。

虽然我建议远离SqlDataSource个对象,但是当 能够通过在添加参数时指定string转换来解决此问题时SQL会理解的东西:

userQuizDataSource.InsertParameters.Add(
    "DateTimeComplete",
    DbType.DateTime,
    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)
);