Hello,
I have the code for deleting a record from a table
through bindNavigator Delete button. My problem is when i click on
delete button it deletes the previous record.
How can i delete the correct record?
Thank you
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Kirtan PatelRank #292 · 8.6K reputationPosted Oct 18, 2009, 9:18 PM EDT
Problem Solved :)
I Changed The connection String ..in code to connect it with my server so change it :)
and One more thing when you delete record you need to first delete Sub Records of Order Table First :) may be it will
cause exception while deleting .
Problem was occurring because when we click on delete Button the Contex of dataBinding Moving to Previous DataRecord ..
but it was just logically deleted the Item But Not Physically deletes the Item
so i have captured the value in TextBox before it movies to previous Record Context.
Corrected Project Files(Updated..)
dont forget to mark "do you like this answer" please :)
Roei BarRank #1,271 · 1.4K reputationPosted Sep 2, 2009, 1:58 AM EDT
you delete one record and upon deleting the other you delete the prevoius
meaning you need to rebind the BindingNavigator ;-)
DealyRank #9,518 · 205 reputationPosted Sep 2, 2009, 2:02 AM EDT
Roei BarRank #1,271 · 1.4K reputationPosted Sep 2, 2009, 3:04 AM EDT
Kirtan PatelRank #292 · 8.6K reputationPosted Sep 2, 2009, 3:16 AM EDT
Can you Provide me the code you are using to delete a Record I will Provide you Correct code :)
DealyRank #9,518 · 205 reputationPosted Sep 2, 2009, 4:31 AM EDT
if (MessageBox.Show("Are you sure you want;", "Confirm Deletion", MessageBoxButtons.OKCancel) == DialogResult.OK) { string connectionString = GetConnection(); SqlConnection conn = new SqlConnection(connectionString); SqlCommand command = new SqlCommand("DELETE_COMMAND", conn); command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@C_IDENTITY", SqlDbType.Int).Value = Convert.ToInt32(c_identity_txt.Text); conn.Open(); int rows = command.ExecuteNonQuery(); conn.Close(); if (rows > 0) MessageBox.Show("Success"); else MessageBox.Show("Failure"); } else MessageBox.Show("Cancelled");Kirtan PatelRank #292 · 8.6K reputationPosted Sep 2, 2009, 5:09 PM EDT
if Still Having Problem then you are having problem with your stored Procedure
if (MessageBox.Show("Are you sure you want;", "Confirm Deletion", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
string connectionString = GetConnection();
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("DELETE_COMMAND", conn);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@C_IDENTITY", c_identity_txt.Text);
conn.Open();
int rows = command.ExecuteNonQuery();
conn.Close();
if (rows > 0)
{
MessageBox.Show("Success");
}
else
{
MessageBox.Show("Failure");
}
}
else
{
MessageBox.Show("Cancelled");
}
Master BillaRank #2,229 · 630 reputationPosted Sep 2, 2009, 6:55 PM EDT
You can use my code
private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e) { if (MessageBox.Show("Sure you wanna delete?", "Warning", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) bindingSource1.RemoveCurrent(); }DealyRank #9,518 · 205 reputationPosted Sep 2, 2009, 9:16 PM EDT
still having the same problem so i would appreciate if you could check my code. Here's the code for Nortwind example:
private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e) { if (MessageBox.Show("Delete?", "Confirm Delete", MessageBoxButtons.OKCancel) == DialogResult.OK) { string connectionString = @"Server=local; Initial Catalog=Northwind; Integrated Security=True"; using (SqlConnection conn = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand("DELETE_ORDER", conn); command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@OrderID", orderID_txt.Text); conn.Open(); int rows = command.ExecuteNonQuery(); conn.Close(); if (rows > 0) MessageBox.Show("Success"); else MessageBox.Show("Failed"); } } else MessageBox.Show("Abort"); }CREATE PROCEDURE DELETE_ORDER (@OrderID INT) AS DELETE FROM Orders WHERE OrderID=@OrderID GOThank you again.
Kirtan PatelRank #292 · 8.6K reputationPosted Oct 18, 2009, 5:31 AM EDT
Its Seems no Problem with code but if you can provide me Project Files then i could quickly resolve problem
I have Northwind Database please send me Only Project Files(zipped) at [email protected] :)
I will Quickly resolve your problem :)
Thank you :)
DealyRank #9,518 · 205 reputationPosted Oct 18, 2009, 11:09 PM EDT
One more question... is it possible to change record after the confirmation of delete, not as soon as the user clicks on delete?
Kirtan PatelRank #292 · 8.6K reputationPosted Oct 19, 2009, 12:30 AM EDT
what do you mean by changing record ????after deleting ??
DealyRank #9,518 · 205 reputationPosted Oct 19, 2009, 3:20 AM EDT
Kirtan PatelRank #292 · 8.6K reputationPosted Oct 19, 2009, 4:08 AM EDT
Cancel part of dialog box
else if (x == DialogResult.Cancel)
{
MessageBox.Show("Abort");
//Added Code
int z = itmBindNavigator.BindingSource.Position;
button1.PerformClick();
itmBindNavigator.BindingSource.Position = z+1;
}