Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 捕捉异常。使用存储过程时出错_C#_Sql - Fatal编程技术网

C# 捕捉异常。使用存储过程时出错

C# 捕捉异常。使用存储过程时出错,c#,sql,C#,Sql,我一直在从事一个web应用程序项目,为了安全起见,将所有SQL命令更改为存储过程时,这段代码突然开始捕获异常。我尝试将调试点放在catch语句上,结果显示 “索引超出了数组的边界。” 我不知道怎么解决这个问题 try { string tw1 = TextBoxTowerNo.Text; LandView.ActiveViewIndex = 0; int anInteger = Convert.ToInt32(TextBoxTowerNo.Text); int a

我一直在从事一个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;
}

错误消息是不言自明的。发生在哪一行?您缺少一组using-BLOCK。请尝试查看问题的确切原因,并验证
过程返回的列数(根据代码,列数应为9)。如果不知道SQL,则很难看到。。但我建议使用列名来访问datareader列,而不是它们的索引,因为在这种情况下更清晰、更有用。Sql事件探查器也是一个方便的故障诊断工具。您应该考虑一下自己的代码。使用更多类似nvoigt的块,以避免打开连接。您还应该考虑输出参数。这将使代码更具可读性,并且易于处理。看看问题的答案,那是你的错。选择6列,尝试获取dataReader.GetValue(8)。因此,您尝试获取第9列的值。错误消息是不言自明的。发生在哪一行?您缺少一组using-BLOCK。请尝试查看问题的确切原因,并验证
过程返回的列数(根据代码,列数应为9)。如果不知道SQL,则很难看到。。但我建议使用列名来访问datareader列,而不是它们的索引,因为在这种情况下更清晰、更有用。Sql事件探查器也是一个方便的故障诊断工具。您应该考虑一下自己的代码。使用更多类似nvoigt的块,以避免打开连接。您还应该考虑输出参数。这将使代码更具可读性,并且易于处理。看看问题的答案,那是你的错。选择6列,尝试获取dataReader.GetValue(8)。因此,您尝试获取第9列的值。错误消息是不言自明的。发生在哪一行?您缺少一组using-BLOCK。请尝试查看问题的确切原因,并验证
过程返回的列数(根据代码,列数应为9)。如果不知道SQL,则很难看到。。但我建议使用列名来访问datareader列,而不是它们的索引,因为在这种情况下更清晰、更有用。Sql事件探查器也是一个方便的故障诊断工具。您应该考虑一下自己的代码。使用更多类似nvoigt的块,以避免打开连接。您还应该考虑输出参数。这将使代码更具可读性,并且易于处理。看看问题的答案,那是你的错。选择6列,尝试获取dataReader.GetValue(8)。因此,您尝试获取第9列的值。