立即运行循环。如果。 [英] Run loop at once. In case.

查看:80
本文介绍了立即运行循环。如果。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的datagrid有两列多行。



但是在这段代码中,循环只运行一次。为什么?

I have datagrid with two columns with multiple rows.

But in this code the loop runs only once. Why?

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

推荐答案

不是您问题的解决方案,而是您遇到的另一个问题。

永远不要通过以下方式构建SQL查询连接字符串。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

-----

你的循环在 txtEnter 中每行循环一次,只要 txtEnter 未清除。

可能你不应该这个

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
txtEnter.Rows.Clear();


循环中的


inside the loop.


正如 Michael_Davies 所述,陈述

As already noted by Michael_Davies, the statement
txtEnter.Rows.Clear();

在循环块结束时,停止迭代。

at the end of the loop block, stops the iteration.


这篇关于立即运行循环。如果。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆