[英]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.