System.IndexOutOfRangeException

本文关键字:IndexOutOfRangeException System | 更新日期: 2023-09-27 18:22:38

我想在文本框中显示数据库中的一些列。当我运行代码时,@ApplicantUsername 上出现错误

IndexOutOfRangeException

这是我的代码:

        cs.Open();
        SqlCommand cmd = new SqlCommand("SELECT ApplicantUsername, Branch, DescriptionOfHouse, DescritpionOfHouseType, LivingCondition, MembersOfHousehold, RealProperty, PersonalProperty, Collaterals, PurposeOfLoan, LoanDesired, MaxLoanVal, PromisoryNoteVal, MonthlyInstallment1, Terms, TotalMonthlyIncome, TotalMonthlyExpenses, Class, NarrativeReport FROM CustomerCreditReport WHERE ApplicantUsername = '" + uname + "'", cs);
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            txtUsername.Text = reader["@ApplicantUsername"].ToString();
            txtBranch.Text = reader["@Branch"].ToString();
            txtDesc.Text = reader["@DescriptionOfHouse"].ToString();
            txtOAR.Text = reader["@DescritpionOfHouseType"].ToString();
            txtCond.Text = reader["@LivingCondition"].ToString();
            txtMem.Text = reader["@MembersOfHousehold"].ToString();
            txtReal.Text = reader["@RealProperty"].ToString();
            txtPersonal.Text = reader["@PersonalProperty"].ToString();
            txtCollateral.Text = reader["@Collaterals"].ToString();
            txtPurpose.Text = reader["@PurposeOfLoan"].ToString();
            txtDesired.Text = reader["@LoanDesired"].ToString();
            txtMLV.Text = reader["@MaxLoanVal"].ToString();
            txtPNV.Text = reader["@PromisoryNoteVal"].ToString();
            txtMI.Text = reader["@MonthlyInstallment1"].ToString();
            txtTerms.Text = reader["@Terms"].ToString();
            txtTotIncome.Text = reader["@TotalMonthlyIncome"].ToString();
            txtTotExpenses.Text = reader["@TotalMonthlyExpenses"].ToString();
            txtClass.Text = reader["@Class"].ToString();
            txtNarrative.Text = reader["@NarrativeReport"].ToString();
        }
        cs.Close();

你能帮我想办法解决这个问题吗。非常感谢。

System.IndexOutOfRangeException

从所有这些字段名称中删除@

@用于添加到参数化查询中的参数(示例中没有参数)。要像现在这样从列中检索值,不应该使用@,只应该使用列的普通名称。

请在下面找到正确的代码。从datareader读取数据时不应使用@。

    cs.Open();
    SqlCommand cmd = new SqlCommand("SELECT ApplicantUsername, Branch, DescriptionOfHouse, DescritpionOfHouseType, LivingCondition, MembersOfHousehold, RealProperty, PersonalProperty, Collaterals, PurposeOfLoan, LoanDesired, MaxLoanVal, PromisoryNoteVal, MonthlyInstallment1, Terms, TotalMonthlyIncome, TotalMonthlyExpenses, Class, NarrativeReport FROM CustomerCreditReport WHERE ApplicantUsername = '" + uname + "'", cs);
    SqlDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        txtUsername.Text = reader["ApplicantUsername"].ToString();
        txtBranch.Text = reader["Branch"].ToString();
        txtDesc.Text = reader["DescriptionOfHouse"].ToString();
        txtOAR.Text = reader["DescritpionOfHouseType"].ToString();
        txtCond.Text = reader["LivingCondition"].ToString();
        txtMem.Text = reader["MembersOfHousehold"].ToString();
        txtReal.Text = reader["RealProperty"].ToString();
        txtPersonal.Text = reader["PersonalProperty"].ToString();
        txtCollateral.Text = reader["Collaterals"].ToString();
        txtPurpose.Text = reader["PurposeOfLoan"].ToString();
        txtDesired.Text = reader["LoanDesired"].ToString();
        txtMLV.Text = reader["MaxLoanVal"].ToString();
        txtPNV.Text = reader["PromisoryNoteVal"].ToString();
        txtMI.Text = reader["MonthlyInstallment1"].ToString();
        txtTerms.Text = reader["Terms"].ToString();
        txtTotIncome.Text = reader["TotalMonthlyIncome"].ToString();
        txtTotExpenses.Text = reader["TotalMonthlyExpenses"].ToString();
        txtClass.Text = reader["Class"].ToString();
        txtNarrative.Text = reader["NarrativeReport"].ToString();
    }
    cs.Close();