protected void lnkDownload_Click(object sender, EventArgs e) { LinkButton lnkbtn = sender as LinkButton; GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow; int ImageId = Convert.ToInt32(GridView1.DataKeys[gvrow.RowIndex].Value.ToString()); string name, type; using (SqlConnection con = new SqlConnection(strCon)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = "select ImageName, FileName, AddedOn from ImageInfo where ImageId=@ImageId"; cmd.Parameters.AddWithValue("@ImageId", ImageId); cmd.Connection = con; con.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { Response.ContentType = dr["FileType"].ToString(); Response.AddHeader("Content-Disposition", "attachment;filename=\"" + dr["ImageName"] + "\""); Response.BinaryWrite((byte[])dr["FileName"]); Response.End(); } } } }
Server Error in '/WebSite5' Application. Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Source Error: Line 149: LinkButton lnkbtn = sender as LinkButton; Line 150: GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow; Line 151: int ImageId = Convert.ToInt32(GridView1.DataKeys[gvrow.RowIndex].Value.ToString()); Line 152: string name, type; Line 153: using (SqlConnection con = new SqlConnection(strCon)) Source File: c:\Users\SURYA-BOSS\Documents\Visual Studio 2010\WebSites\WebSite5\Default.aspx.cs Line: 151 Stack Trace: [ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index] System.Collections.ArrayList.get_Item(Int32 index) +9368776 System.Web.UI.WebControls.DataKeyArray.get_Item(Int32 index) +42 _Default.lnkDownload_Click(Object sender, EventArgs e) in c:\Users\SURYA-BOSS\Documents\Visual Studio 2010\WebSites\WebSite5\Default.aspx.cs:151 System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +141 System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +150 System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +38 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +37 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +289 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4225 Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
gvrow.RowIndex
GridView1.DataKeys.Count - 1
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)