捕捉异常。使用存储过程错误

时间:2015-06-15 05:53:43

标签: c# sql

我一直致力于一个Web应用程序项目,在将所有SQL命令更改为存储过程以确保安全性时,此代码突然开始捕获异常。我尝试在catch语句上放置调试点,它显示

  

"索引超出了数组的范围。"

我不知道如何解决这个问题。

try
{
    string tw1 = TextBoxTowerNo.Text;
    LandView.ActiveViewIndex = 0;
    int anInteger = Convert.ToInt32(TextBoxTowerNo.Text);
    int anInteger1 = anInteger - 1;
    int anInteger2 = anInteger + 1;
    lblTowerNo.Text = tw1;

    string connetionString = System.Configuration.ConfigurationManager
                                   .ConnectionStrings["constr"].ConnectionString; ;
    SqlConnection cnn = new SqlConnection(connetionString);
    String sql = "usp_select_tower_data";             
    cnn.Open();
    SqlCommand command = new SqlCommand(sql, cnn);
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.Add("@tower", SqlDbType.NVarChar, 50,"username").Value = tw1;
    SqlDataReader dataReader = command.ExecuteReader();

    if (dataReader.HasRows)
    {
        while (dataReader.Read())
        {
            tower.Text = dataReader.GetValue(0).ToString();
            latitude.Text = dataReader.GetValue(1).ToString();
            longitude.Text = dataReader.GetValue(2).ToString();
            survey.Text = dataReader.GetValue(3).ToString();
            police.Text = dataReader.GetValue(7).ToString();
            agri.Text = dataReader.GetValue(8).ToString();
            village.Text = dataReader.GetValue(4).ToString();
            taluka.Text = dataReader.GetValue(5).ToString();
            district.Text = dataReader.GetValue(6).ToString();
            HiddenLatt.Value = dataReader.GetValue(1).ToString();
            HiddenLongi.Value = dataReader.GetValue(2).ToString();
        }
    }
    else
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), 
            "alertMessage", "alert('Please enter a correct tower number')", true);
        LandView.ActiveViewIndex = -1;
    }        
}
catch (Exception ex)
{
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), 
        "alertMessage", "alert('error while getting data ')", true);
    LandView.ActiveViewIndex = -1;
}

0 个答案:

没有答案