在C#中检测到无法访问的代码

时间:2014-03-20 19:03:25

标签: c# asp.net linq

此代码显示无法访问的代码,这是我在这里想到的任何内容吗?我正在使用C#和ASP.NET。我运行时编码Response.Redirect(“Upload Process.aspx?QuizID =”+ id +“”);永远不会到达这里。

   if (Page.IsValid)
        {
            string number = ddlNumber.SelectedValue.ToString();
            string title = txtTitle.Text;
            string level = txtLevel.Text;
            string duration = txtDuration.Text;

            Quiz m = new Quiz
            {
                NumberOfQuestion = int.Parse(number),
                Title = title,
                Level = char.Parse(level),
                Duration = int.Parse(duration)
            };
            db.Quizs.InsertOnSubmit(m);
            db.SubmitChanges();

            string id ="";
            var q = from Quiz in db.Quizs where Quiz.Title == title select Quiz;
            foreach(var p in q)
            {
                id = p.QuizID.ToString();
            }

            for (int a = 0; 0 < int.Parse(number); a++)
            {
                Question o = new Question
                {
                    QuizID = int.Parse(id)

                };
                db.Questions.InsertOnSubmit(o);
                db.SubmitChanges();
            }
            Response.Redirect("Upload Process.aspx?QuizID=" + id + "");
        }

2 个答案:

答案 0 :(得分:4)

注意number永远不会在此循环中更改

for (int a = 0; 0 < int.Parse(number); a++)
{
    Question o = new Question
    {
        QuizID = int.Parse(id)

    };
    db.Questions.InsertOnSubmit(o);
    db.SubmitChanges();
}

如果开始循环时0 < int.Parse(number)为真,则循环继续执行时它将继续为真。

答案 1 :(得分:0)

你确定它不是

for (int a = 0; a < int.Parse(number); a++)

想一想,什么时候0将会是&gt; =比数字?