The gridview e.CommandArgument string is not formatted correctly

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.

+4
source share
2 answers

You do not specify a CommandArgument . Therefore, your error occurs because you are trying to convert an empty string to an integer.

Add the row index as CommandArgument :

Try adding this to the Button control:

 <asp:Button ID="Button1" ButtonType="Link" CommandName="DownloadFile" CommandArgument='<%#Container.DataItemIndex%>' HeaderText="Download" runat="server" Text="Button" /> 
+4
source

You do not assign a value to CommandArgument , but access them in RowCommand , which is the cause of the error.

  <asp:Button ID="Button1" ButtonType="Link" CommandName="DownloadFile" HeaderText="Download" runat="server" Text="Button" CommandArgument="1" /> 

You can replace the hardcoded CommandArgument value with the associated value, as described in @Curt.

 CommandArgument='<%#eval("DataField")' 

Use ToString

 int index = Convert.ToInt32(e.CommandArgument.ToString()); 
+3
source

All Articles