Showing posts with label binding. Show all posts
Showing posts with label binding. Show all posts

Monday, March 26, 2012

Strange DataView Problem

Hello,

I need some clarification about a stange DataView behaviour I have noticed. I am developing a dynamic website that is binding data (from an Access db) to a datagrid. I use a cached dataset to save trips to the db, here is the code...

'populate the Cache Variable that stores the Web Content Dataset
Public Function PopulateWebContentDataset(Optional ByVal blnRepopulate As Boolean = False) As Boolean

Dim ds As New DataSet()

'check if cached dataset already exists
If Current.Cache("WebContent") Is Nothing = False Then
'the cached variable already exists
Return True
End If

'get the Web Content from the database
Dim cn As New OleDbConnection(AppSettings("cnStringWebContent"))
Dim da As New OleDbDataAdapter("SELECT * FROM tblWebContent", cn)

Try
'fill the Web Content dataset with records from the database
da.Fill(ds, "tblWebContent")
If ds.Tables("tblWebContent").Rows.Count > 0 Then
'add the dataset to the cache
Current.Cache.Insert("WebContent", ds, _
New System.Web.Caching.CacheDependency(AppSettings("dbPathWebContent")), _
DateTime.Now.AddDays(1), Current.Cache.NoSlidingExpiration, _
System.Web.Caching.CacheItemPriority.Normal, Nothing)
Return True
Else
'no records were found
Current.Cache.Remove("WebContent")
Current.Cache("WebContent") = Nothing
Return False
End If

Catch DefaultExc As Exception

Finally
cn.Dispose()
da.Dispose()
ds.Dispose()
End Try

End Function

Public Function BindPageContent(ByRef dgr As DataGrid, ByVal strPage As String, _
Optional ByVal blnRepopulate As Boolean = False) As Boolean
'check if the cache Web Content dataset already exists
If PopulateWebContentDataset(blnRepopulate) = False And blnRepopulate = False Then
'unable to get the information from the database
Return False
End If

Dim ds As DataSet
Dim dv As New DataView()

Try

ds = Current.Cache("WebContent")
'ds.Tables("tblWebContent").DefaultView.RowFilter = "Page=" & "'" & strPage & "'"
dv = ds.Tables("tblWebContent").DefaultView
dv.RowFilter = "Page=" & "'" & strPage & "'"
dgr.DataSource = dv
dgr.DataBind()

Catch DefaultExc As Exception

Finally
ds.Dispose()
dv.Dispose()
End Try

End Function

My question is, the above code works perfectly the first time the page loads but when I return to this specific page (which should be getting the cached dataset), nothing is displayed. I checked the dataset and it has the correct amount of rows, however, the dataview has a rowcount of 0 (when it previously had a rowcount of 1).

I have done the following to fix this problem, all of which have worked...
1. Instead of getting the dataset from the cache, I connected to the db on each page_load and repopulated the dataset
2. When I bind the datagrid using the dataset as the datasource instead of the dataview, it works...

(ds.tables("tblWebContent").defaultview.rowfilter="Page='" & strPage & "'")

3. When I take out dv.dispose, everything works as expected

I have many instances in my code where I use the exact same code as posted above and it doesn't seem like there is a problem with the DataView. Should I re-examine all my code and remove dataview.dispose()? Does anyone have any suggestions on why this may be happening?Indeed, you shouldn't dispose that object since it's needed when the page execution is done further to display the data on the web form. In fact, it's a strange problem for me too but I would never dispose the dataview... Remember, the dataset object is a disconnected object which doesn't need any connection whatsoever with the database.

Saturday, March 24, 2012

Strange error binding DataTable to DropDownList: out of range... Parameter name: value

I'm getting a nonsensical error when binding a DataTable with one row
to a DropDownList.

The DataTable's row contains the fields "id" (INT), "description"
(VARCHAR) and with a couple other unused fields.

I'm binding the table in code behind:

DataTable table=Publishinggroup.GetPublisheegroups(person.Id );
ddlPublishSource.DataSource=table;
ddlPublishSource.DataBind();

And in the aspx page, it's:

<asp:DropDownList ID="ddlPublishSource" runat="server"
DataValueField="id" DataTextField="description" /
But the error that occurs on the DataBind() command is nonsensical,
because there are no column names and no values with a value of
"value":

Specified argument was out of the range of valid values. Parameter
name: value

Any ideas on what might cause that?

Thanks,

-MikeHi-

Ok, I found the problem--the drop down list was having a value
preselected, before the datasource was assigned, but that value was no
longer in the datatable. That was why the bind was failing.

-Mike

On Wed, 07 Jul 2004 16:21:47 GMT, Mike Bridge <mike@.bridgecanada.com>
wrote:

>I'm getting a nonsensical error when binding a DataTable with one row
>to a DropDownList.

Strange error binding DataTable to DropDownList: out of range... Parameter name: valu

I'm getting a nonsensical error when binding a DataTable with one row
to a DropDownList.
The DataTable's row contains the fields "id" (INT), "description"
(VARCHAR) and with a couple other unused fields.
I'm binding the table in code behind:
DataTable table=Publishinggroup.GetPublisheegroups(person.Id);
ddlPublishSource.DataSource=table;
ddlPublishSource.DataBind();
And in the aspx page, it's:
<asp:DropDownList ID="ddlPublishSource" runat="server"
DataValueField="id" DataTextField="description" />
But the error that occurs on the DataBind() command is nonsensical,
because there are no column names and no values with a value of
"value":
Specified argument was out of the range of valid values. Parameter
name: value
Any ideas on what might cause that?
Thanks,
-MikeHi-
Ok, I found the problem--the drop down list was having a value
preselected, before the datasource was assigned, but that value was no
longer in the datatable. That was why the bind was failing.
-Mike
On Wed, 07 Jul 2004 16:21:47 GMT, Mike Bridge <mike@.bridgecanada.com>
wrote:

>I'm getting a nonsensical error when binding a DataTable with one row
>to a DropDownList.