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;
}