owen
owen

Reputation: 126

Updating mysql table gives error

i have this code and it is giving me this error i tried really hard to solve this but i am getting no luck. This method is used to update a table from mysql by giving select statement, tablename and dataset. The error is "Update unable to find TableMapping['tblapartments'] or DataTable 'tblapartments'."

public bool UpdateDatabase(string SelectStat, string tablename, DataSet dataset)
{
    try
    {
        MySqlDataAdapter da = new MySqlDataAdapter(SelectStat, _conn);
        MySqlCommandBuilder MYCB = new MySqlCommandBuilder(da);
        DataSet ds = dataset;
        da.MissingSchemaAction = MissingSchemaAction.AddWithKey;

        da.Update(ds, tablename);
        MySqlConnection.ClearAllPools();
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }

}

this is the whole method used for calling the method.

 DataTable dt = (DataTable)dtgApartList.DataSource;
 DataSet ds = GlobalVariables.Adap.GetTableCus("SELECT NameNo, MaintenanceFee, AdminFee  FROM tblapartments WHERE BuildingID = '" + cmbBuildingName.SelectedValue.ToString() + "'");
 ds.Tables[0].Merge(dt, false);
 GlobalVariables.Adap.UpdateDatabase("SELECT NameNo, MaintenanceFee, AdminFee  FROM tblapartments WHERE BuildingID = '" + cmbBuildingName.SelectedValue.ToString() + "'", "tblapartments", ds); 

Upvotes: 0

Views: 326

Answers (1)

owen
owen

Reputation: 126

I figured out what i was doing wrong i needed to mapp the tables here is the fixed code

public bool UpdateDatabase(string SelectStat, string tablename, DataSet dataset)
{
    try
    {
        MySqlDataAdapter da = new MySqlDataAdapter(SelectStat, _conn);
        MySqlCommandBuilder MYCB = new MySqlCommandBuilder(da);
        DataSet ds = dataset;
        da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
        //Added this
        ITableMapping testing = da.TableMappings.Add(tablename,"Table");
        da.Update(ds, tablename);
        MySqlConnection.ClearAllPools();
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }

}

Upvotes: 1

Related Questions