I am trying to cast pdf from my database to gridview and allow the user to click on it and load the pdf. I am trying to resolve an issue that has been resolved here:
Access data from BoundField Gridview
However, I get the following error:
Input string was not in a correct format.
Here is my asp.net code:
<Columns> <asp:CommandField ShowEditButton="True" ControlStyle-CssClass="savefile"/> <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> <asp:BoundField DataField="event_name" HeaderText="event_name" SortExpression="event_name" /> <asp:TemplateField HeaderText="PDF"> <ItemTemplate> <asp:Button ID="Button1" ButtonType="Link" CommandName="DownloadFile" HeaderText="Download" runat="server" Text="Button" /> </ItemTemplate> <EditItemTemplate> <asp:FileUpload ID="FileUpload1" runat="server" /> // shown only in edit mode </EditItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
and the corresponding C # code:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "DownloadFile") { int index = Convert.ToInt32(e.CommandArgument); string id = GridView1.DataKeys[index].Value.ToString(); SqlConnection con = new SqlConnection(strcon); string command = "Select pdf from table where id = @id"; SqlCommand cmd = new SqlCommand(command, con); cmd.Parameters.AddWithValue(id, "id"); SqlDataReader reader = cmd.ExecuteReader(); reader.Read(); Response.Clear(); Response.ContentType = "application/pdf"; Response.BinaryWrite((Byte[])reader[0]); Response.Flush(); Response.End(); } }
The error line is in my C # code:
int index = Convert.ToInt32(e.CommandArgument);
Thanks for the help in advance guys!
change ---------------------------------------------- --- -
Now my button field looks like this:
<asp:Button ID="Button1" ButtonType="Link" CommandName="DownloadFile" CommandArgument='<%#Container.DataItemIndex%>' HeaderText="Download" runat="server" Text="Button" />
and my C # error code line:
int index = Convert.ToInt32(e.CommandArgument);
and I still get the same error.