Double Record Count W/Global Dataset

tate

Centurion
Joined
Nov 10, 2003
Messages
116
I have a very confusing problem with a calendar site I'm working on. To spare everyone from the actual pages of code I have created an condensed example that will illustrate my confusion.

If I create a dataset locally within the page load event I return the exact number of records in the underlying database table just as I want to. However, if I declare the dataset variable as a global variable the same code returns a duplicate of each database record so I end up with 2x the records I should. Even though I don't believe post back to be the problem I have tried every possible combination of Not Page.IsPostBack control with no luck.

I really need the dataset to be global so I can reference the data within the associated tables in multiple event triggers. I sure don't want to connect and gather data from the database every time I select a different calendar day.

Anybody have some words of wisdom for me?
Tate

~~~~~~~~~~~ Successful Code ~~~~~~~~~~~~~~~~~~~~~~~

' Set up the database connection and load data
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

' Create connection
Dim strConnect As String
strConnect = "database"
Dim odbcoEvent As OleDbConnection = New OleDbConnection(strConnect)
' Open connection
odbcoEvent.Open()
' Create SQL command for data adapter
Dim strItems As String
strItems = "SELECT * FROM tblEvent"
' Create data adapter
Dim odbdaEvents As OleDbDataAdapter = New OleDbDataAdapter(strItems, odbcoEvent)
' Create dataset and fill with data
Dim dsEvents As New DataSet
odbdaEvents.Fill(dsEvents, "Events")

' Create dataview
Dim dv1 As New DataView(dsEvents.Tables("Events"))
Repeater1.DataSource = dv1
Repeater1.DataBind()
' Close database connection
odbcoEvent.Close()

End Sub

~~~~~~~~~~~ Wrong Code Causing Duplicates ~~~~~~~~~~~~~~~

' Declare global variables
Dim dsEvents As New DataSet


' Set up the database connection and load data
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

' Create connection
Dim strConnect As String
strConnect = "database"
Dim odbcoEvent As OleDbConnection = New OleDbConnection(strConnect)
' Open connection
odbcoEvent.Open()
' Create SQL command for data adapter
Dim strItems As String
strItems = "SELECT * FROM tblEvent"
' Create data adapter
Dim odbdaEvents As OleDbDataAdapter = New OleDbDataAdapter(strItems, odbcoEvent)
' Fill dataset with data
odbdaEvents.Fill(dsEvents, "Events")

' Create dataview
Dim dv1 As New DataView(dsEvents.Tables("Events"))
Repeater1.DataSource = dv1
Repeater1.DataBind()
' Close database connection
odbcoEvent.Close()

End Sub
 
Does it return the duplicates on the 1st run or only on subsequent ones? Also when using a global variable is it referenced from any other procedures on the page?
 
The duplicates are present every time the page is loaded when the dataset object is decared globally.

Yes the dataset is referenced in multiple event procedures that is why I need it to be global. If the dataset object is declared locally everything works great. However, I'm not able to access the dataset tables during other event procedures. In the actual project I'm working on the dataset is populated in the page_load. Then the calendar day_render compares the calendar day to the date within each dataset record. If the date matches then the calendar cell detail is added. Finally, when a specific date is selected additional detail for that day's events are posted in a datagrid. So you can see that there is a lot of interaction going on with the same dataset.

You want to hear something even more interesting? If I develop the project in Visual Studio with code behinds the project works great. Unfortunately my client wanted to add the event calendar to an existing legacy HTML site. So I created a stand-alone *aspx page that the legacy pages link to. This approach was far more easier than updating everything to ASP.NET.
 
Back
Top