简体   繁体   中英

update in ms access using c#

can someone please help what is wrong with my code? it is a update function and during my debug it executing properly but it is not updating my database. I already search for an answer to for this problem but still it didn't work. i also try to create a new database hoping that it is problem but still no effect.

private void update_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
OleDbConnection con = new OleDbConnection();
con.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\sherilyn & justine\Documents\Visual Studio 2015\Projects\jollibee4\jollibee4\jollibee.accdb";
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
String id = dataGridView1.SelectedRows[0].Cells[0].Value + String.Empty;
int id1 = Int32.Parse(id);
try
{
if (database.selectedIndex == 0)
{
cmd.CommandText = "update Breakfast_Meals set [Meals] = @meals, [Price] = @price, [Picture] = @picture, [Description] = @description WHERE [Item ID]=@id";
}
else if (database.selectedIndex == 1)
{
cmd.CommandText = "update Burger_Sandwhich_Meals set [Meals] = @meals, [Price] = @price, [Picture] = @picture, [Description] = @description WHERE [Item ID]=@id";
}
else if (database.selectedIndex == 2)
{
cmd.CommandText = "update Chicken_Meals set [Meals] = @meals, [Price] = @price, [Picture] = @picture, [Description] = @description WHERE [Item ID]=@id";
}
else if (database.selectedIndex == 3)
{
cmd.CommandText = "update Dessert set [Meals] = @meals, [Price] = @price, [Picture] = @picture, [Description] = @description WHERE [Item ID]=@id";
}
else if (database.selectedIndex == 4)
{
cmd.CommandText = "update Kids_Meals set [Meals] = @meals, [Price] = @price, [Picture] = @picture, [Description] = @description WHERE [Item ID]=@id";
}
else if (database.selectedIndex == 5)
{
cmd.CommandText = "update RiceMeals_NoodlesMeals set [Meals] = @meals, [Price] = @price, [Picture] = @picture, [Description] = @description WHERE [Item ID]=@id";
}
else if (database.selectedIndex == 6)
{
cmd.CommandText = "update Side_Items set [Meals] = @meals, [Price] = @price, [Picture] = @picture, [Description] = @description WHERE [Item ID]=@id";
}
else if (database.selectedIndex == 7)
{
cmd.CommandText = "update Value_Meals set [Meals] = @meals, [Price] = @price, [Picture] = @picture, [Description] = @description WHERE [Item ID]=@id";
}
cmd.Parameters.AddWithValue("@id", id1);
cmd.Parameters.AddWithValue("@meals", meal.Text);
int mealPrice = Int32.Parse(price.Text);
cmd.Parameters.AddWithValue("@price", mealPrice);
cmd.Parameters.AddWithValue("@picture", savePhoto());
cmd.Parameters.AddWithValue("@description",description.Text);
DialogResult dialogResult = MessageBox.Show("Are you sure you want to change the data?","Warning", MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
if (dialogResult == DialogResult.Yes)
{
cmd.ExecuteNonQuery();
con.Close();
}
else if (dialogResult == DialogResult.No)
{
con.Close();
}
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(dt);
dataGridView1.DataSource = dt;
database_onItemSelected(sender, e);//to view dgv data for the selected index
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,MessageBoxIcon.Error);
con.Close();
}
}

Add the parameters in the correct order as expected by the placeholders

cmd.Parameters.AddWithValue("@meals", meal.Text);
int mealPrice = Int32.Parse(price.Text);
cmd.Parameters.AddWithValue("@price", mealPrice);
cmd.Parameters.AddWithValue("@picture", savePhoto());
cmd.Parameters.AddWithValue("@description",description.Text);
cmd.Parameters.AddWithValue("@id", id1);

OleDb doesn't resolve parameters values by their name, but by the parameter's position in the parameters collection. With your order the id condition in the where clause receives the value from the description parameter.

Consider also to use Add instead of AddWithValue

See: Can we stop using AddWithValue already?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM