'C# Data Grid keeps populating with old data
I am a noob playing around with basic read, write and delete queries through C# into a MySQL Database. I am however stuck at the initial reading part. It appears that while reading the data from MySQL into a Data Grid View goes fine, however upon attempting to refresh the data it keeps populating the Grid with the same data (See photo) Unsure if this is originating from the Data Grid or the MySQL Dataset.
public partial class CBMain : Form
{
Connection con = new Connection();
private static ArrayList Listid = new ArrayList();
private static ArrayList ListItem = new ArrayList();
private static ArrayList ListDescription = new ArrayList();
private static ArrayList ListSerial = new ArrayList();
private static ArrayList ListQuantity = new ArrayList();
public CBMain()
{
InitializeComponent();
}
private void GetData()
{
try
{
con.Open();
string query = "select id, Item, Description, Serial, QuantityIn from TB_CB_StockIn";
MySqlDataReader row;
row = con.ExecuteReader(query);
if (row.HasRows)
{
while (row.Read())
{
Listid.Add(row["id"].ToString());
ListItem.Add(row["Item"].ToString());
ListDescription.Add(row["Description"].ToString());
ListSerial.Add(row["Serial"].ToString());
ListQuantity.Add(row["QuantityIn"].ToString());
}
}
else
{
MessageBox.Show("Data not found");
}
con.Close();
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
private void updateDatagrid()
{
for (int i = 0; i < Listid.Count; i++)
{
DataGridViewRow newRow = new DataGridViewRow();
newRow.CreateCells(DGV_Stock);
newRow.Cells[0].Value = Listid[i];
newRow.Cells[1].Value = ListItem[i];
newRow.Cells[2].Value = ListDescription[i];
newRow.Cells[3].Value = ListSerial[i];
newRow.Cells[4].Value = ListQuantity[i];
DGV_Stock.Rows.Add(newRow);
}
}
private void BT_Refresh_Click(object sender, EventArgs e)
{
DGV_Stock.DataSource = null;
DGV_Stock.Rows.Clear();
DGV_Stock.Refresh();
GetData();
if (Listid.Count > 0)
{
updateDatagrid();
}
else
{
MessageBox.Show("Data not found");
}
}
}
Connection :
class Connection
{
MySql.Data.MySqlClient.MySqlConnection conn;
static string host = "*******";
static string database = "*******";
static string userDB = "*******";
static string password = "*******";
public static string strProvider = "server=" + host + ";Database=" + database + ";User ID=" + userDB + ";Password=" + password;
public bool Open()
{
try
{
strProvider = "server=" + host + ";Database=" + database + ";User ID=" + userDB + ";Password=" + password;
conn = new MySqlConnection(strProvider);
conn.Open();
return true;
}
catch (Exception er)
{
MessageBox.Show("Connection Error ! " + er.Message, "Information");
}
return false;
}
public void Close()
{
conn.Close();
conn.Dispose();
}
public DataSet ExecuteDataSet(string sql)
{
try
{
DataSet ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
da.Fill(ds, "result");
return ds;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return null;
}
public MySqlDataReader ExecuteReader(string sql)
{
try
{
MySqlDataReader reader;
MySqlCommand cmd = new MySqlCommand(sql, conn);
reader = cmd.ExecuteReader();
return reader;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return null;
}
public int ExecuteNonQuery(string sql)
{
try
{
int affected;
MySqlTransaction mytransaction = conn.BeginTransaction();
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
affected = cmd.ExecuteNonQuery();
mytransaction.Commit();
return affected;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return -1;
}
}
Initial Data Read in Red, then after Refresh keeps populating
Solution 1:[1]
You never clear arraylist data so it will appear multiple times your click. My suggestion you should learn how to use arraylist. Anyway, you should use only one arraylist and create object class instead to store those fields.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sovary |
