items collection cannot be modified when the DataSource property is set.

Posted by Saadiq under C# on 7/9/2013 | Points: 10 | Views : 1742 | Status : [Member] | Replies : 1
i am getting an error items collection cannot be modified when the DataSource property is set. in my code which is like bellow.
i am getting this error when i am clicking on delete button( btnDelete method)
i need to reload my combobox after i delete the image..
public void Bind_Image_Name()
{
try
{
cmbPick.Items.Clear();
conn.Open();
cmd = new SqlCommand("SelectImageName", conn);//here SelectImageName is the StoredProcedure name
cmd.CommandType = CommandType.StoredProcedure;
da = new SqlDataAdapter(cmd);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
cmbPick.DataSource = dt;
cmbPick.ValueMember = "ImagePath";
cmbPick.DisplayMember = "ImagePath";
cmbPick.Text = "select one";
cmbPick.SelectedIndex = 0;

}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}


private void btnDelete_Click(object sender, EventArgs e)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
cmd = new SqlCommand("SelectImage", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Option", "Delete");
cmd.Parameters.AddWithValue("@ImagePath", cmbPick.Text);
cmd.ExecuteNonQuery();
}
conn.Close();

Bind_Image_Name();
}

saadiq..


Responses

Posted by: Vuyiswamb on: 7/9/2013 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
if you look at the HTML of cmbPick dropdown , you will notice it has a datasource set , so remove it

Thank you for posting at Dotnetfunda
[Administrator]

Saadiq, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response