2016-04-12 60 views
0

我有一個SELECT COUNT查詢用於確定用戶是否已經被授予了成績,如果該特定用戶已經有一個成績,等級將變得不可見。然而,即使我給了那個學生一個等級,按鈕仍然會出現。當我在調試模式下啓動時,查詢的值爲空。下面是該方法的代碼:SELECT COUNT查詢不會產生我期待的結果

String connectionString = WebConfigurationManager.ConnectionStrings["UniString"].ConnectionString; 
    SqlConnection myConnection = new SqlConnection(connectionString); 

    myConnection.Open(); 
    String modOnpwayModID = "SELECT id FROM module_on_pathway WHERE module_id = '" + modDropDown.SelectedValue + "'"; 
    SqlCommand modOnpwayModIDQuerycmd = new SqlCommand(modOnpwayModID, myConnection); 
    Int32 modOnpwayModIDResult = Convert.ToInt32(modOnpwayModIDQuerycmd.ExecuteScalar().ToString()); 
    Label lb = (Label)e.Item.FindControl("user_idLabel"); 
    String userIDLabel = lb.Text.ToString(); 
    Int32 usrIDVal = Convert.ToInt32(userIDLabel); 

    String gradeSelectionQuery = "SELECT COUNT(student_module_grade.grade) FROM student_module_grade INNER JOIN classlist ON student_module_grade.classlist_id = classlist.classlist_id INNER JOIN student_assignment_grade ON student_module_grade.classlist_id = student_assignment_grade.classlist_id INNER JOIN assignments ON student_assignment_grade.assignment_id = assignments.assignment_id WHERE student_module_grade.module_on_pathway_id ='" + modOnpwayModIDResult + "'AND classlist.user_id = '" + userIDLabel + "'"; 


    SqlCommand gradeSelectionQuerycmd = new SqlCommand(gradeSelectionQuery, myConnection); 
    Int32 gradeCount = Convert.ToInt32(gradeSelectionQuerycmd.ExecuteScalar().ToString()); 

    //See if a final score has been given already- can then be changed by the admin if it needs to be changed 
    if (gradeCount == 0) 
    { 

     submitmodMark1st.Visible = true; 

     //All elements for grade submission made invisible- admin will be unable to change the grade 
     //TB.Visible = false; 
     //changedFlagVal.Text = "The grade for this module has already been changed for the selected student"; 
     //changedFlagVal.Visible = true; 
     //changedFlagVal.ForeColor = System.Drawing.Color.Red; 

    } 

    else 
    { 

     submitmodMark1st.Visible = false; 
    } 

String repeatGradeSelectionQuery = "SELECT COUNT(student_module_repeat_grades.grade) FROM student_module_repeat_grades INNER JOIN classlist ON student_module_repeat_grades.classlist_id = classlist.classlist_id INNER JOIN student_assignment_grade ON student_module_repeat_grades.classlist_id = student_assignment_grade.classlist_id INNER JOIN assignments ON student_assignment_grade.assignment_id = assignments.assignment_id WHERE student_module_repeat_grades.module_on_pathway_id ='" + modOnpwayModIDResult + "'AND classlist.user_id = '" + userIDLabel + "'"; 
    SqlCommand repeatGradeSelectionQuerycmd = new SqlCommand(repeatGradeSelectionQuery, myConnection); 
    Int32 repeatGradeCount = Convert.ToInt32(repeatGradeSelectionQuerycmd.ExecuteScalar().ToString()); 

    if (repeatGradeCount == 0) 
    { 

     submitmodMark1st.Visible = true; 

      } 

      else 
      { 

       //All elements for grade submission made invisible- admin will be unable to change the grade 
       //TB.Visible = false; 
       changedFlagVal.Text = "The grade for this module has already been changed for the selected student"; 
       changedFlagVal.Visible = true; 
       changedFlagVal.ForeColor = System.Drawing.Color.Red; 
       submitmodMark1st.Visible = false; 
      } 
+2

太多不相關的代碼;你能否只發布必要的代碼 –

+1

你真的應該考慮使用sql參數,而不是將值連接到你的SQL查詢中。 – juharr

+0

@juharr你認爲使用這些連接值可能是問題的原因嗎?在 – Spiros

回答

0

你可以轉換到這一點這樣的存儲過程在SQL-

CREATE PROC getCountStudentModule @pathwayId int, @userID int 

AS 

BEGIN 

SELECT COUNT(student_module_repeat_grades.grade) AS CntRepeatGrades 
FROM student_module_repeat_grades 
    INNER JOIN classlist 
     ON student_module_repeat_grades.classlist_id = classlist.classlist_id 
    INNER JOIN student_assignment_grade 
     ON student_module_repeat_grades.classlist_id = student_assignment_grade.classlist_id 
    INNER JOIN assignments 
     ON student_assignment_grade.assignment_id = assignments.assignment_id 
WHERE student_module_repeat_grades.module_on_pathway_id = @pathwayId 
AND classlist.user_id = @userID 

END 

GO 

然後像做這個 -

public bool CheckStatus() 
    { 
     bool status = false; 

     using (SqlConnection con = new SqlConnection(CS)) 
     { 
      SqlCommand cmd = new SqlCommand("getCountStudentModule", con); 
      cmd.CommandType = CommandType.StoredProcedure; 

      cmd.Parameters.AddWithValue("@pathwayid", modOnpwayModIDResult); 
      cmd.Parameters.AddWithValue("@userID", userIDLabel); 
      con.Open(); 
      cmd.ExecuteNonQuery(); 

      SqlDataReader rdr = cmd.ExecuteReader(); 
      if (rdr.HasRows) 
      { 
       while (rdr.Read()) 
       { 
        string active = rdr["CntRepeatGrades"].ToString(); 

        if (active == "0") 
        { 
         status = true; 

        } 

        else 
        { 
         status = false; 
        } 
       } 
      } 



      return status; 
     } 

    } 

然後你可以做這樣的檢查你的按鈕 -

bool status = CheckStatus(); 

     if (status) 
     { 

      submitmodMark1st.Visible = true; 

     } 

     else 
     { 

      //All elements for grade submission made invisible- admin will be unable to change the grade 
      //TB.Visible = false; 
      changedFlagVal.Text = "The grade for this module has already been changed for the selected student"; 
      changedFlagVal.Visible = true; 
      changedFlagVal.ForeColor = System.Drawing.Color.Red; 
      submitmodMark1st.Visible = false; 
     } 
相關問題