473,323 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

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

2
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(object sender, EventArgs e)
{
dataGridView2.Rows.Clear();
Program.cmd.CommandText = "select * from bon_reception_marche 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.Text+"')";
Program.dr = Program.cmd.ExecuteReader();
while (Program.dr.Read())
{
dataGridView2.Rows.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.Close();
}


script click datagridview

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





script button save :


private void button1_Click(object sender, EventArgs e)
{
int colIndex = dataGridView2.Columns["CheckBox"].Index;
try
{
var rows = dataGridView2.Rows
.Cast<DataGridViewRow>()
.Where(row => row.Cells[colIndex].Value != null)
.Where(row => (bool)row.Cells[colIndex].Value)
.ToList();
foreach (DataGridViewRow row in rows)
insertRowData(row);
MessageBox.Show("c'est ajouté avec succés");
}
catch (FormatException)
{
MessageBox.Show("Only input numbers into the table!",
"Only Numbers", MessageBoxButtons.OK);
}
catch (Exception)
{
MessageBox.Show("There was an error while saving!",
"Error", MessageBoxButtons.OK);
}
}
private void insertRowData(DataGridViewRow row)
{
double montantValue = Convert.ToDouble(row.Cells["Column7"].Value);
int id_br_value = Convert.ToInt32(row.Cells["Column11"].Value);
string check;
if (checkBox1.Checked == true)
{
check = "O";
}
else
{
check = "N";
}
Program.cmd.Parameters.Clear();
Program.cmd.CommandText = "insert into attachement_marche (Id_bon_reception_marche,Id_marche,Num_attachement ,Date_debut,Date_fin,Flag_dernier,Montant,User_cre ate,Date_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.Date + "')";
Program.cmd.ExecuteNonQuery();
}
Mar 18 '17 #1
1 1217
MAROUA
2


Mar 18 '17 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Joachim Klassen | last post by:
Hi all, first apologies if this question looks the same as another one I recently posted - its a different thing but for the same szenario:-). We are having performance problems when...
0
by: Nick | last post by:
How do I go about setting a default value for a row when inserting a new record with the DetailsView ? Effectively I need to access the underlying data source and set a column to be a default value...
0
by: Eric | last post by:
Visual C++ 2005 Express MVP's and experience programmer's only please!... I need to get the number of lines in a textbox so I can insert them into a listview. The text comes from my database...
0
by: Eric | last post by:
Visual C++ 2005 Express MVP's and experience programmer's only please!... I need to get the number of lines in a textbox so I can insert them into a listview. The text comes from my database...
0
by: Eric | last post by:
Visual C++ 2005 Express MVP's and experience programmer's only please!... I need to get the number of lines in a textbox so I can insert them into a listview. The text comes from my database...
0
by: Eric | last post by:
Visual C++ 2005 Express MVP's and experience programmer's only please!... I need to get the number of lines in a textbox so I can insert them into a listview. The text comes from my database...
0
by: Eric | last post by:
Visual C++ 2005 Express MVP's and experience programmer's only please!... I need to get the number of lines in a textbox so I can insert them into a listview. The text comes from my database...
0
by: Eric | last post by:
Visual C++ 2005 Express MVP's and experience programmer's only please!... I need to get the number of lines in a textbox so I can insert them into a listview. The text comes from my database...
1
by: DanielLauJJ | last post by:
When inserting a record into a table, I want SQL Server to generate a number automatically for the Primary Key. (e.g. OrderID is 1, 2, 3 and so on) How to do it? (This behavior is similar to the...
7
by: DoubleD | last post by:
Hi, I need some assistance or ideas on ho to group data when inserting to a MS SQL table. I currently have a continuous form that an "overview" of my inventory including vendor details, stock...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.