How to avoid duplicating when inserting both lines at once C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MAROUA
    New Member
    • Mar 2017
    • 2

    How to avoid duplicating when inserting both lines at once C#

    Hello friends first I will explain the state after I will give you my need
    I have a datagridview with a checkbox I check the lines after I click on the "save" button and normally the lines I check they will have to be inserted both in the other table but I find that just one line that was Insert twice
    The second problem is that I want the lines I inserted I want the most to see when I click on the search button because there are already assigned in the table
    You will find the two interfaces that process the state
    I hope I have described my condition well and thank you in advance








    script search button

    private void button4_Click(o bject sender, EventArgs e)
    {
    dataGridView2.R ows.Clear();
    Program.cmd.Com mandText = "select * from bon_reception_m arche where Date_reception between '" + dateTimePicker1 .Value.Date + "' and '" + dateTimePicker2 .Value.Date + "' and Id_marche in (select TOP 1 Id_marche from marche where Num_marche = '"+textBox1.Tex t+"')";
    Program.dr = Program.cmd.Exe cuteReader();
    while (Program.dr.Rea d())
    {
    dataGridView2.R ows.Add(Program .dr[0],Program.dr[2], Program.dr[3], Program.dr[5], Program.dr[6], Program.dr[7], Program.dr[8], Program.dr[9], Program.dr[10], Program.dr[11], Program.dr[12]);
    }
    Program.dr.Clos e();
    }


    script click datagridview

    private void dataGridView2_C ellClick(object sender, DataGridViewCel lEventArgs e)
    {
    if (e.ColumnIndex == 11/*myColumn*/ && e.RowIndex >= 0 /*myRow*/)
    {
    button1.Enabled = true;
    }
    }





    script button save :


    private void button1_Click(o bject sender, EventArgs e)
    {
    int colIndex = dataGridView2.C olumns["CheckBox"].Index;
    try
    {
    var rows = dataGridView2.R ows
    .Cast<DataGridV iewRow>()
    .Where(row => row.Cells[colIndex].Value != null)
    .Where(row => (bool)row.Cells[colIndex].Value)
    .ToList();
    foreach (DataGridViewRo w row in rows)
    insertRowData(r ow);
    MessageBox.Show ("c'est ajouté avec succés");
    }
    catch (FormatExceptio n)
    {
    MessageBox.Show ("Only input numbers into the table!",
    "Only Numbers", MessageBoxButto ns.OK);
    }
    catch (Exception)
    {
    MessageBox.Show ("There was an error while saving!",
    "Error", MessageBoxButto ns.OK);
    }
    }
    private void insertRowData(D ataGridViewRow row)
    {
    double montantValue = Convert.ToDoubl e(row.Cells["Column7"].Value);
    int id_br_value = Convert.ToInt32 (row.Cells["Column11"].Value);
    string check;
    if (checkBox1.Chec ked == true)
    {
    check = "O";
    }
    else
    {
    check = "N";
    }
    Program.cmd.Par ameters.Clear() ;
    Program.cmd.Com mandText = "insert into attachement_mar che (Id_bon_recepti on_marche,Id_ma rche,Num_attach ement,Date_debu t,Date_fin,Flag _dernier,Montan t,User_create,D ate_create) values ( " + id_br_value + ",(select TOP 1 Id_marche from marche where Num_marche = '" + textBox1.Text + "'),'" + textBox3.Text + "','" + dateTimePicker1 .Value.Date + "','" + dateTimePicker1 .Value.Date + "','" + check + "'," + montantValue + ",'" + values.username + "','" + DateTime.Now.Da te + "')";
    Program.cmd.Exe cuteNonQuery();
    }
  • MAROUA
    New Member
    • Mar 2017
    • 2

    #2

    Comment

    Working...