2013-05-29 92 views
1

我想讓我已經在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

我認爲你必須先用new關鍵字創建DropDownList對象。你沒有創建任何對象。 –

+0

@Vivek你能告訴我那個代碼嗎?我沒有在第一個成員這樣做,它的工作正常。第二次而不是第一次導致問題的區別是什麼? – Austin

回答

1

注意解決nullreference後,你也將要改變:

ddlSelect.Items.FindByText(dt.Rows [0] [ 「PN」]的ToString());

ddlSelect.Items.FindByText(dt.Rows [0] [ 「PN」]的ToString()。)選定=真。

以便它變成被選中

0

我會檢查您用來返回下拉菜單的代碼,因爲您沒有使用構造函數或new關鍵字。它是否返回空值?

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

這裏ddlSelect可以爲空,所以你得到System.NullReferenceException

如果使用as符,所以櫃面名爲「ddlSelect」,所以最好添加空校驗就像沒有找到控制返回null下面

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

} 

如果你想訪問編輯模板控件,你需要檢查當前模式等於編輯或不編輯,如果它不編輯,你不能找到編輯模板控件

if (FormView1.CurrentMode != FormViewMode.Edit) 
     return; 
//your code 
相關問題