Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Guys, I want to know the difference between i++ and ++i in loop bcz i am using i++ in my project and it's add extra value in database so i need to know the difference between it.

What I have tried:

C#
for (int j = 0; j < dataGridView1.Rows.Count; j++)
                    {
                        connection.Open();
                        OleDbCommand command = new OleDbCommand();
                        command.Connection = connection;
                        command.CommandText = "insert into Total ([Column1],[Column2],[Column3],[Date],[Receipt No],[Delivery Person],[Report],[Flavours],[Return])values('" + dataGridView1.Rows[j].Cells[0].Value.ToString() + "','" + dataGridView1.Rows[j].Cells[1].Value.ToString() + "','" + dataGridView1.Rows[j].Cells[2].Value.ToString() + "','" + dataGridView1.Rows[j].Cells[4].Value.ToString() + "','" + label2.Text + "','" + "---" + "'," + dataGridView1.Rows[j].Cells[0].Value.ToString() + ",'" + dataGridView1.Rows[j].Cells[3].Value.ToString() + "','" + textBox66.Text + "');";
                        command.ExecuteNonQuery();
                        connection.Close();
                    }

                    MessageBox.Show("Inserted Sucessfully", "Database", MessageBoxButtons.OK, MessageBoxIcon.Information);
Posted
Updated 31-Oct-16 10:28am
Comments
Richard Deeming 31-Oct-16 16:15pm    
You've got bigger problems than that!

Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
Member 9983063 31-Oct-16 16:32pm    
sir please guide me what i am doing wrong in my code please tell me i need to use i++ or ++i

You probably have the AllowUserToAddRows[^] property set to true, and you're inserting the values from the empty "insert" row. Check the IsNewRow[^] property before trying to insert the values.

You'll also want to fix the SQL Injection[^] vulnerability in your code.

And it would be best to open the connection once, rather than opening and closing it for every row.
C#
const string Query = @"INSERT INTO Total ([Column1], [Column2], [Column3], [Date], [Receipt No], [Delivery Person], [Report], [Flavours], [Return])
VALUES (@Column1, @Column2, @Column3, @Date, @ReceiptNo, @DeliveryPerson, @Report, @Flavours, @Return)";

using (var connection = new OleDbConnection("..."))
using (var command = new OleDbCommand(connection, Query))
{
    connection.Open();
    
    for (int j = 0; j < dataGridView1.Rows.Count; j++)
    {
        var row = dataGridView1.Rows[j];
        if (row.IsNewRow) continue;
        
        command.Parameters.Clear();
        command.Parameters.AddWithValue("@Column1", row.Cells[0].Value);
        command.Parameters.AddWithValue("@Column2", row.Cells[1].Value)
        command.Parameters.AddWithValue("@Column3", row.Cells[2].Value);
        command.Parameters.AddWithValue("@Date", row.Cells[4].Value);
        command.Parameters.AddWithValue("@ReceiptNo", label2.Text);
        command.Parameters.AddWithValue("@DeliveryPerson", "---");
        command.Parameters.AddWithValue("@Report", row.Cells[0].Value);
        command.Parameters.AddWithValue("@Flavours", row.Cells[3].Value);
        command.Parameters.AddWithValue("@Return", textBox66.Text);
        
        command.ExecuteNonQuery();
    }
}


Once you've fixed that, do yourself a favour and give your controls meaningful names, instead of accepting the default name that Visual Studio provides. You might remember what textBox66 means now, but when you come back to your code in a few months, you won't have a clue.
 
Share this answer
 
v2
Comments
Member 9983063 31-Oct-16 16:41pm    
sir thank you for your answer and sir now i just need to ask a question sir my problem is when i insert 2 items in database so database show me 3 items so your this code is the solution of this problem?
Richard Deeming 31-Oct-16 17:01pm    
Probably. As I said, I suspect you're inserting an extra row for the blank "insert" row at the bottom of the grid. Checking the IsNewRow property should resolve that.
Member 9983063 31-Oct-16 18:33pm    
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
var row = dataGridView1.Rows[j];
if (row.IsNewRow)
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "insert into Total ([Column1],[Column2],[Column3],[Date],[Receipt No],[Delivery Person],[Report],[Flavours],[Return])values('" + dataGridView1.Rows[j].Cells[0].Value.ToString() + "','" + dataGridView1.Rows[j].Cells[1].Value.ToString() + "','" + dataGridView1.Rows[j].Cells[2].Value.ToString() + "','" + dataGridView1.Rows[j].Cells[4].Value.ToString() + "','" + label2.Text + "','" + "---" + "'," + dataGridView1.Rows[j].Cells[0].Value.ToString() + ",'" + dataGridView1.Rows[j].Cells[3].Value.ToString() + "','" + textBox66.Text + "');";
command.ExecuteNonQuery();
connection.Close();
}
else
{
MessageBox.Show("abc");
}
sir this is right?
Richard Deeming 1-Nov-16 8:56am    
No, that is not correct.

Apart from the fact that you're only inserting a row for the blank "insert" row, rather than excluding that row, you've also ignored all other suggestions. Your code is STILL vulnerable to SQL Injection. You are STILL opening and closing the connection for every row.

I have already given you the correct code in my answer. Use that.
Member 9983063 1-Nov-16 19:05pm    
hmmm thnx again sir,now i corrected my query i just want to show you please tell me now it's ok

OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = @"INSERT INTO Total ([Column1], [Column2], [Column3], [Date], [Receipt No], [Delivery Person], [Report], [Flavours], [Return])
VALUES (@Column1, @Column2, @Column3, @Date, @ReceiptNo, @DeliveryPerson, @Report, @Flavours, @Return)";
connection.Open();
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
var row = dataGridView1.Rows[j];
if (row.IsNewRow) continue;

command.Parameters.Clear();
command.Parameters.AddWithValue("@Column1", row.Cells[0].Value);
command.Parameters.AddWithValue("@Column2", row.Cells[1].Value);
command.Parameters.AddWithValue("@Column3", row.Cells[2].Value);
command.Parameters.AddWithValue("@Date", row.Cells[4].Value);
command.Parameters.AddWithValue("@ReceiptNo", label2.Text);
command.Parameters.AddWithValue("@DeliveryPerson", "---");
command.Parameters.AddWithValue("@Report", row.Cells[0].Value);
command.Parameters.AddWithValue("@Flavours", row.Cells[3].Value);
command.Parameters.AddWithValue("@Return", textBox66.Text);

command.ExecuteNonQuery();
} connection.Close();
MessageBox.Show("Inserted Sucessfully", "Database", MessageBoxButtons.OK, MessageBoxIcon.Information);
The difference is in the order of operations:

  • i++, returns the value of i and then increments
  • ++i, increments first and then returns the value of i


However, as pointed out you should fix the query to use OleDbParameter Class (System.Data.OleDb)[^] . Currently the most likeliest error source is the concatenation of values directly to the SQL statement which enables SQL injections, introduces conversion problems and so on.

After that the next steps should be:
- utilize using Statement (C# Reference)[^] to dispose the objects properly
- Open the connection first, do all the operations, then close the connection. You're currently doing vice versa
- Use transactions to ensure that everything succeeds or is rolled back
 
Share this answer
 
v2
Comments
Member 9983063 31-Oct-16 16:32pm    
sir please guide me what i am doing wrong in my code please tell me i need to use i++ or ++i
Wendelius 31-Oct-16 16:40pm    
Have a look at the example @RichardDeeming wrote. It's a good example how you should write the code. As far as I can see using i++ or ++i is not the problem so why not let it be as-is.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900