我试图让我已经在formview的编辑页面内创建的下拉列表开始于先前从当前用户的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之外的东西吗?)
答案 0 :(得分:1)
DropDownList ddlSelect = FVStudentClass.FindControl("ddlSelect") as DropDownList;
此处ddlSelect可以为null,因此您可以获得System.NullReferenceException
如果你使用as
运算符,它可以返回null找不到名为'ddlSelect的控件',所以最好添加如下所示的null检查
if(ddlSelect !=null)
{
// do the bindings
}
如果要访问编辑模板控件,则需要检查当前模式是否等于编辑。如果不编辑,则无法找到编辑模板控件
if (FormView1.CurrentMode != FormViewMode.Edit)
return;
//your code
答案 1 :(得分:1)
请注意,解决空引用后,您还需要更改:
ddlSelect.Items.FindByText(dt.Rows [0] [ “PN”]的ToString());
到
ddlSelect.Items.FindByText(dt.Rows [0] [“PN”]。ToString())。Selected = true;
以便它被选中
答案 2 :(得分:0)
我会检查您用来返回下拉菜单的代码,因为您没有使用构造函数或new关键字。是否返回空值?