Global Dataset?

  • Thread starter Thread starter Charles A. Lackman
  • Start date Start date
C

Charles A. Lackman

Hello,

Is there a way to make a dataset global to all my aspx 2.0 web forms?

Thanks,
Chuck
 
Sure. Declare it in your Global.asax as a public static field. Then you can
refer to it from any page as Global.MyDataSet
Peter
 
Thanks,

For some reason is it not working for me.

This is the code:

<%@ Application Language="VB" %>

<script runat="server">

Public MyDataset As New Dataset

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application startup
End Sub

I Cannot put Public Static as an error is shown that says it is not a part
of a declaration.

When I type Global. - MyDataset is not listed.

Thanks,

Chuck


Sure. Declare it in your Global.asax as a public static field. Then you can
refer to it from any page as Global.MyDataSet
Peter
 
You could put it in Application State.

Application.Lock()
Application("MyDataSet") = MyDataSet
Application.Unlock()
 
Hello,

I am not sure why this isn't working


<%@ Application Language="VB" %>

<script runat="server">

Public Shared ATest as string

OR

Public Shared MyDataset as New Dataset


In Default.aspx in the Load_Event

Global. does not show ATest or MyDataset

could there be a setting that I am missing?

Thanks

Chuck


Charles,
"static" is the C# modifier. in VB.NET, you want to use the equivalent
keyword, "Shared".
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
 
Hello,

This appears to let me see the dataset throughout my application, but when I
make changes to it, the changes to follow. IE

This is Page 1

Dim ARow as DataRow = Application("MyDataset").Tables(0).NewRow

ARow(0) = "Hello"

Application("MyDataset).Tables(0).Rows.Add(ARow)

This is Page 2

Datagrid1.DataSource = Application("MyDataset")
Datagrid1.Databind

Could I be loosing my data on the postback? Any Suggestions.
Page 2 shows an empty dataset without the row I added on page 1


Thanks,

Chuck

You could put it in Application State.

Application.Lock()
Application("MyDataSet") = MyDataSet
Application.Unlock()
 
After you make changes to the DataSet you should put the new modified
version back into Application State using the same lines of code I already
gave you.
 
Hello,

Still not working:

Page 1

Dim ADataset as new Dataset

'Table Added here

Application.Lock
Application("MyDataset") = ADataset
Application.UnLock

Page 2

Dim ADataset As New Dataset

ADataset.Merge(Application("MyDataset"))

ARow = ADataset.Tables(0).NewRow
ARow(0) = "Hello"

ADataset.Tables(0).Rows.Add(ARow)

Application.Lock
Application("MyDataset") = ADataset
Application.UnLock

Page 3

Datagrid1.Datasource = Application("MyDataset")
Datagrid1.Databind

Am I doing something wrong, it's not working.

Chuck

After you make changes to the DataSet you should put the new modified
version back into Application State using the same lines of code I already
gave you.
 
The namespace of this sample is "shred":

' Global.asax
Imports System.Web.SessionState
Imports System.Data

Public Class Global_asax
Inherits System.Web.HttpApplication

Public Shared MyDataSet As DataSet = New DataSet

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = New DataTable
dt.Columns.Add("TEST")
Dim row As DataRow = dt.NewRow
row("TEST") = "JUST A TEST"
dt.Rows.Add(row)
MyDataSet.Tables.Add(dt)
End Sub



'Page:
Partial Public Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim ds As DataSet = Global.shred.Global_asax.MyDataSet
Response.Write(ds.Tables(0).Rows(0)("TEST"))
End Sub

End Class

--Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
 
It worked Great!! Thank you VERY Much.

Chuck


The namespace of this sample is "shred":

' Global.asax
Imports System.Web.SessionState
Imports System.Data

Public Class Global_asax
Inherits System.Web.HttpApplication

Public Shared MyDataSet As DataSet = New DataSet

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = New DataTable
dt.Columns.Add("TEST")
Dim row As DataRow = dt.NewRow
row("TEST") = "JUST A TEST"
dt.Rows.Add(row)
MyDataSet.Tables.Add(dt)
End Sub



'Page:
Partial Public Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim ds As DataSet = Global.shred.Global_asax.MyDataSet
Response.Write(ds.Tables(0).Rows(0)("TEST"))
End Sub

End Class

--Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
 
Declare it in a class in the APP_CODE folder with a public accessor.
But it does involve some code because your other pages will then
have to 'call it' from the class.
 
Testing...

Hello,

This appears to let me see the dataset throughout my application, but when I
make changes to it, the changes to follow. IE

This is Page 1

Dim ARow as DataRow = Application("MyDataset").Tables(0).NewRow

ARow(0) = "Hello"

Application("MyDataset).Tables(0).Rows.Add(ARow)

This is Page 2

Datagrid1.DataSource = Application("MyDataset")
Datagrid1.Databind

Could I be loosing my data on the postback? Any Suggestions.
Page 2 shows an empty dataset without the row I added on page 1


Thanks,

Chuck

You could put it in Application State.

Application.Lock()
Application("MyDataSet") = MyDataSet
Application.Unlock()
 
Back
Top