Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have datagrid with two columns with multiple rows.

But in this code the loop runs only once. Why?
C#
private void btnsave_Click(object sender, EventArgs e)
{
    if (txtledger.Text.Length == 0)
    {
        MessageBox.Show("Fill all Infomation", "Exclamation!!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
    else
    {

        try
        {
            for (int i = 0; i < txtEnter.Rows.Count; i++)
            {
                par = txtEnter.Rows[i].Cells[0].Value.ToString();
                amount1 = txtEnter.Rows[i].Cells[1].Value.ToString();
                Comparbal();

                quary = "INSERT INTO `account book` (`Traction Date`,Ledger,Particulars,`Vch Type`,Debits,Credits,FY,Narration)VALUES ('" + Vdate.Value.ToString("yyyy/MM/dd") + "','" + par + "','" + txtledger.Text + "','Payment'," + amount1 + ",0,'" + CFY + "','" + txtnar.Text + "')";
                Connection.executenonquery(quary);

                quary = "INSERT INTO `account book` (`Traction Date`,Ledger,Particulars,`Vch Type`,Debits,Credits,FY,Narration)VALUES ('" + Vdate.Value.ToString("yyyy/MM/dd") + "','" + txtledger.Text + "','" + par + "','Payment',0," + amount1 + ",'" + CFY + "','" + txtnar.Text + "')";
                Connection.executenonquery(quary);

                metroLabel2.Text = "Accounting Voucher:-" + (id + 1).ToString();
                txtledger.SelectedIndex = -1;
                txtEnter.CancelEdit();
                particuars.Dispose();
                amount.Dispose();
                txtnar.Clear();
                metroLabel2.Text = "";
                txtEnter.Rows.Clear();


            }
            MessageBox.Show("Vouchar Post");
        }
        catch
        {
            MessageBox.Show("Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}


What I have tried:

Thanks in advance. I want to make an account software. This code belongs to Payment Voucher
Posted
Updated 31-Jul-17 20:39pm
v2
Comments
Graeme_Grant 1-Aug-17 1:07am    
What happens when the button is clicked? Have you set a breakpoint at the top of the function and stepped through your code to see?
StM0n 1-Aug-17 1:08am    
My guess would be, that txtEnter.Rows.Count is 1... have you checked?
Michael_Davies 1-Aug-17 1:44am    
What does this do?

txtEnter.Rows.Clear();

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
-----
Your loop loops once per row in txtEnter as long as txtEnter is not cleared.
May be you should not have this
C#
txtEnter.Rows.Clear();

inside the loop.
 
Share this answer
 
v2
As already noted by Michael_Davies, the statement
C#
txtEnter.Rows.Clear();
at the end of the loop block, stops the iteration.
 
Share this answer
 

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