0

I was writing a web based program and this is my authentication page. It was working fine but suddenly it started to give that error.

Here is my code:

 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");
        }

Can anyone help me with this? Thanks in advance.

  • What you are doing here is very bad practise. I would advise you to visit following link: http://stackoverflow.com/questions/6547986/how-to-prevent-a-sql-injection-escaping-strings – Gregor Primar Jun 02 '13 at 20:20

1 Answers1

0

You don't specify where the exception is thrown but a very common reason for this (my opinion) is that your query doesn't return any results (or rows).

Hauns TM
  • 1,909
  • 2
  • 22
  • 38
  • 1
    Tip: Try to send in a hard coded command to your data base and examine the result – Hauns TM Jun 02 '13 at 20:10
  • yes, you are right, it does not return any rows. How can i do the exception handling here can you help? –  Jun 02 '13 at 20:17
  • Well, i surrounded it with try-catch block and it is working now. Thanks –  Jun 02 '13 at 20:20