本文介绍了对象引用未设置为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数据库下载fie ..

有一个datalist控件,其中我放了一个linkbtn ..

点击事件文件mst可以下载。

bt发生了错误..



错误是对象引用未设置为对象

my代码

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





和..

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

       }

推荐答案

protected void LinkButton10_Click(object sender, EventArgs e)



you重定向到 Download.aspx ,无需任何其他选项。





然后您有以下行您收到错误的代码:


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


Then you have the following line of code where you get the error:

string fileName = Request.QueryString["name"].ToString();





因为查询字符串没有被传递。



你需要得到这样的网址:http://localhost/Download.aspx?name = filename.ext,当你得到这样的网址时:http://localhost/Download.aspx



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


这篇关于对象引用未设置为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 21:42