C#迭代文本框并更新值

时间:2015-03-19 12:12:03

标签: c# loops textbox

前言......我根本没有在C#中发展,所以我对最佳实践的批评持开放态度。

我有一个包含大约70组文本框的表单,这些文本框分组在6个组框中。我有一个方法,从两个文本框中获取值,并从数据库返回一些值。每组文本框都包含以下迭代:

org1...org70
app1...app70
orgDesc1...orgDesc70
status1...status70
sent1...sent1
error1...error1

我在下面的代码中尝试做的是从第1组开始并遍历第20组(第一个组框),每次将org和app传递给我的查询方法并返回4个值以填充orgDesc,状态,发送,错误。

我查看过其他帖子,我试图让一些人工作,但无济于事。我发布了我目前作为迭代的内容,但在运行查询时出现null错误的查询方法失败。需要注意的是,如果我静态地定义过程,一切都运行得很完美,但我不希望在循环中完成相同过程的70个版本。

private void Refresh1_Click(object sender, EventArgs e)
{
    int count = 1;
    do
    {
        string org = "org" + count;
        string orgIn = groupBox1.Controls[org].Text;
        string app = "app" + count;
        string appIn = groupBox1.Controls[app].Text;
        string name = "name" + count;
        string stat = "status" + count;
        string Sent = "Sent" + count;
        string error = "error" + count;
        string orgDesc, status, numSent, numError;
        NumSent1(orgIn, appIn, out orgDesc, out status, out numSent, out numError);

        groupBox1.Controls[name].Text = orgDesc;
        groupBox1.Controls[stat].Text = status;
        groupBox1.Controls[Sent].Text = numSent;
        groupBox1.Controls[error].Text = numError;

        count++;
    } while (count < 21);
}

查询方法:

    static void NumSent1(string orgIn, string appIn, out string orgDesc, out string status, out string numSent, out string numError)
    {
        string connString1 = "Server=[IP];Database=Master;Integrated Security=SSPI";

        string query2 = "use SynergyEOM SELECT JobDefID FROM eDistJobs where OrgName = '" + orgIn + "' and App = '" + appIn + "' and Environment = 1";

        SqlConnection Conn = new SqlConnection(connString1);
        SqlCommand Comm2 = new SqlCommand(query2, Conn);

        Conn.Open();

        string JobDef = Comm2.ExecuteScalar().ToString();

        string qryOrgDesc = "use [SynEdistDB] Select Orgdesc " +
                        "From JobInstances "+
                        "join [DB1].[SynAdmDB].[dbo].[OrgDef] db1 on JobInstances.OrgID = db1.OrgID " +
                        "where State in (32,64) and DateStarted > DATEADD(d,-30,GETDATE()) and OrgName = '" + orgIn + "' and JobDefID = '" + JobDef + "'";

        string qryState = "use [SynEdistDB] Select State " +
                        "From JobInstances "+
                        "join [DB1].[SynAdmDB].[dbo].[OrgDef] db1 on JobInstances.OrgID = db1.OrgID " +
                        "where State in (32,64) and DateStarted > DATEADD(d,-30,GETDATE()) and OrgName = '" + orgIn + "' and JobDefID = '" + JobDef + "'";

        string qryNumSent = "use [SynEdistDB] Select NumSent " +
                        "From JobInstances "+
                        "join [DB1].[SynAdmDB].[dbo].[OrgDef] db1 on JobInstances.OrgID = db1.OrgID " +
                        "where State in (32,64) and DateStarted > DATEADD(d,-30,GETDATE()) and OrgName = '" + orgIn + "' and JobDefID = '" + JobDef + "'";

        string qryNumError = "use [SynEdistDB] Select NumError " +
                        "From JobInstances "+
                        "join [DB1].[SynAdmDB].[dbo].[OrgDef] db1 on JobInstances.OrgID = db1.OrgID " +
                        "where State in (32,64) and DateStarted > DATEADD(d,-30,GETDATE()) and OrgName = '" + orgIn + "' and JobDefID = '" + JobDef +"'";

        SqlCommand Comm3 = new SqlCommand(qryOrgDesc, Conn);
        SqlCommand Comm4 = new SqlCommand(qryState, Conn);
        SqlCommand Comm5 = new SqlCommand(qryNumSent, Conn);
        SqlCommand Comm6 = new SqlCommand(qryNumError, Conn);

        orgDesc = Comm3.ExecuteScalar().ToString();
        status = Comm4.ExecuteScalar().ToString();
        numSent = Comm5.ExecuteScalar().ToString();
        numError = Comm6.ExecuteScalar().ToString();

        Conn.Close();
    }

静态工作正常

    private void Refresh1_Click(object sender, EventArgs e)
    {
        string orgIn = org1.Text;
        string appIn = app1.Text;
        string orgDesc, status, numSent, numError;
        NumSent1(orgIn, appIn, out orgDesc, out status, out numSent, out numError);
        name1.Text = orgDesc;
        status1.Text = status;
        Sent1.Text = numSent;
        error1.Text = numError;
    }

0 个答案:

没有答案