Item Collection cannot be modified when the datasource property is set

Hi I am moving 1 item from one listbox to the other and I keep getting the  error in the title
I do not know how to take the datasouce away and still have my values show up - any help
        protected void LoadRolesandGroups(int userid)
        {

            string connectionString = ConfigurationManager.ConnectionStrings["TaskManager"].ToString();
            SqlConnection con = new SqlConnection(connectionString);

            try
            {
                int iuserid = 87;// Selected agent UID
                SqlCommand com = new SqlCommand("sp_Membership_GetAllRoleswithUser", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@useruid", iuserid);
                SqlDataAdapter sqldba = new SqlDataAdapter(com);
                DataSet dsRoles = new DataSet();
                sqldba.Fill(dsRoles);
                dsRoles.Tables[0].DefaultView.RowFilter = "InRole = 1";
                lstActiveRoles.DataSource=dsRoles.Tables[0];
                lstActiveRoles.DisplayMember = "RoleDefinition";
                lstActiveRoles.ValueMember = "roledefuid";
              }

            catch (Exception exLoadRolesandGroups)
            {
                con.Close();
                MessageBox.Show(exLoadRolesandGroups.Message.ToString());
            }
        }

Open in new window

LVL 6
r3nderAsked:
Who is Participating?
 
r3nderConnect With a Mentor Author Commented:
I figured it out - since it was databound and I was doing a rowfilter  on the data I simply inserted it into the database and refreshed the datasource - thanks anyway
0
 
Rajar AhmedConsultantCommented:
Try this,
lstActiveRoles.DataSource=nothing;
        protected void LoadRolesandGroups(int userid)
        {

            string connectionString = ConfigurationManager.ConnectionStrings["TaskManager"].ToString();
            SqlConnection con = new SqlConnection(connectionString);

            try
            {
                int iuserid = 87;// Selected agent UID
                SqlCommand com = new SqlCommand("sp_Membership_GetAllRoleswithUser", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@useruid", iuserid);
                SqlDataAdapter sqldba = new SqlDataAdapter(com);
                DataSet dsRoles = new DataSet();
                sqldba.Fill(dsRoles);
                dsRoles.Tables[0].DefaultView.RowFilter = "InRole = 1";
                lstActiveRoles.DataSource = Nothing; // Need to set the data source nothing
                lstActiveRoles.Items.Clear();
                lstActiveRoles.DataSource=dsRoles.Tables[0];
                lstActiveRoles.DisplayMember = "RoleDefinition";
                lstActiveRoles.ValueMember = "roledefuid";
              }

            catch (Exception exLoadRolesandGroups)
            {
                con.Close();
                MessageBox.Show(exLoadRolesandGroups.Message.ToString());
            }
        }

Open in new window

0
 
r3nderAuthor Commented:
sorry that didnt work here is the how page as a refference
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.SqlClient;
using System.Configuration;
using Sentry;
using Sentry.Security;
using System.Web;
namespace testlistbox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void MoveListBoxItems(ListBox source, ListBox destination)
        {
            ListBox.SelectedObjectCollection sourceItems = source.SelectedItems;
            foreach (var item in sourceItems)
            {
                destination.Items.Add(item);
            }
            while (source.SelectedItems.Count > 0)
            {
                source.Items.Remove(source.SelectedItems[0]);
            }
        } 


        private void btnTo_Click(object sender, EventArgs e)
        {
            MoveListBoxItems(lstRoles, lstActiveRoles);
        }

        private void btnFrom_Click(object sender, EventArgs e)
        {
            MoveListBoxItems(lstActiveRoles, lstRoles);

        }

        public void fillRoles()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["TaskManager"].ToString();
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                string nothing = null;
                //Sentry.Security.KKMembershipUser user = KKPlmData.User.Get(false);
                con.Open();
                int userid = 87;
                SqlCommand com = new SqlCommand("sp_Membership_GetAllRoleswithUser", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@Useruid", userid);
                SqlDataAdapter sqldba = new SqlDataAdapter(com);
                DataSet dsRoles = new DataSet();
                sqldba.Fill(dsRoles);
                DataTable dtRoles = dsRoles.Tables[0];
                //lstRoles.DataSource = nothing;
                lstRoles.DataSource = dtRoles;
                lstRoles.DisplayMember = "RoleDefinition";
                lstRoles.ValueMember = "Roledefuid";
                DataTable dtActiveRoles = dsRoles.Tables[0].Clone();
                //lstActiveRoles.DataSource = dtActiveRoles;
                lstActiveRoles.DisplayMember = "RoleDefinition";
                lstActiveRoles.ValueMember = "roledefuid";
                //rlbActiveGroups.ValueMember = "Roledefuid";<--make these hidden text fields
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            fillRoles();
            LoadRolesandGroups(87);
        }
        protected void LoadRolesandGroups(int userid)
        {

            string connectionString = ConfigurationManager.ConnectionStrings["TaskManager"].ToString();
            SqlConnection con = new SqlConnection(connectionString);

            try
            {
                string nothing = null;
                int iuserid = 87;// Selected agent UID
                SqlCommand com = new SqlCommand("sp_Membership_GetAllRoleswithUser", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@useruid", iuserid);
                SqlDataAdapter sqldba = new SqlDataAdapter(com);
                DataSet dsRoles = new DataSet();
                sqldba.Fill(dsRoles);
                dsRoles.Tables[0].DefaultView.RowFilter = "InRole = 1";
                lstActiveRoles.DataSource = nothing;
                lstActiveRoles.DataSource=dsRoles.Tables[0];
                lstActiveRoles.DisplayMember = "RoleDefinition";
                lstActiveRoles.ValueMember = "roledefuid";
              }

            catch (Exception exLoadRolesandGroups)
            {
                con.Close();
                MessageBox.Show(exLoadRolesandGroups.Message.ToString());
            }
        }
  
}
}

Open in new window

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Rajar AhmedConsultantCommented:
which line/Method  your are getting error ?
i dont find the invocation of the code which was changed on last post during button action ?
0
 
Rajar AhmedConsultantCommented:
try this code,for this action.
http://forums.asp.net/t/1003876.aspx/1
0
 
Obadiah ChristopherCommented:
@meeran03, this is a Windows App I guess... That link might not help.

private void MoveListBoxItems(ListBox source, ListBox destination)
        {
           
            ListBox.SelectedObjectCollection sourceItems = source.SelectedItems;
            source.DataSource = nothing
            foreach (var item in sourceItems)
            {
                destination.Items.Add(item);
            }
            while (source.SelectedItems.Count > 0)
            {
                source.Items.Remove(source.SelectedItems[0]);
            }
        }
0
 
r3nderAuthor Commented:
I tried all of thos suggestions - I got the same error with the code you suggested @informaniac - now I am getting  Input array is longer than the number of columns in this table. - they  are the same length
I am now using
         private void btnGroupTo_Click(object sender, EventArgs e)
         {
             if (rlbGroups.SelectedIndex != -1)
             {
                 DataRowView drvTemp = (DataRowView)rlbGroups.SelectedItem;
                 DataRow drTemp = drvTemp.Row;
                 DataTable dtGroups = new DataTable();
                 DataTable dtActiveGroups = new DataTable();
                 dtActiveGroups.Rows.Add(drTemp.ItemArray);
                 dtGroups.Rows.Remove(drTemp);
             }
         }

Open in new window

0
 
r3nderAuthor Commented:
I may have misstyped this is therror i am getting now I am getting  "Input array is longer than the number of columns in this table"
0
 
r3nderAuthor Commented:
so options were given to get a solution
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.