从sqldatabase中检索pdf或doc文件 [英] Retrieve pdf or doc files from sqldatabase

查看:80
本文介绍了从sqldatabase中检索pdf或doc文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要从sql数据库中检索pdf或任何文件,但我收到错误索引超出范围。必须是非负面且小于集合的大小



我尝试过的事情:



I tring to retrieve pdf or any file from sql database but I got error Index was out of range. Must be non-negative and less than the size of the collection

What I have tried:

<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();
        }
    }
}

推荐答案

成员12856488写道:
Member 12856488 wrote:

int id = int.Parse(GridView1.DataKeys [gr.RowIndex] .Value.ToString());

int id = int.Parse(GridView1.DataKeys[gr.RowIndex].Value.ToString());



检查datakeys count


check the datakeys count


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.


这篇关于从sqldatabase中检索pdf或doc文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆