System.IndexOutOfRangeException

时间:2015-11-19 06:28:25

标签: c# asp.net sql-server error-handling

我想在文本框中显示数据库中的一些列。运行代码时,@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();

请帮助我找到解决这个问题的方法。谢谢。

2 个答案:

答案 0 :(得分:0)

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

您可以将@用于参数化查询中添加的参数(示例中没有参数)。要像您一样从列中检索值,您不应该使用@,只能使用列的简单名称。

答案 1 :(得分:0)

请在下面找到正确的代码。在从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();