开发者

I am moving selected data from one listbox to another but I am getting an error "Object reference not set to an instance of an object."

            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.Selected开发者_StackOverflowItem.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();


I just saw your comment... Try to check if 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜