2013-10-18 198 views
-4

在鏈接按鈕中,將下拉列表中的ID從會話ID保存到會話ID併發送文本以獲取選定內容,並且在網格上單擊選擇鏈接時在表單中自動填充表單做工精細..但試圖挽救button_save給予不設置到對象的實例錯誤對象引用...猜它與追趕dropdownvalue問題的時候.. plz幫助未將對象引用設置爲對象的實例..錯誤

public partial class Admin_AddSubject : System.Web.UI.Page 
    { 
     Add_Subject cm = new Add_Subject(); 
     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sms"].ConnectionString); 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!IsPostBack) 
      { 
       classfill(); 
      } 
     } 




    protected void LinkButton1_Click(object sender, EventArgs e) 
     { 


     LinkButton b = (LinkButton)sender; 
     GridViewRow row = (GridViewRow)b.NamingContainer; 
     if (row != null) 
     { 
      int rowIndex = row.RowIndex; 

      string pid = GridView1.DataKeys[rowIndex].Values["pid"].ToString(); 
      string subject = GridView1.DataKeys[rowIndex].Values["subject"].ToString(); 
      string id = GridView1.DataKeys[rowIndex].Values["id"].ToString(); 


      Session["pid"] = pid.ToString(); 
       // saving id from dropdownlist to session 
      Session["id"] = ddl_class.SelectedItem.Text; 


      TextBoxSubject.Text = subject.ToString(); 
       //sending dropdownlist text to get selected 
      ddl_class.Text = id.ToString(); 


     } 


    } 

    protected void ButtonSave_Click(object sender, EventArgs e) 
    { 
     // error: Object reference not set to an instance of an object. 
     cm.ButtonSave_Click(int.Parse(Session["id"].ToString()),TextBoxSubject.Text); 
     ScriptManager.RegisterStartupScript(this.Page, typeof(string), "alert", "alert('Your data successfully Saved..');", true); 
     ddl(); 
     clear(); 
     } 
+0

你在哪裏宣佈釐米? –

+1

聽起來像這個問題是一個對象引用沒有設置爲一個對象的實例... – Phill

+0

這一行中的一件事是null cm.ButtonSave_Click(int.Parse(Session [「id」]。ToString() ),TextBoxSubject.Text);'可能cm或Session [「id」]調試它找出哪些和爲什麼。 –

回答

0

首先檢查你的會話中可用。

protected void ButtonSave_Click(object sender, EventArgs e) 
    { 
     **if(Session["id"]!=null)//Check the condition for session is null** 
     { 

     cm.ButtonSave_Click(int.Parse(Session["id"].ToString()),TextBoxSubject.Text); 
     ScriptManager.RegisterStartupScript(this.Page, typeof(string), "alert", "alert('Your data successfully Saved..');", true); 
     ddl(); 
     clear(); 
     } 
     else 
     { 
     throw new Exception("session was cleared or time out") // display error like session was cleared or time out 
     } 

    } 

編輯

我認爲你可以直接在save方法中給出* ddl_class.SelectedItem.Text *值,你不需要在session中存儲值。

像:

cm.ButtonSave_Click(int.Parse(ddl_class.SelectedItem.Text),TextBoxSubject.Text);

,並確保您的下拉列表中選擇值不爲空,你設定DROPDOWNLIST 的AutoPostBack =「真」你?

+0

感謝您的迴應..你的權利異常來了..會議不可用..但你知道的方式,使我可以從一個下拉列表中保存價值列表ID被選中並將其添加到會話中。 –

+0

爲什麼使用會話進行此任務?你可以直接給'ddl_class.SelectedItem.Text'而不是'Session [「id」]。ToString()'並檢查下拉選擇的值是否爲空。 –

+0

看到我更新的答案! –

相關問題