Click here to Skip to main content
15,912,507 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I have a problem on refreshing the dataset. here is my code.

C#
public void BindingMetaboliteName(frmMetCon config)
{
    schemaForm = new SchemaName();
    string connStr = "datasource=localhost;port=3306;username=root;password=root;";
    conn = new MySqlConnection(connStr);
    command = conn.CreateCommand();
    string database = schemaForm.getData;

    try
    {
        dtable = new DataTable();
        bindingSource = new BindingSource();


        conn.Open();
        switch (cmbMetabolite.SelectedIndex)
        {
            case 0:
                dbSumMetabolite.ResetBindings();
                dbSumMetabolite.AutoGenerateColumns = false;
                command.CommandText = "SELECT MetaboliteID, Metabolite_Name FROM " + database +
                                      ".Metabolites " +
                                      "WHERE Metabolite ID IN ('met1', 'met2');";
                MySqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
                dtable.Load(dr);
                dbSumMetabolite.DataSource = null;
                for (int i = 0; i < dtable.Rows.Count; i++)
                {
                    dbSumMetabolite.Rows.Add();
                    dbSumMetabolite.Rows[dbSumMetabolite.Rows.Count - 1].Cells["MetaboliteID"].Value = dtable.Rows[i]["MetaboliteID"].ToString();
                    dbSumMetabolite.Rows[dbSumMetabolite.Rows.Count - 1].Cells["Metabolite_Name"].Value = dtable.Rows[i]["Metabolite_Name"].ToString();
                }
                break;

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




The problem i am facing is that the reset binding is not working. whenever i updating the the data the datagridview will duplicate the rows instead of replacing it. and this code is inserted in leavepage event handled
Posted
Updated 12-Nov-13 13:09pm
v2
Comments
idenizeni 12-Nov-13 16:21pm    
If you run the query directly against the database do you see duplicate records? It looks like the issue may be in getting duplicate records in the database call.
arave0521 12-Nov-13 19:02pm    
yes. I tried to put dbSumMetabolite.DataSource = null;
It is not working.

write "dbSumMetabolite.DataSource=null;" at the starting of the code
 
Share this answer
 
Comments
arave0521 12-Nov-13 11:25am    
not working. still produce duplication
sorry write "dbSumMetabolite.Rows.Clear();" at the starting of the code
 
Share this answer
 
Comments
agent_kruger 13-Nov-13 6:00am    
did the above solution worked? @arave0521

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900