Add New Record Binding Source...

Hi everybody,

    I new with binding source control and binding navigator control of .Net Framework 2.0. I having problem with adding new record. I have binding source control and bindingnavigator control, it displays the data ok and data navigation between records is ok. But when I try to add data using "+" icon on binding navigator control, it causes error on data type...What is the problem and how can I make it work   Thanks.

Error Message:

System.InvalidOperationException

"Objects added to a BindingSource's list must all be of the same type."

"   at System.Windows.Forms.BindingSource.Add(Object value)\r\n   at System.Windows.Forms.BindingSource.AddNew()\r\n   at PracticeWinApp_CSharp.BindedControlsForm.bindingNavigatorAddNewItem_Click(Object sender, EventArgs e) in ...

 Here is the part of the code:

private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)

{

this.bindingSource1.AddNew(); <<--- Error Occur here...

}

private void bindingSource1_AddingNew(object sender, AddingNewEventArgs e)

{

//this.ClearFields();

if (this.txtProdId.Text != "" && this.txtProdName.Text != "" && this.txtPrice.Text != "" && this.txtQty.Text != "" && this.txtReorder.Text != "")

{

DataRow dr = dsProducts.Products.NewProductsRow();

//DataRow dr = ds.Tables["Products"].NewRow();

dr["ProductId"] = this.txtProdId.Text.Trim();

dr["ProductName"] = this.txtProdName.Text.Trim();

dr["UnitPrice"] = Double.Parse(this.txtPrice.Text.Trim());

dr["Quantity"] = Int32.Parse(this.txtQty.Text.Trim());

dr["ReorderLevel"] = Int32.Parse(this.txtReorder.Text.Trim());

e.NewObject = dr;

}

}

Now the sql structure is:

Products Table

  ProductId VarChar(10), ProductName VarChar(100), UnitPrice money, Quantity int,

ReorderLevel int

Structure of dsProducts:

  ProductId string, ProductName string, UnitPrice Double(Decimal), Quantity Int32, ReorderLevel Int32

 

den2005



Answer this question

Add New Record Binding Source...

  • OLD_TIMES

    Thanks for the reply..

    I have tried that already, still causing error when you click the navigation buttons on the bindingnavigator control...

    den2005


  • swift_hcw

    hi,

    bindingsource doesn't contain Datarows so you can't add datarows to it, bindingsource depend on a dataview which contain DataRowView its something the same like datarow but different class

    you can add the row to your table, the bindingsource will detect it and refelect that to your display control

    hope this helps



  • lfx

    hi,

    are those textbox's or datagridview textboxcolumns

    anyway try add the datarow to your table instead of accept changeds

    MyDataSet.MyTable.Rows.Add(dr);

    hope this helps



  • flyboy2001

    Thanks for the reply, shakalama.

    I tried the approach you mentioned, but it seems the new row is empty...

    Here is modified code...Can you provide some codes that works Thanks..

    private void bindingSource1_AddingNew(object sender, AddingNewEventArgs e)

    {

    //this.ClearFields();

    if (this.txtProdId.Text != "" && this.txtProdName.Text != "" && this.txtPrice.Text != "" && this.txtQty.Text != "" && this.txtReorder.Text != "")

    {

    DataRow dr = dsProducts.Products.NewProductsRow();

    dr["ProductId"] = this.txtProdId.Text.Trim();

    dr["ProductName"] = this.txtProdName.Text.Trim();

    dr["UnitPrice"] = Double.Parse(this.txtPrice.Text.Trim());

    dr["Quantity"] = Int32.Parse(this.txtQty.Text.Trim());

    dr["ReorderLevel"] = Int32.Parse(this.txtReorder.Text.Trim());

    dsProducts.Products.AcceptChanges();

    }

    }

    den2005


  • Add New Record Binding Source...