2013-10-18 195 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

看到我更新的答案! –

相关问题