protected void btnDownload_Click(object sender, EventArgs e)
{
//to request the name of the event from the listbox from Main.aspx
string EventName = Request.QueryString["ename"];
//Select event id statement
//const string S = "SELECT EventName FROM Event WHERE EventID = @EventID";
const string q = "SELECT EventID from Event WHERE EventName = @EventName";
string eventid = "";
using (SqlConnection c = new SqlConnection(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=PSeminar;Integrated Security=true;Trusted_Connection=Yes;MultipleActiveResultSets=true"))
using (SqlCommand Command = new SqlCommand(q, c))
{
Command.Parameters.AddWithValue("@EventName", EventName);
c.Open();
using (SqlDataReader rdr = Command.ExecuteReader())
while (rdr.Read())
{
Command.CommandText = "Select * from Attendance where [email protected]";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(String.Format("\"{0}\",\"{1}\", \"{2}\", \"{3}\", \"{4}\", \"{5}\", \"{6}\", \"{7}\"n",
rdr[0], rdr[1], rdr[2], rdr[3], rdr[4], rdr[5], rdr[6], rdr[7]));
// I have an error here(Index out of bound)
// to get event id from the Event name
eventid = rdr.GetString(0);
rdr.Close();
c.Close();
byte[] ar = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content.Type", "application/octet-stream");
Response.AddHeader("Content-Length", ar.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=download.csv");
Response.BinaryWrite(ar);
Response.Flush();
Response.End();
}
}
錯誤是 - 「索引超出了數組的範圍。」 我正在嘗試根據事件下載文件。到目前爲止,我已經完成了大量的代碼。但我不明白錯誤的含義。請向我解釋「索引是否超出數組範圍」的錯誤,並請給我解決方案。謝謝錯誤:索引超出範圍
我有10列,這意味着我必須有一個數組9我對嗎?但是在調試時不會假設任何東西,它仍然會給出錯誤消息 –
@TerenceTeng。始終驗證。你沒有10列,但只有一列。當你只選擇一列時,表格有多少個並不重要。 –