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
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