简体   繁体   English

错误:其他信息:设置DataSource属性时,不能修改项目集合

[英]ERROR: Additional information: Items collection cannot be modified when the DataSource property is set

I'm having this issue with a program that adds files to my combobox and also added to database. 我遇到了一个程序,该程序将文件添加到我的组合框,同时也添加到了数据库。

I had 2 button add+ and - to delete the fields one by one on combobox 我有2个按钮add +和-分别删除组合框上的字段

when i click ADD + button the data in my orm that means i have another combobox, lable,datetimepicker these are added in my FAVORITES combobox 当我单击添加+按钮时,我的orm中的数据意味着我还有另一个组合框,标签,日期时间选择器,这些都添加到了我的收藏组合框中

combofavorite.Items.Add("Report Type: " + cmbreporttype.Text + " Date From: " + dateTimePicker1.Text + " To: " + dateTimePicker2.Text);
            combofavorite.SelectedIndex = 0;
            MessageBox.Show("Add Report Type: " + cmbreporttype.Text + " Date From: " + dateTimePicker1.Text + " To: " + dateTimePicker2.Text + " TO Favorite", "Added to Favorite", MessageBoxButtons.OK, MessageBoxIcon.Information);

Add the added data in the combobox is also added into database table 在组合框中添加添加的数据也添加到数据库表中

 MySqlConnection connection = new MySqlConnection(MyCon);
            MySqlCommand cmd = new MySqlCommand("INSERT INTO favorite (favorite) VALUES (@favo)", connection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@favo", combofavorite.Text);
            connection.Open();
            cmd.ExecuteNonQuery();



 private void cmbfav_SelectedIndexChanged(object sender, EventArgs e)
        {
            string ID = cmbfav.SelectedValue.ToString();
        }

When i try this i got an error on here. 当我尝试这个时,我在这里遇到错误。

在此处输入图片说明

Plese help me to solve my problem . 请帮我解决我的问题。

@Anu Dont use combofavorite.Text in AddWithValue. @Anu不使用combofavorite.Text在AddWithValue。

instead of this 代替这个

     MySqlCommand cmd = new MySqlCommand("INSERT INTO favorite (favorite) VALUES (@favo)", connection);
     cmd.CommandType = CommandType.Text;
     cmd.Parameters.AddWithValue("@favo", combofavorite.Text);

use this: 用这个:

      MySqlCommand cmd = new MySqlCommand("INSERT INTO favorite (favorite) VALUES (@favo)", connection);
      cmd.CommandType = CommandType.Text;
      cmd.Parameters.AddWithValue("@favo","Report Type: " + cmbreporttype.Text + " Date From: " + dateTimePicker1.Text + " To: " + dateTimePicker2.Text);

Hope it will help. 希望它会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 列表框错误:设置了DataSource属性时,无法修改项目集合 - listbox error: Items collection cannot be modified when the DataSource property is set 例外:设置DataSource属性时,无法修改项集合 - Exception : Items collection cannot be modified when the DataSource property is set 在c#中设置DataSource属性时,无法修改项目集合 - Items collection cannot be modified when the DataSource property is set in c# 设置DataSource属性时,无法修改c#项目集合 - c# Items collection cannot be modified when the DataSource property is set 设置DataSource属性时,无法修改项目集合 - Items collection cannot be modified when the DataSource property is set 如何在不同的 colors 中为列表框中的项目着色? 出现异常:设置 DataSource 属性后无法修改 Items 集合 - How to color items in listBox in different colors? getting exception : Items collection cannot be modified when the DataSource property is set System.ArgumentException:设置DataSource属性时,不能修改Items集合 - System.ArgumentException: Items collection cannot be modified when the DataSource property is set 设置DataSource属性后,将无法修改项目集合。 C# - Items collection cannot be modified when the DataSource property is set. c# System.ArgumentException:“设置 DataSource 属性时无法修改项目集合。” C# Windows Forms - System.ArgumentException: 'Items collection cannot be modified when the DataSource property is set.' C# Windows Forms C#ListBox:设置DataSource属性时,无法修改Items集合 - C# ListBox : Items collection cannot be modified when the DataSource property is set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM