Returning a dataset to bind to a listbox

J

Jordan

I'm trying to incorporate a data tier into my asp.net web application (add,
update, delete, etc), as opposed to using spaghetti code.
So far my "getter" class, "CabinetSpec.vb", has Update, Add, and Delete
methods that
work perfectly. However, I am having problems with my "List" function which
returns a dataset that gets bound to a listbox on my webform.

Everytime I add, update or delete a CabinetSpec on the webform, the ListBox
containing the list of CabinetSpecs updates itself using this function:

-------------------------------------------------------
Public Class CabinetSpec

Public Function List() As DataSet
'return a list of RowIDs & Descriptions
mstrSQL = "SELECT CabinetSpecID, Description FROM CabinetSpecs ORDER BY
Description"
Dim ds As New DataSet
Try
Connect()
Dim da As New SqlDataAdapter(mstrSQL, objConnection)
da.Fill(ds, "CabinetSpecs")
Catch ex As Exception
Throw ex
Finally
Disconnect()
End Try
Return ds
End Function
-------------------------------------------------------

Then my webform binds the data to the ListView here:

-------------------------------------------------------
CabinetSpec.aspx

Private Sub Fill_lstSpecs()
Try
Dim CS As New CabinetSpec
With lstSpecs
.SelectedIndex = -1
.DataSource = CS.List.Tables(0).DefaultView
.DataTextField = "Description"
.DataValueField = "CabinetSpecID"
.DataBind()
End With
Catch ex As Exception
lblError.Text = ex.Message
End Try
End Sub
-------------------------------------------------------

This partially works. I can see the ListBox update itself everytime I Add,
Update
or Delete a record. The problem occurs when I leave the page and return -
for some reason when
the page loads Fill_lstSpecs() the ListBox no longer shows the most recent
dataset. For example, I add a new CabinetSpec Description "test1".
Fill_lstSpecs() fires and the ListBox now shows "test1". I leave the page
and come back, Fill_lstSpecs() fires again, but now "test1" doesn't appear.
Restarting the site and loading the page will then show the updated dataset,
so I guess this is a scope problem?

Any diagnosis would be appreciated.

Thanks,
Jordan
 
A

Alex Passos

Everytime I add, update or delete a CabinetSpec on the webform, the ListBox
containing the list of CabinetSpecs updates itself using this function:

-------------------------------------------------------
Public Class CabinetSpec

Public Function List() As DataSet

Alex
 
J

Jordan

Everytime I add, update or delete a CabinetSpec on the webform, the
What happens when the form loads i.e. when you come to the page? Do you
have this function called as well so it can select the data from the
database and refresh the recordset? Just a thought.

The dataset is returned and then re-bound to the listbox on each Page_Load,
Add, Update, and Delete.

Jordan
 
A

Alex Passos

Is the data being updated in the database as well? From your code snipped I
can only see the data being refreshed and updated in place but I don't see a
trip to the database for an update/insert.

Alex
 
J

Jordan

Alex Passos said:
Is the data being updated in the database as well? From your code snipped
I can only see the data being refreshed and updated in place but I don't
see a trip to the database for an update/insert.

Yes, I snipped out the Add, Update and Delete sections. They work
perfectly. I double checked my database after each add, update or delete
and verified that the data is correctly making it to the database.

Something is happening when I leave the page and then return... when
page_load re-fills the listbox it is using an old version of the dataset
somehow.

Jordan
 
A

Alex Passos

Is the dataset allocated on the application level, session level, or page
level? Can you run the debugger and verify that when you come back to the
page the refresh is happening on an empty dataset (which is being populated
from the DB)? I have a feeling that somehow the data set is propagating
between pages (possibility 1)? or page is being chached? (possibility #2).
Have you tried access with different web browsers, clear cache, then access
again?

Lots to check.
Alex
 
J

Jordan

Alex Passos said:
Is the dataset allocated on the application level, session level, or page
level? Can you run the debugger and verify that when you come back to the
page the refresh is happening on an empty dataset (which is being
populated from the DB)? I have a feeling that somehow the data set is
propagating between pages (possibility 1)? or page is being chached?
(possibility #2). Have you tried access with different web browsers, clear
cache, then access again?

Lots to check.
Alex

Yesss!! The page was cached!

Here's what happened: I didn't want to put a link to the new CabinetSpec
page up yet since it was still under construction, so I made a shortcut to
it on my IE links toolbar.
So I guess using a shortcut to a page uses a cached version and therefore
doesn't run the Page_Load sub. I was under the false impression that a page
automatically refreshes every time I visit it.

I put an actual link to get to the page and now the Page_Load sub runs
everytime.

Thanks for helping me troubleshoot, Alex!

Jordan
 
A

Alex Passos

YW! Most times you won't believe that it usually comes down to the simplest
things we take for granted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top