"/"应用程序中的服务器错误.使用(C#).NET [英] Server Error in '/' Application. Using (C#) .NET

查看:99
本文介绍了"/"应用程序中的服务器错误.使用(C#).NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的应用程序可以在localhost上正常运行,但是会尽快出现此错误
当我把它放在服务器上时.我知道当对象具有
时会发生此错误 尚未在引用之前实例化,但为什么不应该发生
在本地主机中?
整个错误消息如下所示:

Hi,
My application works fine on the localhost but spits this error as soon
as I put it on the server. I know this error occurs when an object has
not been instantiated prior to a reference, but why shouldn''t it happen
in the localhost?
Here''s what the the whole error msg looks like:

Server Error in ''/'' Application.
--------------------------------------------------------------------------------
Object reference not set to an instance of an object.


我的代码:


my code:

iBookings.Lodgings.Lodging objLodging = new iBookings.Lodgings.Lodging();

objLodging.ID = (int)Session["lodgingID"];

string name = objLodging.GetName();


Thanx


Thanx

推荐答案

最可能的Session["lodgingID"]null.您不能假定会话状态"lodgingID"始终存在.你怎么知道?
请参阅 http://msdn.microsoft.com/en-us/library/03sekbw5(v = VS.85).aspx [的事实值得怀疑.在任何情况下,都不应使用此类的立即数.您如何支持它?如果您在多个地方写相同的常量,那么如何保证它是相同的呢?编译器不会警告您.

—SA
Most likely Session["lodgingID"] is null. You cannot assume the session state "lodgingID" always exist. How do you know?
See http://msdn.microsoft.com/en-us/library/03sekbw5(v=VS.85).aspx[^].

The mere fact you use immediate constant "lodgingID" in code (hard-coded) is suspect. There are no situations when you should use immediate constant like that. How can you support it? If you write the same constant in more than one place, how can you guarantee it''s the same? Compiler won''t warn you.

—SA


可能是Session["lodgingID"]对象为空或根本没有实例化.在转换为所需类型之前,请检查会话对象.以下代码帮助您执行此操作.
This probably happens the Session["lodgingID"] object is empty or not instantiated at all.Check the session object before casting to the required type.The following code help you how to do this.
//...
if(Session["lodgingID"]!=null)
   objLodging.ID = (int)Session["lodgingID"];
   // ...


希望对您有所帮助.


I hope this helps you well.


感谢大家的快速解答...
终于我解决了这个问题.
这是与
错误有关的问题 类中的代码中的数据库的连接字符串(objLodging.GetName();)


还是要感谢!
Thank you all of you for your quick answers...
Finally I solved this problem..
It was a problem that has to do with something wrong with the
connection string to the Database in the code inside my class ( objLodging.GetName();)


Thnks anyway!


这篇关于"/"应用程序中的服务器错误.使用(C#).NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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