Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to download fie from database..
there is a datalist control in which i've put a linkbtn..
on which click event file mst be download.
bt the error occured..

error is object refrence not set to object
my code
DirectoryInfo directory = new DirectoryInfo(Server.MapPath("~/Booksimg/pdf/"));
          int counter = 0;
          foreach (FileInfo file in directory.GetFiles())
          {
              HyperLink link = new HyperLink();
              link.ID = "Link" + counter++;
              link.Text = file.Name;
              link.NavigateUrl = "Download.aspx?name=" + file.Name;

              //    Page.Controls.Add(link);
              //    Page.Controls.Add(new LiteralControl("<br/>"));
          }
      }
      protected void LinkButton10_Click(object sender, EventArgs e)
      {

          Response.Redirect("Download.aspx");
      }


and ..
download.aspx

protected void Page_Load(object sender, EventArgs e)
       {
           object us = Session["UserID"];
           if (Session["UserID"] == null || Session["RoleId"] == null)
               Response.Redirect("Login.aspx");
           else
           {
               user = Session["UserID"].ToString();
               String role = Session["RoleId"].ToString();


               cn.Open();
               string cmdstr = "SELECT unm from register where email='"+ user + "' ";
               SqlCommand cmd = new SqlCommand(cmdstr, cn);

               SqlDataReader rd = cmd.ExecuteReader();
               //if (rd.Read())
               //{

               //    Label2.Text = rd.GetString(1);

               //}
               rd.Close();



               string fileName = Request.QueryString["name"].ToString();// here is the error.

               Response.ContentType = "application/octet-stream";
               Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
               Response.TransmitFile(Server.MapPath("~/Booksimg/pdf/" + fileName));
               Response.End();
           }

       }
Posted
Updated 9-Apr-13 3:50am
v2
Comments
Naz_Firdouse 9-Apr-13 9:45am    
debug the code and see where you are getting this error?
I mean at which line it is throwing error...
Then it becomes easy to find out the issue...

1 solution

Hi,

When you click to download:
C#
protected void LinkButton10_Click(object sender, EventArgs e)

you make a redirect to Download.aspx without any additional options.


Then you have the following line of code where you get the error:
C#
string fileName = Request.QueryString["name"].ToString();


Because the query string is not being passed.

You need to get a url like this: http://localhost/Download.aspx?name=filename.ext, when you are getting a url like this: http://localhost/Download.aspx
 
Share this answer
 
Comments
mahi0607 9-Apr-13 11:01am    
bt this will only donload one file.
i have to make download file on which was selected.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900