没有现有记录时填充数据时出错。 ASP.NET C#

时间:2012-07-25 18:11:44

标签: c# asp.net

我有两个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?

FYR:

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 个答案:

答案 0 :(得分:1)

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

int subjectVal;
if(TryParse(ddlSubjects.SelectedValue, out subjectVal)
{
    PopulateStudent(subjectVal, yearVal);
}
else
{
    //Invalid value.  Log something, throw error, whatever.
}

答案 1 :(得分:0)

PopulateSubjects()返回bool:true如果data != 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;结束。

答案 2 :(得分:0)

这将是int.TryParse或只是在尝试填充

之前检查!string.IsNullOrEmpty(ddlYearLevel.SelectedValue)

答案 3 :(得分:0)

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