Reputation: 1
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 EventID=@EventID";
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();
}
}
The error was - "Index was outside the bounds of the array." I'm trying to download the file according to the event. so far i have done this much of codes. but i do not understand what the error means. pls explain to me what the error "Index was outside the bounds of the array means" and pls give me solutions. thanks
Upvotes: 0
Views: 2408
Reputation: 3191
This error could happen if you read an array and ask for an index greater or equal than the lenght of the array. Checks if the table you're reading has 8 fields or if you're selecting 8 fields.
Upvotes: 0
Reputation: 15673
The reader contains the result of the following statement
SELECT EventID from Event WHERE EventName = @EventName
and not for this statement
Select * from Attendance where EventID=@EventID
I would replace const string q with
const string q = @" Select * from dbo.Attendance
where EventID = (SELECT EventID from dbo.Event WHERE EventName = @EventName");
And instead of * I would use the column names, for two reasons: the database server will like it more and you will be sure which columns you'll have
Upvotes: 1
Reputation: 8357
you try to access up to 7 columns in that line, but you only have 1 column (EventId).
Edit:
You can't change the commandtext of a command while reading it. Well, apparently you can, but you won't get the expected results.
Upvotes: 2
Reputation: 1800
Index out of bounds means exactly that, you are trying to access somewhere that is past the end of the array.
e.g:
var array = new[] {0, 1, 2};
var temp = array[10];
will throw an index out of bounds exception because there is no item at position 10 in array
(it only has 3 items and so positions 0, 1 & 2).
This should be enough for you to try solving your problem. If you are still stuck let me know and I'll take a look at your code.
Upvotes: 0