"/"应用程序中的服务器错误.使用(C#).NET

编程入门 行业动态 更新时间:2024-10-27 15:27:49
本文介绍了"/"应用程序中的服务器错误.使用(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"始终存在.你怎么知道? 请参阅 msdn.microsoft/en-us/library/03sekbw5(v = VS.85).aspx [ ^ ]. 仅在代码(硬编码)中使用立即常数"lodgingID"的事实值得怀疑.在任何情况下,都不应使用此类的立即数.您如何支持它?如果您在多个地方写相同的常量,那么如何保证它是相同的呢?编译器不会警告您.
—SA
Most likely Session["lodgingID"] is null. You cannot assume the session state "lodgingID" always exist. How do you know? See msdn.microsoft/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

本文发布于:2023-07-27 14:12:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222792.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   错误   服务器   quot   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!