asp.net上的位置0错误没有行

时间:2013-06-02 19:58:16

标签: asp.net database

我正在编写一个基于Web的程序,这是我的身份验证页面。它工作正常,但突然间它开始出现错误。

这是我的代码:

 else if (LoginAs.SelectedValue == "Student")
        {
            string tableName = "StudentTable";
            String name = "", surname = "", email = "";
            string query = "Select level from " + tableName + " where ID='" + idBox.Text + "'";
            SqlCommand cmd = new SqlCommand(query, con);
            string level = Convert.ToString(cmd.ExecuteScalar());
            CreateUser(con, tableName, ref name, ref surname, ref email);
            query = "Select program from " + tableName + " where ID='" + idBox.Text + "'";
            cmd = new SqlCommand(query, con);
            string program = Convert.ToString(cmd.ExecuteScalar());
            MyGlobals.student = new Student(Convert.ToInt32(idBox.Text), "Active", email, name, surname, password, level, program);

            MyGlobals.currentID = idBox.Text;
            query = "Select * from RegisterTable where StudentID='" + idBox.Text + "'";

            cmd = new SqlCommand(query, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            DataTable dt = new DataTable();
            da.Fill(dt);

            foreach (DataRow dr in dt.Rows)
            {
                query = "SELECT * FROM CourseTable WHERE CourseCode='" + dr["CourseCode"] + "' AND CourseNumber='" + dr["CourseNumber"] + "' AND Term='" + dr["Term"] + "'";
                cmd = new SqlCommand(query, con);
                SqlDataAdapter da2 = new SqlDataAdapter(cmd);

                DataTable dt2 = new DataTable();
                da2.Fill(dt2);
                DataRow dr2 = dt2.Rows[0];  //ERROR COMES AT HERE

                Course course = new Course(dr2["InstructorName"].ToString(), dr2["CourseCode"].ToString(), dr2["CourseNumber"].ToString(), dr2["CourseName"].ToString(), dr2["Term"].ToString(), dr2["CRN"].ToString(), dr2["Level"].ToString(), dr2["Credit"].ToString(), dr2["Description"].ToString(), dr2["Capacity"].ToString());
                Register reg = new Register(course, MyGlobals.student);

                MyGlobals.student.addToSchedule(reg);
            }
            int num = (int)Application["OnlineUsers"];
            Response.Redirect("Student.aspx");
        }

任何人都可以帮我吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

您没有指定抛出异常的位置,但这个(我的观点)的一个非常常见的原因是您的查询不会返回任何结果(或行)。