1

我正在尝试使我已经在表单视图的编辑页面中创建的下拉列表从先前从当前用户的 sql 数据库中选择的值开始。

到目前为止,我有填充下拉列表的代码正常工作:

protected void ddlSelect_Init(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ReinstatementCS"].ConnectionString);
    SqlCommand myCommand = new SqlCommand("SELECT Prefix, Number, ClassSection, Location, StartTime, EndTime, ClassDay, Prefix + Number  + ', Section: ' + CAST(ClassSection AS VarChar) +  ', Location: ' + Location + ', Start Time: ' + StartTime + ', End Time: ' + EndTime + ', Days: ' + ClassDay + ', Credits: ' + CAST(ClassCredit AS VarChar) AS PN, Capacity, GPAReqAbove1, GPAReqBelow1, CreditReqAbove30, CreditReqBelow30, ClassCredit, IsTransfer, SLN FROM Classes");
    myCommand.Connection = con;
    SqlDataAdapter da = new SqlDataAdapter(myCommand);
    DataTable dt = new DataTable();
    da.Fill(dt);

    DropDownList ddlSelect = FVStudentClass.FindControl("ddlSelect") as DropDownList;
    ddlSelect.DataSource = dt;
    ddlSelect.DataTextField = "PN";
    ddlSelect.DataValueField = "SLN";
    ddlSelect.DataBind();

    con.Close();
}

其中 SLN 是下拉列表中每个项目的唯一值,PN 是下拉列表中每个项目的背景信息。我希望突出显示的项目是与该特定用户已存储在数据库中的内容相对应的 PN。问题是,当我尝试选择该值时,我正在使用:

protected void FVStudentClass_ModeChanging(object sender, FormViewModeEventArgs e)
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ReinstatementCS"].ConnectionString);
    SqlCommand myCommand = new SqlCommand("SELECT Prefix + Number  + ', Section: ' + CAST(ClassSection AS VarChar) +  ', Location: ' + Location + ', Start Time: ' + StartTime + ', End Time: ' + EndTime + ', Days: ' + ClassDay + ', Credits: ' + CAST(ClassCredit AS VarChar) AS PN, SLN FROM Classes JOIN StudentClass on SLN = SCClass WHERE SCWSUID = " + Request.QueryString["ALWSUID"]);
    myCommand.Connection = con;
    SqlDataAdapter da = new SqlDataAdapter(myCommand);
    DataTable dt = new DataTable();
    da.Fill(dt);

    DropDownList ddlSelect = FVStudentClass.FindControl("ddlSelect") as DropDownList;
    ddlSelect.DataSource = dt;

    ddlSelect.Items.FindByText(dt.Rows[0]["PN"].ToString());
    con.Close();        
}

并得到错误:

System.NullReferenceException: Object reference not set to an instance of an object.

符合以下条件:

ddlSelect.DataSource = dt;

有谁知道如何解决这个问题,还是我应该提供更多信息?提前致谢!

编辑:

我听从了你们的建议,现在我有:

 protected void FVStudentClass_ModeChanging(object sender, FormViewCommandEventArgs e)
    {
        if (FVStudentClass.CurrentMode != FormViewMode.Edit)
            return;
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ReinstatementCS"].ConnectionString);
        SqlCommand myCommand = new SqlCommand("SELECT Prefix + Number  + ', Section: ' + CAST(ClassSection AS VarChar) +  ', Location: ' + Location + ', Start Time: ' + StartTime + ', End Time: ' + EndTime + ', Days: ' + ClassDay + ', Credits: ' + CAST(ClassCredit AS VarChar) AS PN, SLN FROM Classes JOIN StudentClass on SLN = SCClass WHERE SCWSUID = " + Request.QueryString["ALWSUID"]);
        myCommand.Connection = con;
        SqlDataAdapter da = new SqlDataAdapter(myCommand);
        DataTable dt = new DataTable();
        da.Fill(dt);
        DropDownList ddlSelect = new DropDownList();
        ddlSelect = FVStudentClass.FindControl("ddlSelect") as DropDownList;
        if (ddlSelect != null)
        {
            ddlSelect.DataSource = dt;
            ddlSelect.Items.FindByText(dt.Rows[0]["PN"].ToString()).Selected = true;
        }
        con.Close();
    }

但我仍然卡住了,因为下拉列表没有从选择保存的值开始。你知道如何解决这个问题吗?我是否使用了错误的命令(我应该使用除 ModeChanging 之外的其他东西吗)?

4

3 回答 3

1
DropDownList ddlSelect = FVStudentClass.FindControl("ddlSelect") as DropDownList;

这里 ddlSelect 可以为空,所以你得到System.NullReferenceException

如果您使用as运算符,它可以在未找到名为“ddlSelect”的控件的情况下返回空值,因此最好添加空值检查,如下所示

if(ddlSelect !=null)
{
   // do the bindings 

}

如果要访问编辑模板控件,则需要检查当前模式是否等于编辑。如果它不编辑你找不到编辑模板控件

if (FormView1.CurrentMode != FormViewMode.Edit)
        return;
//your code 
于 2013-05-29T17:01:17.847 回答
1

请注意,在解决 nullreference 之后,您还需要更改:

ddlSelect.Items.FindByText(dt.Rows[0]["PN"].ToString());

进入

ddlSelect.Items.FindByText(dt.Rows[0]["PN"].ToString()).Selected = true;

使其被选中

于 2013-05-29T17:08:22.107 回答
0

我会检查用于返回下拉菜单的代码,因为您没有使用构造函数或 new 关键字。它返回一个空值吗?

于 2013-05-29T16:59:41.130 回答