Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 选择计数查询不生成结果I';我期待着_C#_Sql_Asp.net_Button_Count - Fatal编程技术网

C# 选择计数查询不生成结果I';我期待着

C# 选择计数查询不生成结果I';我期待着,c#,sql,asp.net,button,count,C#,Sql,Asp.net,Button,Count,我有一个SELECT COUNT查询,用于确定用户是否已经获得了分数,如果该特定用户已经获得了分数,则提交分数的按钮将不可见。然而,即使我给了那个学生一个分数,按钮仍然会出现。在调试模式下启动时,查询的值为null。以下是方法中的代码: String connectionString = WebConfigurationManager.ConnectionStrings["UniString"].ConnectionString; SqlConnection myConnecti

我有一个SELECT COUNT查询,用于确定用户是否已经获得了分数,如果该特定用户已经获得了分数,则提交分数的按钮将不可见。然而,即使我给了那个学生一个分数,按钮仍然会出现。在调试模式下启动时,查询的值为null。以下是方法中的代码:

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

您可以在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;
        }

太多不相关的代码;你能发布必要的代码吗?你真的应该考虑使用SQL参数而不是将值连接到SQL查询中。@ JuaHr你认为使用这些级联值可能是问题的原因吗?这并没有给我带来麻烦before@Spiros不,这只是一个建议。它有助于避免sql注入,并且需要确保在某些值周围加引号,而不是根据类型在其他值周围加引号。将来,当问题显然是SQL问题时,使用该标签将有所帮助。