1

I have been working on a web application project and while changing all the SQL commands to stored procedures for security, this code suddenly started catching the exception. I tried placing debug point on the catch statement and it shows

"Index was outside the bounds of the array."

I don't know how to solve this.

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;
}
leppie
  • 112,162
  • 17
  • 191
  • 293
  • 2
    The error message is self-explanatory. Which line does it happen on? You are missing a bunch of using-blocks. – nvoigt Jun 15 '15 at 06:00
  • Try [Break when exceptions are thrown](https://msdn.microsoft.com/en-us/library/d14azbfh.aspx) and see what exactly causing the issue, also validate number of columns returned by your `procedure` (As per code columns should be 9) . – Hari Prasad Jun 15 '15 at 06:00
  • It's hard to see without knowing the SQL.. But I would suggest using column names to access datareader columns rather than their indices as its clearer and helps in these kinds of situations. Sql profiler is also a handy tool for troubleshooting – ne1410s Jun 15 '15 at 06:07
  • You should think about your code. Use more using-blocks like nvoigt said to avoid open connection. You also should think about output parameter. This would make you code more readable and it is easy to handle. look at the accepted answer from http://stackoverflow.com/questions/11292940/stored-procedure-output-parameter-asp-net-c-sharp question. – Tjasun Jun 15 '15 at 06:18
  • im not using the "using" blocks because it is not going over the internet..it is used in a private company server. – Ishaan shringi Jun 15 '15 at 06:24
  • With that code every time you run into an (sql) exception your connection to the database is not closed an still open. If you reach the limit of the connections of you sql server you always get an connection error. That is not an internet vs. intranet issue. – Tjasun Jun 15 '15 at 06:26
  • You need to check if stored procedure is returning values. Second better if you fetch values using syntax reader["Column_Name"]; – Amit Jun 15 '15 at 06:30
  • the procedure contains this command ---> SELECT [from1] ,[to1] ,[tower] ,[village] ,[description] ,[id] FROM [dbo].[tower] where tower = @tower order by tower – Ishaan shringi Jun 15 '15 at 06:30
  • 2
    Thats you fault. You select 6 columns an try to get dataReader.GetValue(8). So you try to get the value of column 9. – Tjasun Jun 15 '15 at 06:31
  • i tried "break when exception is thrown". and it stopped at the line "police.text=...." – Ishaan shringi Jun 15 '15 at 06:33
  • That is correct. You just can get values from 0 up to 5 with your statement. Just comment out the others and your code will run without index out of bounce. Or edit you sql statement to SELECT [from1] ,[to1] ,[tower] ,[village] ,[description] ,[id], '', '', '' FROM [dbo].[tower] where tower = @tower order by tower – Tjasun Jun 15 '15 at 06:36
  • is that because im selecting only 6 values from the table so i can use 0 to 5? @Tjasun – Ishaan shringi Jun 15 '15 at 06:38
  • That's the point. Extend the sql statement: SELECT [from1] ,[to1] ,[tower] ,[village] ,[description] ,[id], '', '', '' FROM [dbo].[tower] where tower = @tower – Tjasun Jun 15 '15 at 06:40
  • okay..it is working..thank you so much for ur help :) – Ishaan shringi Jun 15 '15 at 06:41
  • BTW, `using` blocks have nothing to do with whether you're using the Internet or not. You should _always_ put using blocks in these cases. – John Saunders Jun 17 '15 at 07:35

0 Answers0