Duplicate the old data when the datagridview refreshed
i already can add the new data to the database and display that database
through datagridview, and i command to refresh the datagridview whenever i
click "Add" button (Adding new data to the database). But it is duplicate
the old data with the new one.
Here is the screenshot that show it duplicate after adding new data and
refresh datagridview:
In the above image, there is duplicate data of ID "16", whenever i enter
the new value and insert it to the database, it add the new (current)
value to the database, but the old value will be duplicated. Therefore, i
have to quit the program and re-launch it again. But, is there any other
ways to solve this (Old data not being duplicated after add a new data to
the database and refresh the datagridview)?
Here is my code:
private void ViewDatabase(object sender, EventArgs e)
{
using (OleDbConnection conn = new
OleDbConnection(connectionString))
{
string query = "SELECT * FROM [Table]";
conn.Open();
using (OleDbDataAdapter adapter = new
OleDbDataAdapter(query, conn))
{
adapter.Fill(ds, "Table");
dataGridView1.DataSource = ds.Tables[0];
}
conn.Close();
}
}
private void Add(object sender, EventArgs e)
{
using (OleDbConnection conn = new
OleDbConnection(connectionString))
{
string query = "INSERT INTO [Table] ([ProductCode],
[Quantity], [Description], [Price]) VALUES (@ProductCode,
@Quantity, @Description, @Price)";
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(query, conn))
{
cmd.Parameters.Add("@ProductCode",
System.Data.OleDb.OleDbType.Integer);
cmd.Parameters["@ProductCode"].Value =
this.numericTextBox1.Text;
cmd.Parameters.Add("@Quantity",
System.Data.OleDb.OleDbType.Integer);
cmd.Parameters["@Quantity"].Value =
this.numericUpDown1.Value;
cmd.Parameters.Add("@Description",
System.Data.OleDb.OleDbType.VarChar);
cmd.Parameters["@Description"].Value =
this.textBox3.Text;
cmd.Parameters.Add("@Price",
System.Data.OleDb.OleDbType.Integer);
cmd.Parameters["@Price"].Value = this.textBox4.Text;
cmd.ExecuteNonQuery();
if (choice.comboBox1.Text == "English")
{
System.Media.SoundPlayer sound = new
System.Media.SoundPlayer(@"C:\Windows\Media\Windows
Exclamation.wav");
sound.Play();
DialogResult dialogResult = MessageBox.Show("Added
Successfully!", "Success", MessageBoxButtons.OK);
if (dialogResult == DialogResult.OK)
{
ViewDatabase(sender, e);
ClearTextBoxes(sender, e);
}
}
No comments:
Post a Comment