会话意外消失

时间:2011-04-26 12:29:58

标签: asp.net vb.net linq session

该网站是买/卖网站,代码来自的网页是“添加产品”页面。

问题是会话(“更改”)由于某种原因变得一无所获,我找不到任何错误。 payment.aspx有一个按钮,可以通过会话(“更改”)将我发回给页面。 我看到问题的原因是,当我尝试编辑某些内容时,该类别会被重新发送到列表中的第一个类别。当我调试时,我看到会话什么都没有,虽然它应该是

下面是代码:

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSubmit.Click
        If Not stats > 0 Then
            If Session("change") IsNot Nothing Then
                Dim dc As New DataClassesDataContext
                Dim getP = From prod In dc.Products _
                           Where prod.ProductID = CInt(Session("change")) _
                           Select prod

                If getP.Any Then
                    If rdbSell.Checked = True Then
                        getP.FirstOrDefault.BuySell = True
                    Else
                        getP.FirstOrDefault.BuySell = False
                    End If
                    If ddlSubSubcat.SelectedValue IsNot String.Empty Then
                        getP.FirstOrDefault.CategoryID = CInt(ddlSubSubcat.SelectedValue)
                    Else
                        getP.FirstOrDefault.CategoryID = CInt(ddlSubCat.SelectedValue)
                    End If

                    getP.FirstOrDefault.Content = txtContent.Text.Replace(Environment.NewLine, "<br />")
                    getP.FirstOrDefault.CountyID = CInt(ddlCounty.SelectedValue)
                    getP.FirstOrDefault.E_mail = txtEmail.Text
                    getP.FirstOrDefault.Date = DateTime.Now
                    getP.FirstOrDefault.Active = 0
                    getP.FirstOrDefault.Alias = txtAlias.Text.Replace("'", "''")
                    getP.FirstOrDefault.ShowEmail = 0
                    Dim PreID As Integer = getP.FirstOrDefault.ProductID
                    If chkShowEmail.Checked = True Then
                        getP.FirstOrDefault.ShowEmail = 1
                    Else
                        getP.FirstOrDefault.ShowEmail = 0
                    End If
                    If chkShowPhone.Checked = True Then
                        getP.FirstOrDefault.ShowPhone = 1
                    Else
                        getP.FirstOrDefault.ShowPhone = 0
                    End If
                    getP.FirstOrDefault.Headline = txtHeadline.Text
                    getP.FirstOrDefault.Password = txtPassword.Text
                    getP.FirstOrDefault.Phone = txtPhone.Text
                    getP.FirstOrDefault.Price = txtPrice.Text

                    If chkUnknown.Checked = True Then
                        getP.FirstOrDefault.YearModel = String.Empty
                    Else
                        getP.FirstOrDefault.YearModel = ddlYear.SelectedValue
                    End If

                    For Each item In libPictures.Items
                        Dim i As String = item.ToString
                        Dim imagecheck = From img In dc.Pictures _
                                         Where img.Name = i And img.ProductID = CInt(Session("change")) _
                                         Select img

                        If imagecheck.Any Then
                        Else
                            Dim img As New Picture
                            img.Name = item.ToString

                            img.ProductID = CInt(Session("change"))
                            dc.Pictures.InsertOnSubmit(img)
                            dc.SubmitChanges()
                        End If
                    Next

                    dc.SubmitChanges()

                    Session.Remove("change")
                    Response.Redirect("~/precheck.aspx?id=" + PreID.ToString)
                End If
            Else
                Dim dc As New DataClassesDataContext
                Dim prod As New Product
                If rdbSell.Checked = True Then
                    prod.BuySell = True
                Else
                    prod.BuySell = False
                End If
                If ddlSubSubcat.DataValueField IsNot String.Empty Then
                    prod.CategoryID = CInt(ddlSubSubcat.SelectedValue)
                Else
                    prod.CategoryID = CInt(ddlSubCat.SelectedValue)
                End If

                prod.Content = txtContent.Text.Replace(Environment.NewLine, "<br />")
                prod.CountyID = CInt(ddlCounty.SelectedValue)
                prod.E_mail = txtEmail.Text
                prod.Date = DateTime.Now
                prod.Active = 0
                prod.Alias = txtAlias.Text.Replace("'", "''")
                prod.ShowEmail = 0
                If chkShowEmail.Checked = True Then
                    prod.ShowEmail = 1
                Else
                    prod.ShowEmail = 0
                End If
                If chkShowPhone.Checked = True Then
                    prod.ShowPhone = 1
                Else
                    prod.ShowPhone = 0
                End If
                prod.Headline = txtHeadline.Text
                prod.Password = txtPassword.Text
                prod.Phone = txtPhone.Text
                prod.Price = txtPrice.Text

                If chkUnknown.Checked = True Then
                    prod.YearModel = String.Empty
                Else
                    prod.YearModel = ddlYear.SelectedValue
                End If

                dc.Products.InsertOnSubmit(prod)
                dc.SubmitChanges()
                Dim PreID As Integer = prod.ProductID

                For Each item In libPictures.Items
                    Dim img As New Picture
                    img.Name = item.ToString

                    img.ProductID = prod.ProductID
                    dc.Pictures.InsertOnSubmit(img)
                    dc.SubmitChanges()
                Next
                Session.Remove("change")
                Response.Redirect("./precheck.aspx?id=" + PreID.ToString, False)
            End If
        End If
        stats = 0
        'Catch ex As Exception
        'End Try

    End Sub

1 个答案:

答案 0 :(得分:1)

这取决于应用程序如何管理会话状态。如果您的会话状态是InProc,那么如果应用程序池被回收,那么您的所有会话信息都将丢失。如果发生这种情况,那么在SQL Server中存储会话状态可能是一个很好的选择,它将在应用程序池回收之间保持不变。

更多信息:

ASP.NET Session State Overview

ASP.NET State Management Recommendations