网站本地测试没问题后上传,打开网页出现未将对象引用设置到对象的实例。

来源:百度知道 编辑:UC知道 时间:2024/05/15 05:13:39
本地测试完全没问题的,但是上传网站后就出现以下内容:

未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:

行 22: public void bind()
行 23: {
行 24: string conn = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ToString();
行 25: string sql = " select top 6 u.userId,a.title,u_count,a.albumsId,p.photoPath from users u,albums a,photo p where u.userId=a.userId and a.userId=p.userId and u.recommend='已推荐' and title_Page='是' order by u.recommend_time desc";
行 26:

源文件: d:\hosting\wwwroot\sooodo_com\htdocs\index.aspx.cs 行: 24

堆栈跟踪:

[NullReferenceException: 未将对象引用设置到对象的实例。]
_Default.bind() in d:\hosting\wwwroot\sooodo_com\htdocs\index.aspx.cs:24
_Default.Page_Load(Object sender, EventArgs e) in

错误24行的意思不就是压根没找到System.Configuration.ConfigurationManager.ConnectionStrings["constr"]吗?不再使用它的TOString()方法肯定会报错啊
你在本地发布配置到IIS中运行看看行不行
造成这种情况的可能很多,方便的话把网站贴出来,你看看是不是缺少using System.Configuration;的引用,或者发布的时候是不是有人动过代码,是不是Web.config文件丢失了?是不是Web.config中“constr”属性被删了或隐藏了
我通常都是用WebConfigurationManager.AppSettings["constr"]方法获取Web.config内容
<appSettings>
<add key="constr" value="Server=192.168.8.250;Database=ManagerData;User ID=sa;Password=123456"/>
</appSettings>
要不你换个试试
你上面web.config中sa怎么没有密码?
你把下面的代码复制到config中试试,读取的时候用 string conn = WebConfigurationManager.AppSettings["constr"].ToString();读取,另外加上sa的密码

<?xml version="1.0" encoding="utf-8"?>

<configuration>
<appSettings>
<add key="constr" value="Server=.;Database=sooomo;User ID=sa;Password=sa的密码"/>
</appSettings>