<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <columns> <asp:BoundField DataField="Section" HeaderText="File Name" /> <asp:BoundField DataField="Category" HeaderText="File Name" /> <asp:TemplateField ItemStyle-HorizontalAlign="Center"> <itemtemplate> <asp:LinkButton ID="lnkView" OnClick="OpenDocument" runat="server" Text='<%# Eval("Link") %>'>
protected void OpenDocument(object sender, EventArgs e) { LinkButton lnk = (LinkButton)sender; GridViewRow gr = (GridViewRow)lnk.NamingContainer; if (gr.RowIndex > 0) { int id = int.Parse(GridView1.DataKeys[gr.RowIndex].Value.ToString()); Download(id); } } private void Download(int id) { DataTable dt = new DataTable(); string constr = ConfigurationManager.ConnectionStrings["IntranetConnectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = "SELECT Seqn, Section, Category, Link from Links WHERE Seqn = @seqn "; cmd.Connection = con; cmd.Parameters.Add("@seqn", SqlDbType.Int).Value = id; con.Open(); SqlDataReader reader = cmd.ExecuteReader(); dt.Load(reader); } } string name = dt.Rows[0]["Category"].ToString(); byte[] documentBytes = (byte[])dt.Rows[0]["Link"]; Response.ClearContent(); Response.ContentType = "application/octetstream"; Response.AddHeader("Content-Disposition", string.Format("attachment; filename={}", name)); Response.AddHeader("Content-Length", documentBytes.Length.ToString()); Response.BinaryWrite(documentBytes); Response.Flush(); Response.Close(); } private void FillData() { DataTable dt = new DataTable(); string constr = ConfigurationManager.ConnectionStrings["IntranetConnectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = "SELECT Seqn, Section, Category, Link from Links "; cmd.Connection = con; con.Open(); SqlDataReader reader = cmd.ExecuteReader(); dt.Load(reader); con.Close(); } if (dt.Rows.Count > 0) { GridView1.DataSource = dt; GridView1.DataBind(); } } }
on whichh line you are getting error? first need to check datakey and then rowindex. and these two are ok then check final id which you are passing to download function.
Member 12856488 wrote:int id = int.Parse(GridView1.DataKeys[gr.RowIndex].Value.ToString());
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)