?
Solved

Item Collection cannot be modified when the datasource property is set

Posted on 2012-08-22
9
Medium Priority
?
3,917 Views
Last Modified: 2012-09-01
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

0
Comment
Question by:r3nder
  • 5
  • 3
9 Comments
 
LVL 18

Expert Comment

by:Rajar Ahmed
ID: 38321269
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
 
LVL 6

Author Comment

by:r3nder
ID: 38321471
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
 
LVL 18

Expert Comment

by:Rajar Ahmed
ID: 38321511
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:Rajar Ahmed
ID: 38321624
try this code,for this action.
http://forums.asp.net/t/1003876.aspx/1
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38322107
@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
 
LVL 6

Author Comment

by:r3nder
ID: 38323240
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
 
LVL 6

Author Comment

by:r3nder
ID: 38327090
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
 
LVL 6

Accepted Solution

by:
r3nder earned 0 total points
ID: 38338925
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
 
LVL 6

Author Closing Comment

by:r3nder
ID: 38356899
so options were given to get a solution
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses
Course of the Month16 days, 2 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question