会话错误:对象未设置为实例
在我的网页中,我无法在远程服务器中获取回发的会话变量。
问题是
如果页面不是 IsPostback 那么我能够获取会话变量。
但如果页面回发则会出现错误。
当我使用异步文件上传时,也会出现同样的错误。我正在使用 asynchfileupload 上传图像并将其存储到会话变量中。然后单击按钮“我正在将数据保存到目录”。
当我将任何文件上传到服务器时,就会出现此问题。我正在使用 asynchfileupload 控件上传图像。但这种情况并不经常发生。
这是我
protected void AsynImageLoader_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
if (AsynImageLoader.PostedFile != null)
{
string extension = System.IO.Path.GetExtension(AsynImageLoader.PostedFile.FileName);
if (extension == ".jpg" || extension == ".gif" || extension == ".jpeg" || extension == ".png")
{
HttpPostedFile file = AsynImageLoader.PostedFile;
Session["TempImage"] = ReadFile(file);
}
}
}
单击按钮
var storedImage = Session["TempImage"] as byte[];
String Strthumbpath = Server.MapPath("content\\thumbnail\\");
if (storedImage != null)
{
System.Drawing.Image image = GetImage(storedImage);
if (image != null)
{
image.Save(Strthumbpath + strFileName);
}
}
将值插入数据库的代码。
经过如此多的谷歌搜索后,我读到,当任何文件添加到任何子目录或编辑 webconfig 时,都会导致应用程序重新启动。
如果是这样我该如何解决这个问题..
提前致谢。
In my webpage I'm not able to get the session variable on post back in remote server.
The problem is
if the page is not IsPostback then I'm able to get the session variable.
But if the page postback then the error appears.
This same error comes when i use the asynchfile upload. I'm using asynchfileupload to upload a image and store it to a session variable. and later on button click i am saving data to directory.
this problem arouse when i upload any files to the server.i am using the asynchfileupload control to upload the image.,but it is not occuring frequently.
here is my code
protected void AsynImageLoader_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
if (AsynImageLoader.PostedFile != null)
{
string extension = System.IO.Path.GetExtension(AsynImageLoader.PostedFile.FileName);
if (extension == ".jpg" || extension == ".gif" || extension == ".jpeg" || extension == ".png")
{
HttpPostedFile file = AsynImageLoader.PostedFile;
Session["TempImage"] = ReadFile(file);
}
}
}
on button click
var storedImage = Session["TempImage"] as byte[];
String Strthumbpath = Server.MapPath("content\\thumbnail\\");
if (storedImage != null)
{
System.Drawing.Image image = GetImage(storedImage);
if (image != null)
{
image.Save(Strthumbpath + strFileName);
}
}
inserting values to datbase.
after somuch googling i read that when any files is adding to any sub directory or editing the webconfig will cause cause the application to restart.
if so how can i solve this..
thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当您回发时,名为“User_id”的会话变量似乎被删除。您看到“对象未设置为对象的实例”,因为您尝试对空对象执行“ToString()”方法,这是不允许的。
希望这有帮助。
It looks like your session variable called "User_id" is getting removed when you post back. You're seeing "Object not set to an instance of an object" because you're trying to perform the "ToString()" method on a null object, which isn't allowed.
Hope this helps.
请提供代码,否则我们无法猜测问题。
不过,您始终可以在使用会话变量之前对其进行空检查。
Please provide code, without that we can't guess the issue.
Though, you can always do null checking on your session variable before using it.
检查远程计算机上的
web.config
文件,并确保sessionState
元素配置正确。您可能已将远程服务器上的mode
属性设置为Off
。正如其他人指出的那样,在访问存储在会话中的对象的属性之前,您可以编写更具防御性的代码来检查
null
。Check the
web.config
file on your remote machine and ensure that thesessionState
element is configured correctly. You might have themode
attribute set toOff
on your remote server.As others have pointed out you could code more defensively checking for
null
before accessing properties on the object you've got stored in session.首先检查会话。
永远不要使用
.ToString()
方法,你应该使用Convert.ToString
,这也是处理 null 值。First check session.
Never use
.ToString()
Method, you should useConvert.ToString
, this is handle null value also.按钮单击事件发生在页面加载事件之后...这可能就是未设置它的原因。 ;-)
The button click event happens AFTER the page Load event... this might be why it's not set. ;-)
您必须检查会话是否为空或为空,如下所示:
you have to check you sesssion if its null or empty as follows: