简体   繁体   中英

My application stores data but it duplicates it in the database .what can do?

When I click on Add it shows data added successfully, but when I go to the Microsoft Access database I find that the text value is duplicated.

Here is the Add Function:

public string Add()
{ 
    string insert = String.Format("Insert into profile Values ('{0}','{1}')", FIRSTname, LASTname);
    RunDml(insert);
    if(RunDml(insert) == true)
    {
        return "Data added seccessfully !";
    }
    else
    {
        return "Retry Again Please !";
    }
}

AND HERE IS THE RunDml FUNCTION:

public bool RunDml(string statmente)
{
    try
    {
        Connect();
        cmd = new OleDbCommand(statmente, conn);
        cmd.ExecuteNonQuery();
        Disconnect();
        return true;
    }
    catch (OleDbException ex)
    {
        ErrorCode = ex.ErrorCode;
        ErrorMessage = ex.Message;
        return false;
    }
}

AND THIS IS THE ADD BOTTON CLICK:

private void Add_button_Click(object sender, EventArgs e)
{
    Person Prs1 = new Person();
    Prs1.FIRSTname = Firstname_textBox.Text;
    Prs1.LASTname = Lastname_textBox.Text;
    MessageBox.Show(Prs1.Add());
    Firstname_textBox.Text = "";
    Lastname_textBox.Text = "";

}

What can I do? every time i try it is the same result.

In your Add() function, you are calling the function twice.

     RunDml(insert);
     if(RunDml(insert) == true)

And, that is why it is getting executed twice and you end up with duplicate record. Remove the line above the if condition.

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