Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamic sql generation is not supported against multiple base tables

I tried to add a new row to a Table in an SQL DB, but I had a problem :

dynamic sql generation is not supported against multiple base tables

this is the code I tried :

private MyClass myClass = new MyClass();
private SqlDataAdapter adapter;
private SqlDataAdapter adapter2;

private void GestionCollections_Load(object sender, EventArgs e)
{
     adapter = new SqlDataAdapter("select Id_Collection ID, Libelle_Collection Collection,Libelle_Editeur Editeur from Collection_ left join Editeur on Id_Editeur = Collection_.Id_Editeur_Editeur", myClass.cnx);
     adapter.Fill(myClass.ds, "Collection_");

     adapter2 = new SqlDataAdapter("Select Id_Editeur ID,Libelle_Editeur Editeur from Editeur", myClass.cnx);
     adapter2.Fill(myClass.ds, "Editeur");
}

private void AjouterBarButton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
    String newKeyWordCollectionName = ajoutCollection.KeyWordCollectionName;
    String newKeyWordAEditeurName = ajoutCollection.KeyWordEditeurName;        
    DataRow row = myClass.ds.Tables["Collection_"].NewRow();
    row[1] = newKeyWordCollectionName;

    foreach(var myRow in myClass.ds.Tables["Editeur"].AsEnumerable())
    {
         if (newKeyWordAEditeurName == myRow[1] as String)
              row[2] = (int)myRow[0];
    }
     myClass.ds.Tables["Collection_"].Rows.Add(row);
     SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
     adapter.Update(myClass.ds, "Collection_");

}
like image 833
Spoon Yukina Avatar asked Nov 30 '25 02:11

Spoon Yukina


2 Answers

Change your select query and add distinct with inner join.

For example there are two query from which you can understand that what I want to tell you

Wrong query

select iop.pob_id, iop.pob_product_id, iop.pob_qty, iop.pob_unit_id
    , iop.pob_rate, iop.pob_value, iop.pob_fiscalyear_id
    , **p.product_desc** as orderBy from inv_product_open_balc iop
left join inv_product p on iop.pob_product_id = p.product_id
where p.product_desc like 'Air Freshner%' and iop.pob_fiscalyear_id = 3

Correct query

select distinct iop.pob_id, iop.pob_product_id, iop.pob_qty
    , iop.pob_unit_id, iop.pob_rate, iop.pob_value, iop.pob_fiscalyear_id
    , **(select Product_desc from** inv_product p where p.product_id = iop.pob_product_id )as orderBy
from inv_product_open_balc iop
inner join inv_product p on iop.pob_product_id = p.product_id
where p.product_desc like 'Air Freshner%' and iop.pob_fiscalyear_id = 3
like image 89
Saajan Chaudhary Avatar answered Dec 02 '25 16:12

Saajan Chaudhary


You can't use an SqlCommandBuilder here:

Automatically generates single-table commands that are used to reconcile changes made to a DataSet with...

The key words here being "single-table". It has no way to reverse engineer from the SELECT statement how a specific update should be applied (e.g. if you NULL all of the columns from the right side of a left join, should it delete the row, or set each column to null.

You need to author appropriate Insert, Update and Delete commands on the SqlDataAdapter.

like image 35
Damien_The_Unbeliever Avatar answered Dec 02 '25 14:12

Damien_The_Unbeliever