2011-03-21 27 views
0
  ListItem item = new ListItem(); 
      item.Text = ListBox1.SelectedItem.Text;////(error occured here) 
      ListBox2.Items.Add(item.Text); 
      ListBox1.Items.Remove(item.Text); 
      string usrid = null; 

      string usridselect = "select user_id from user_membership where email_id ='" + ListBox1.SelectedItem.Text + "'"; 
      SqlConnection sqcon = DBConnection.connectDB(); 
      sqcon.Open(); 

      SqlDataReader sqldr = DBConnection.SelectData(usridselect, sqcon); 
      if (sqldr.HasRows) 
      { 
       while (sqldr.Read()) 
       { 
        usrid = sqldr[0].ToString(); 
       } 
      } 
      sqldr.Close(); 
+2

請澄清。錯誤發生在哪裏(哪條線)?如果你得到這個錯誤,你可以跳到發生錯誤的那一行,然後你很可能會看到哪個對象是空的。 – Botz3000 2011-03-21 07:38:50

+0

請給我們更多的信息,錯誤源自哪一行? – 2011-03-21 07:39:34

+0

項目與列表框的綁定發生在!IsPostback中嗎?如果不是,那麼您的SelectedItem將爲空。 – TurBas 2011-03-21 07:43:06

回答

0

我剛纔看到您的評論...嘗試檢查,如果將selectedItem!= NULL

if (ListBox1.SelectedItem!=null) 
    { 
     /// paste your code here 

     /// anyway - keep in mind that you refer to a selectedItem in your listbox after 
     /// you removed it from the according listbox 
    } 
+0

它不工作,它顯示相同的錯誤。我正在按鈕控制下使用它。 – user123 2011-03-21 07:53:26

+0

你可以粘貼完整的eventHandler代碼!和確切的錯誤信息 – 2011-03-21 09:00:58

+0

protected void Button1_Click1(object sender,EventArgs e) { string email_id = ListBox1.SelectedItem.Text; ///(此處顯示錯誤) ListBox2.Items.Add(email_id); ListBox1.Items.Remove(email_id); string usrid = null; string usridselect =「select user_id from user_membership where email_id ='」+ email_id +「'」; SqlConnection sqcon = DBConnection.connectDB(); sqcon.Open(); – user123 2011-03-21 09:41:50

相關問題