2

我有两个 DropDownLists 和一个 ListBox,即:ddlYearLevel、ddlSubjects 和 lstStudents。

ddlSubjects 和 lstStudents 依赖于 ddlYearLevel 因为我设置了这样的代码

protected void ddlYearLevel_SelectedIndexChanged(object sender, EventArgs e)
{
    PopulateSubjects(int.Parse(ddlYearLevel.SelectedItem.Value));
    PopulateStudent(int.Parse(ddlSubjects.SelectedValue),
        int.Parse(ddlYearLevel.SelectedValue));
}

如果我有所选 YearLevel 的现有科目记录,那么就没有问题。

但是,当我没有现有的主题记录时,就会出现错误,指出“输入字符串的格式不正确”。

我的问题是,如果没有记录,如何将值设置为 null?

财政年度:

private void PopulateSubjects(int yearLevel)
{           
    con1.Open();
    SqlCommand com = new SqlCommand();
    com.Connection = con1;
    com.CommandType = CommandType.Text;
    com.CommandText =
        "SELECT Subjects.SubjectID, Subjects.SubjectCode, Subjects.YearID " +
        "FROM Subjects WHERE Subjects.YearID = " + yearLevel;
    SqlDataReader data = com.ExecuteReader();

    if (data != null)
    {
        ddlSubjects.DataSource = data;
        ddlSubjects.DataValueField = "SubjectID";
        ddlSubjects.DataTextField = "SubjectCode";
        ddlSubjects.DataBind();
    }
    else
        lblAdd.Text = "No records found!";

    data.Close();
    con1.Close();
    con1.Dispose();      
}
private void PopulateYearLevel()
{
    con5.Open();
    SqlCommand com = new SqlCommand();
    com.Connection = con5;
    com.CommandType = CommandType.Text;
    com.CommandText =
        "SELECT * FROM YearLevels";
        SqlDataReader data = com.ExecuteReader();
        ddlYearLevel.DataSource = data;
        ddlYearLevel.DataValueField = "YearID";
        ddlYearLevel.DataTextField = "YearLevel";
        ddlYearLevel.DataBind();

    data.Close();
    con5.Close();
    con5.Dispose();
}
4

4 回答 4

1

您正在尝试对非 int 值使用 Int.Parse。如果没有可用的可解析值,我建议使用TryParse并修改应用程序的流程。例子:

int subjectVal;
if(TryParse(ddlSubjects.SelectedValue, out subjectVal)
{
    PopulateStudent(subjectVal, yearVal);
}
else
{
    //Invalid value.  Log something, throw error, whatever.
}
于 2012-07-25T18:16:03.983 回答
0

PopulateSubjects()返回一个 bool: if truedata != null反之亦然。然后你PopulateStudent()只能在true返回时调用。

编辑:

protected void ddlYearLevel_SelectedIndexChanged(object sender, EventArgs e)
{
    if(PopulateSubjects(int.Parse(ddlYearLevel.SelectedItem.Value)))
        PopulateStudent(int.Parse(ddlSubjects.SelectedValue), int.Parse(ddlYearLevel.SelectedValue));
}  

然后,更改private void PopulateSubjects(int yearLevel)

private bool PopulateSubjects(int yearLevel)

并修改 if/else 语句以添加 bool

bool subjectsFound;
if (data != null)
{
    ddlSubjects.DataSource = data;
    ddlSubjects.DataValueField = "SubjectID";
    ddlSubjects.DataTextField = "SubjectCode";
    ddlSubjects.DataBind();
    subjectsFound = true;
}
else
{
    lblAdd.Text = "No records found!";
    subjectsFound = false;
}

然后return subjectsFound;在最后。

于 2012-07-25T18:15:22.440 回答
0

我还可以建议您可能想查看ddlYearLevel.SelectedIndex而不是ddlYearLevel.SelectedItem. 这可能不是您想要的,但 SelectedIndex 返回当前选定项目的编号。然后,您可以使用它来确定有关所选项目的信息,而不是检查它。

于 2012-07-26T16:25:13.740 回答
0

那将是 int.TryParse 或!string.IsNullOrEmpty(ddlYearLevel.SelectedValue)在尝试填充之前简单地检查

于 2012-07-25T18:25:04.287 回答