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

47 views Asked by At

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 .

1

There are 1 answers

1
Anand Sowmithiran On BEST ANSWER

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.