当我想运行此代码时,我收到一个错误,引发" IndexOutOfRangeException" on for循环条件。我试图使用断点,并在我的DataSet填充后单击放大镜。 Visualizer说它是一个空的DataSet。如果有人可以帮助我,我将不胜感激!
我的PageLoad功能包括if(!IsPostBack)
,如下所示:(freeQuery.aspx.cs)
if (!IsPostBack
{
ViewState["sortColumn"] = " ";
ViewState["sortDirection"] = " ";
string pageURL = HttpContext.Current.Request.RawUrl;
DataSet tables = new DataSet();
string queryString2 = "show tables;";
tables = connectHive(queryString2, pageURL);
List<string> list = new List<string>();
for (int i = 0; i < tables.Tables[0].Rows.Count; i++)
{
list.Add(tables.Tables[0].Rows[i][0].ToString());
}
ListBox1.DataSource = list;
ListBox1.DataBind();
ListBox1.Rows = (ListBox1.Items.Count / 2) + 3;
}
和connectHive
功能位于BasePage.cs
下方:
protected DataSet connectHive(string query, string pageURL)
{ // Hadoop Hive Connection
var page = HttpContext.Current.CurrentHandler as Page;
OdbcConnection DbConnection = new OdbcConnection("DSN=Hive2");
DbConnection.ConnectionTimeout = 20000000;
try
{
DbConnection.Open();
}
catch (OdbcException exep)
{
ScriptManager.RegisterStartupScript(page, page.GetType(), "alert", "alert('Error in Query!');window.location ='" + pageURL.Replace("/", "") + "';", true);
}
OdbcCommand cmd = DbConnection.CreateCommand();
OdbcDataAdapter adapter = new OdbcDataAdapter(query, DbConnection);
adapter.SelectCommand.CommandTimeout = 20000000;
DataSet data = new DataSet();
data.EnforceConstraints = false;
try
{
adapter.Fill(data);
}
catch (Exception ex)
{
List<string> parameters = new List<string>();
string pageName = findNameByUrl(HttpContext.Current.Request.RawUrl);
parameters.Add("UserID:");
parameters.Add(Session["sicil"].ToString());
parameters.Add("Exception:");
parameters.Add(ex.ToString());
string parameterString = castParameters(parameters);
string exception = "";
string[] sep = new string[] { "\r\n" };
string[] lines = ex.ToString().Split(sep, StringSplitOptions.RemoveEmptyEntries);
int position = lines[0].ToString().LastIndexOf("FAILED:");
if (position > -1)
exception = lines[0].ToString().Substring(position, lines[0].Length - position);
Session["Exception"] = exception.Replace("'", "");
Log(query, "Select Table(BasePage)", pageName, "Failed", parameterString);
}
DbConnection.Close();
return data;
}