DataTable Selecting!

jesus4u

Well-known member
Joined
Feb 13, 2003
Messages
47
I want to cache a table that I can Query using different criteria.

Why do I get the error:

Value of type 1-dimensional array of System.Data.DataRow cannot be converted to System.Data.DataRow.

When I try to compile this code?

Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Cache.Item("AllTranscripts") Is Nothing Then
            Dim ds As New DataSet
            ds = dataAccess.ExecuteDataset(conn.ConnectionString, CommandType.StoredProcedure, "GetAllTranscripts")
            Cache.Add("AllTranscripts", ds.Tables(0), Nothing, DateTime.Now.AddMinutes(30), TimeSpan.Zero, CacheItemPriority.Low, Nothing)
            Dim dtNew As DataTable
            dtNew = Cache.Item("AllTranscripts")
            Dim drTranscriptSummary As DataRow
            drTranscriptSummary = dtNew.Select("TranscriptsID = 681")
            DataGrid1.DataSource = drTranscriptSummary
            DataGrid1.DataBind()
        Else
            DataGrid1.DataSource = Cache.Item("AllTranscripts")
            DataGrid1.DataBind()
        End If

    End Sub
 
IIRC DataTable.Select returns an array of rows so you need to declare your variable as an array.

Code:
Dim drTranscriptSummary() As DataRow    Changed this line
drTranscriptSummary = dtNew.Select("TranscriptsID = 681")
 
ok got it to filter and bind BUT how can I bind where the one record fills the Grid in one row? The way it is now it loads all the data into one column.

Code:
            Dim ds As New DataSet
            ds = dataAccess.ExecuteDataset(conn.ConnectionString, CommandType.StoredProcedure, "GetAllTranscripts")
            Cache.Add("AllTranscripts", ds.Tables(0), Nothing, DateTime.Now.AddMinutes(30), TimeSpan.Zero, CacheItemPriority.Low, Nothing)
            drTranscriptSummary = QueryDataTable("TranscriptsID = 681")
            DataGrid1.DataSource = drTranscriptSummary(0).ItemArray
            DataGrid1.DataBind()
 
I would use a DataView, as in:
Code:
Dim ds As New DataSet
ds = dataAccess.ExecuteDataset(conn.ConnectionString, CommandType.StoredProcedure, "GetAllTranscripts")

 I dont remember the enum for the last param, might not be 
 DataViewRowState... check the definition of the DataView constructor
Dim dv As DataView = New DataView(ds.Tables(0), "TranscriptsID = 681", Nothing, DataViewRowState.CurrentRows)
DataGrid1.DataSource = dv
DataGrid1.DataBind()

You can create as many DataViews as you want from one Table. Each can have its own filter and sort. In the above, I passed Nothing for the sort - but you can pass a comma delimited list along with "asc" or "desc" for each column.

-Nerseus
 
Back
Top