会话错误:对象未设置为实例

发布于 2024-11-04 17:51:39 字数 1287 浏览 3 评论 0原文

在我的网页中,我无法在远程服务器中获取回发的会话变量。

问题是

如果页面不是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一张白纸 2024-11-11 17:51:39

当您回发时,名为“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.

心意如水 2024-11-11 17:51:39

请提供代码,否则我们无法猜测问题。
不过,您始终可以在使用会话变量之前对其进行空检查。

if(Session["User_id"]) != null;

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.

if(Session["User_id"]) != null;
娇女薄笑 2024-11-11 17:51:39

检查远程计算机上的 web.config 文件,并确保 sessionState 元素配置正确。您可能已将远程服务器上的 mode 属性设置为 Off

正如其他人指出的那样,在访问存储在会话中的对象的属性之前,您可以编写更具防御性的代码来检查 null

Check the web.config file on your remote machine and ensure that the sessionState element is configured correctly. You might have the mode attribute set to Off 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.

灼疼热情 2024-11-11 17:51:39

首先检查会话。

if (Session["User_id"] == null)
{
//Put code here

}
else
{
     Response.Write(Session["User_id"]);

     // or you can write as below

     Response.Write(Convert.ToString(Session["User_id"]));
}

永远不要使用 .ToString() 方法,你应该使用 Convert.ToString,这也是处理 null 值。

First check session.

if (Session["User_id"] == null)
{
//Put code here

}
else
{
     Response.Write(Session["User_id"]);

     // or you can write as below

     Response.Write(Convert.ToString(Session["User_id"]));
}

Never use .ToString() Method, you should use Convert.ToString, this is handle null value also.

遗忘曾经 2024-11-11 17:51:39

按钮单击事件发生在页面加载事件之后...这可能就是未设置它的原因。 ;-)

The button click event happens AFTER the page Load event... this might be why it's not set. ;-)

抽个烟儿 2024-11-11 17:51:39

您必须检查会话是否为空或为空,如下所示:

 if (Session["User_id"] == null || Session["User_id"].ToString() == string.Empty)
 {
    Response.write(Session["User_id"].ToString());
 }
 else
 {
   // your code
 }

you have to check you sesssion if its null or empty as follows:

 if (Session["User_id"] == null || Session["User_id"].ToString() == string.Empty)
 {
    Response.write(Session["User_id"].ToString());
 }
 else
 {
   // your code
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文