开发者

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."

开发者 https://www.devze.com 2023-02-18 12:17 出处:网络
ListItem item = new ListItem(); item.Text = ListBox1.SelectedItem.Text;////(error occured here) ListBox2.Items.Add(item.Text);
            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

精彩评论

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

关注公众号