0

When I delete a record, it works. But when I close the program and open it again, actually the record is not deleted.

private void button5_Click(object sender, EventArgs e) 
{
        SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Upper_Stage_Data.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
        con.Open();
        MessageBox.Show("Delete Current Row of Database Open");

        if (MaxRows != 0)
        {
            SqlCommand Command = new SqlCommand("DELETE FROM Table1 WHERE Data_no='@inc'", con);

            try
            {
                Command.ExecuteNonQuery();
                MessageBox.Show("Record Deleted");
            }
            catch (SqlException)
            {
                MessageBox.Show("ERROR: Could not delete record");
            }
            MaxRows--;
            if (inc != 0)
            {
                inc--;
                NavigationRecords();
            }
            else
            {
                if (MaxRows != 1)
                {
                    inc++;
                    NavigationRecords();
                }
                else
                {
                    MessageBox.Show("No Record");
                }
            }
        }
        else
        {
            MessageBox.Show("No Record");
        }

        con.Close();
        MessageBox.Show("Delete Current Row of Database Closed");
        con.Dispose();
}

When I searched, some records can not be deleted because they are "master" record. How can I delete the record TRULY?

1
  • Could it be that each time you compile, a fresh copy (with the row present) is copied to the output directory and used by your program? Commented Oct 10, 2015 at 17:57

1 Answer 1

3

you don't need quotes around '@inc':

SqlCommand Command = new SqlCommand("DELETE FROM Table1 WHERE Data_no=@inc", con);

Also you needs to provide the value for the @inc parameter too.

Command.Parameters.AddWithValue("@inc", inc); //inc is a variable that contain a value for the @inc parameter

Although specify the type directly and use the Value property is more better than AddWithValue. Check this for more details: Can we stop using AddWithValue() already?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.