データベースからグリッドビューにPDFを取り込み、ユーザーがクリックしてPDFをダウンロードできるようにしようとしています。ここで解決された質問に従おうとしています:
Gridview の BoundField からのデータへのアクセス
ただし、次のエラーが発生します。
Input string was not in a correct format.
ここに私のasp.netコードがあります:
     <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>
および対応する c# コード:
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(); 
    }
}
エラー行は、私の C# コードでは次のとおりです。
int index = Convert.ToInt32(e.CommandArgument);
事前に助けてくれてありがとう!
編集 - - - - - - - - - - - - - - - - - - - - - - - - - -
ボタンフィールドは次のようになります。
<asp:Button ID="Button1" ButtonType="Link" CommandName="DownloadFile" CommandArgument='<%#Container.DataItemIndex%>' HeaderText="Download"  runat="server" Text="Button" />
そして私のC#エラーコード行:
int index = Convert.ToInt32(e.CommandArgument);
私はまだ同じエラーを受け取っています。