it displays this error Items collection cannot be modified when the DataSource property is set.?

Posted by Sudheep.grandhe under C# on 11/3/2011 | Points: 10 | Views : 7946 | Status : [Member] | Replies : 7
Hi,

it displays this error Items collection cannot be modified when the DataSource property is set.?i am underlying the error.please find it.

Error:-Items collection cannot be modified when the DataSource property is set.

public void loadcompanyname()
{
DBConnection db = new DBConnection();

SqlConnection con = db.opencon();//new SqlConnection("data source=mars-104;intial catalog=marsweb.imageimpacts;integrated security=true");
SqlCommand cmd = new SqlCommand("select custid,custname from tbl_customer order by custname", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
cmb_companyname.DataSource = dt;
cmb_companyname.ValueMember = "custid";
cmb_companyname.DisplayMember = "custname";
}
cmb_companyname.Items.Insert(0, "-select one-");
}

Best,
Sudheep.



Responses

Posted by: Jpchoudhari on: 11/3/2011 [Member] Starter | Points: 25

Up
0
Down
u have already assigned datatable to ur combo box so you can not directly insert any item to combo box collection.
first insert ur item (0, "-select one-") in datatable and then bind that to the combo box.

Kind Regards,
Jay

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

Posted by: Sudheep.grandhe on: 11/3/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Can you please send the code how can i insert in datatable and how it is bind to the combobox.please do the needful.


Best,
Sudheep.

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

Posted by: Jpchoudhari on: 11/3/2011 [Member] Starter | Points: 25

Up
0
Down
u dont have to insert it in database. once u get all ur data in datatable just insert one row at 0th position of ur datatable

Kind Regards,
Jay

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

Posted by: Sudheep.grandhe on: 11/3/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

I am not getting you.how can i insert the code .

Best,
Sudheep.

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

Posted by: Jpchoudhari on: 11/3/2011 [Member] Starter | Points: 25

Up
0
Down
   DataRow drNewRow = dt.NewRow();

drNewRow["DisplayItem"] = "-select one-";
drNewRow["ValueItem"] = "0";
dt.Rows.InsertAt(drNewRow, 0);


Kind Regards,
Jay

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

Posted by: Sudheep.grandhe on: 11/3/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

It shows the error.like this.
Error:-Object reference not set to an instance of an object.

private void cmb_companyname_SelectedIndexChanged(object sender, EventArgs e)
{
//SqlConnection con = new SqlConnection("data source=mars-104;intial catalog=marsweb.imageimpacts;integrated security=true");

DBConnection db = new DBConnection();

SqlConnection con = db.opencon();
SqlCommand cmd = new SqlCommand("select custaddress from tbl_customer where custname=@custname", con);
cmd.Parameters.AddWithValue("@custname", cmb_companyname.SelectedItem.ToString());
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
txt_customeraddress.Text = dt.Rows[0][0].ToString();
}
}

Best,
Sudheep.

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

Posted by: Jpchoudhari on: 11/3/2011 [Member] Starter | Points: 25

Up
0
Down
y u have commented ur first line... try to see your self the problem b4 posting here.... u could have done this yourself

Kind Regards,
Jay

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

Login to post response