Link to home
Start Free TrialLog in
Avatar of mk_b
mk_bFlag for South Africa

asked on

Session and Dataview issue

Ok please can someone help me with this anouying little problem. As you can se from the code below th my page loads the first time and it runs the If Session("MyHistoryBool") = "" Then and it works and shows the history fine. i and now have a dataset saved to Session("MyHistory") and session("MyHistoryBool") = True.

i would then click in a link and it would run the page again and then skip to the Else of If Session("login") <> "True"
but it will not show the data properly from the Session("MyHistory") ? i have also tryed using cType(Session("MyHistory") , Dataview)

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Session("login") <> "True" Then
            Response.Redirect("login.aspx?page=Login")
        End If
        '--get 12month history
        If Session("MyHistoryBool") = "" Then
            Dim myData As DataSet
            Dim myDataTable As New DataTable("myHistory")
            myDataTable.Columns.Add("mMonth", GetType(String))
            myDataTable.Columns.Add("Amount", GetType(Double))
            myDataTable.Columns.Add("vMonth", GetType(String))
            myDataTable.Columns.Add("vYear", GetType(String))
            Dim Startdate, EndDate

            Dim i, cCount As Integer
            Dim yYear, yMonth As Integer
            yYear = Year(Now())
            yMonth = Month(Now())
            For i = 1 To 12

                If yMonth = 0 Then
                    yYear = yYear - 1
                    yMonth = 12
                Else
                End If
                EndDate = fDate(DateSerial(yYear, yMonth + 1, 1 - 1))
                Startdate = yMonth & "/01/" & yYear
                myData = x.CustomerTurnover(Session("Code"), Startdate, EndDate)

                Total = Total + myData.Tables("DetailRecords").Rows(0)("Amount")
                Dim myDataRow = myDataTable.NewRow
                myDataRow("mMonth") = MonthName(yMonth) & " " & yYear
                myDataRow("Amount") = myData.Tables("DetailRecords").Rows(0)("Amount")
                myDataRow("vMonth") = yMonth
                myDataRow("vYear") = yYear
                myDataTable.Rows.Add(myDataRow)

                yMonth = yMonth - 1
            Next

            Dim DataHistory As New DataSet
            DataHistory.Tables.Add(myDataTable)

            Session("MyHistory") = DataHistory.Tables(0).DefaultView
            Repeater1.DataSource = Session("MyHistory")
            Repeater1.DataBind()

            myDataTable.Clear()
            myData.Clear()

            Session("MyHistoryBool") = "True"
            Session("Total") = Total
        Else
            Repeater1.DataSource = Session("MyHistory")
            Repeater1.DataBind()
        End If

        '--get selected month history
        If Request.QueryString("mMonth") <> "" Then
            Dim Startdate, EndDate
            Dim docHeadData As New DataSet
            EndDate = fDate(DateSerial(Request.QueryString("mYear"), Request.QueryString("mMonth") + 1, 1 - 1))
            Startdate = Request.QueryString("mMonth") & "/01/" & Request.QueryString("mYear")
            docHeadData = x.CustomerDocumentHeaders(Session("Code"), Startdate, EndDate)

            Dim myView As DataView
            myView = docHeadData.Tables(1).DefaultView
            myView.Sort = "DocumentDate ASC"

            pagedData.DataSource = myView
            pagedData.AllowPaging = True
            pagedData.PageSize = 15

            Try
                pagedData.CurrentPageIndex = Int32.Parse(Request.QueryString("PageNo")).ToString()
            Catch ex As Exception
                pagedData.CurrentPageIndex = 0
            End Try

            cmdPrev.Enabled = (Not pagedData.IsFirstPage)
            cmdNext.Enabled = (Not pagedData.IsLastPage)

            Repeater2.DataSource = pagedData
            Repeater2.DataBind()
        End If


    End Sub
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

Is the Session("login" storing a boolean or a string? If a boolean, it will never = "True"

Try using IsNothing(Session("")) in place of ="".
Avatar of mk_b

ASKER

Its not that if you had read the question you would have seen the error is with the dataset in the Session("MyHistory")

../mk
Have you tried
dim dv as dataview = Session("MyHistory")
repeater1.datasource = dv

You're storing a dataview ( Session("MyHistory") = DataHistory.Tables(0).DefaultView) in the session variable.
Avatar of mk_b

ASKER

Yes currently i have it like this but still not working?

Else
            Dim source As New DataView
            source = CType(Session("MyHistory"), DataView)
            Repeater1.DataSource = source
            Repeater1.DataBind()
End If

Okay... here is some code from a working project. I use a datatable, not a dataview, but the concept is the same... Instead of merely using the session object, I've used a property to reference the datatable. I initialize the dt property once, during the Initialize event for the control.

  Private _dt as DataTable
  Private Property dt() As DataTable
    Get
      If _dt Is Nothing Then _dt = Me.Page.Session("DataPage_dt")
      Return _dt
    End Get
    Set(ByVal Value As DataTable)
      _dt = Value
      Me.Page.Session("DataPage_dt") = _dt
    End Set
  End Property 'dt
Avatar of mk_b

ASKER

Still not its strang i step though the code and it looks like its working but when you get though it no data?

here is what i have now

        '--get 12month history
        If Session("MyHistoryBool") = "" Then
            Dim myData As DataSet
            Dim myDataTable As New DataTable("myHistory")
            myDataTable.Columns.Add("mMonth", GetType(String))
            myDataTable.Columns.Add("Amount", GetType(Double))
            myDataTable.Columns.Add("vMonth", GetType(String))
            myDataTable.Columns.Add("vYear", GetType(String))
            Dim Startdate, EndDate

            Dim i, cCount As Integer
            Dim yYear, yMonth As Integer
            yYear = Year(Now())
            yMonth = Month(Now())
            For i = 1 To 12

                If yMonth = 0 Then
                    yYear = yYear - 1
                    yMonth = 12
                Else
                End If
                EndDate = fDate(DateSerial(yYear, yMonth + 1, 1 - 1))
                Startdate = yMonth & "/01/" & yYear
                myData = x.CustomerTurnover(Session("CustCode"), Startdate, EndDate)

                Total = Total + myData.Tables("DetailRecords").Rows(0)("Amount")
                Dim myDataRow = myDataTable.NewRow
                myDataRow("mMonth") = MonthName(yMonth) & " " & yYear
                myDataRow("Amount") = myData.Tables("DetailRecords").Rows(0)("Amount")
                myDataRow("vMonth") = yMonth
                myDataRow("vYear") = yYear
                myDataTable.Rows.Add(myDataRow)

                yMonth = yMonth - 1
            Next

            Dim DataHistory As New DataSet
            DataHistory.Tables.Add(myDataTable)

            Me.dt = DataHistory.Tables(0)

            Repeater1.DataSource = Me.dt
            Repeater1.DataBind()

            DataHistory.Clear()
            myDataTable.Clear()
            myData.Clear()

            Session("MyHistoryBool") = "True"
            Session("Total") = Total
        Else
            Repeater1.DataSource = Me.dt
            Repeater1.DataBind()
        End If

    Private _dt As DataTable
    Private Property dt() As DataTable
        Get
            If _dt Is Nothing Then _dt = Me.Page.Session("MyHistory")
            Return _dt
        End Get
        Set(ByVal Value As DataTable)
            _dt = Value
            Me.Page.Session("MyHistory") = _dt
        End Set
    End Property 'dt
This line will not behave as expected... since the "day" value is 1-1=0

EndDate = fDate(DateSerial(yYear, yMonth + 1, 1 - 1))
Avatar of mk_b

ASKER

no that day value will be the last day of the month!

the thing that gets me is that it works the first time arround its just when you try get the datatable from the session or now dt property it displays nothing???

../mk
Okay... when I pulled up the code, it indicated that value had to range from 1 to 31...

I wonder if what is happening is that the datatable is being passed with an implied dependency on the parent dataset?

Try changing Me.dt = DataHistory.Tables(0) to me.dt=mydatatable
Avatar of mk_b

ASKER

still not Chaosian i dont htink its going to be something like that? like i've said it works the first time just not there after?

and whin i step thought the code all look well it says Me.dt is a datatable but then when you out there is no data?
out of curiousity... if you remove the DataHistory.Clear line, whan happens?
Avatar of mk_b

ASKER

VICTORY

I used the httpcache

http://www.codeproject.com/aspnet/datastorage.asp
http://samples.gotdotnet.com/quickstart/aspplus/doc/datacaching.aspx


        Dim Source As DataTable
        Source = Cache("MyDataSet")

        If Source Is Nothing Then

            Dim myData As DataSet
            Dim myDataTable As New DataTable("myHistory")
            myDataTable.Columns.Add("mMonth", GetType(String))
            myDataTable.Columns.Add("Amount", GetType(Double))
            myDataTable.Columns.Add("vMonth", GetType(String))
            myDataTable.Columns.Add("vYear", GetType(String))
            Dim Startdate, EndDate

            Dim i, cCount As Integer
            Dim yYear, yMonth As Integer
            yYear = Year(Now())
            yMonth = Month(Now())
            For i = 1 To 12

                If yMonth = 0 Then
                    yYear = yYear - 1
                    yMonth = 12
                Else
                End If
                EndDate = fDate(DateSerial(yYear, yMonth + 1, 1 - 1))
                Startdate = yMonth & "/01/" & yYear
                myData = x.CustomerTurnover(Session("CustCode"), Startdate, EndDate)

                Total = Total + myData.Tables("DetailRecords").Rows(0)("Amount")
                Dim myDataRow = myDataTable.NewRow
                myDataRow("mMonth") = MonthName(yMonth) & " " & yYear
                myDataRow("Amount") = myData.Tables("DetailRecords").Rows(0)("Amount")
                myDataRow("vMonth") = yMonth
                myDataRow("vYear") = yYear
                myDataTable.Rows.Add(myDataRow)

                yMonth = yMonth - 1
            Next

            Source = myDataTable
            Cache("MyDataSet") = Source

            Cache("MyTotal")= Total

        End If

        Repeater1.DataSource = Source
        Repeater1.DataBind()


Thanks for trying Chaosian. I think it may have had something to do with DataHistory.Clear() myDataTable.Clear() myData.Clear()/ maybe....

im going to ask the the questing be closed and points refunded?

regards,
../mk
ASKER CERTIFIED SOLUTION
Avatar of ee_ai_construct
ee_ai_construct
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial