database paging using asp table+ oledbdataadapter
hi, i waste 3 days looking to how to implement the paging using the objectdatasource and gridView. then i have this idea, which i wana know if it is good or not, and if there is could be any wrong results.
the idea was, why dont i use the asptable, and the DataAdapter.fill method to fill the table with the records that i want to be viewed.
here is the code
Partial Class _Default
Inherits System.Web.UI.Page
Dim dc As New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet .OLEDB.4.0;Data Source=C:\dotnet samples\asp.net\datapager\NWIND.MDB;Persist Security Info=False")
Dim strSQL As String
Dim myDataAdapter As Data.OleDb.OleDbDataAdapter
Dim dstaccounts As New Data.DataSet()
Dim s As Integer
Dim pages As Integer
Dim rowperpages As Integer = 10
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
strSQL = Session("select")
If strSQL <> Nothing Then
myDataAdapter = New Data.OleDb.OleDbDataAdapter(strSQL, dc)
If Not IsPostBack Then
dc.Open()
myDataAdapter.Fill(dstaccounts, "AccountsTable")
Session("s") = dstaccounts.Tables(0).Rows.Count
Session("pages") = Session("s") / rowperpages
If Session("s") - Conversion.Int(Session("pages")) * rowperpages > 0 Then Session("pages") = Session("pages") + 1
Dim i As Integer
For i = 1 To Session("pages")
DropDownList1.Items.Add(i)
Next
dc.Close()
dstaccounts.Clear()
bind_table()
Else
If DropDownList1.Text <> "" Then
Session("hit") = DropDownList1.Text
Else
Session("hit") = 0
End If
bind_table() 'Else
End If
End If
End Sub
Private Sub bind_table()
dc.Open()
Dim x As Integer
x = Session("hit") - 1
If x = -1 Or x = 0 Then
myDataAdapter.Fill(dstaccounts, 0, rowperpages, "AccountsTable")
Else
myDataAdapter.Fill(dstaccounts, x * rowperpages, rowperpages, "AccountsTable")
End If
dc.Close()
Dim ii As Integer
Dim theader As New TableRow
theader.BackColor = Drawing.Color.Blue
Dim tcheader As New TableCell()
tcheader.Text = "number"
theader.Cells.Add(tcheader)
Table1.Rows.Add(theader)
theader.BackColor = Drawing.Color.Blue
Dim tcheader1 As New TableCell()
tcheader1.Text = "name"
theader.Cells.Add(tcheader1)
Table1.Rows.Add(theader)
For ii = 0 To rowperpages
Try
Dim drow As Data.DataRow = dstaccounts.Tables(0).Rows.Item(ii)
Dim tr As New TableRow
Dim tc As New TableCell()
Dim rowcount As Integer
If DropDownList1.Text = 1 Then rowcount = ii + 1 Else rowcount = rowperpages * (DropDownList1.Text - 1) + ii + 1
tc.Text = rowcount
tr.Cells.Add(tc)
''''''
tc = New TableCell()
tc.Text = drow.Item(1).ToString
tr.Cells.Add(tc)
''''''
Dim tc2 As New TableCell()
tc2.Text = drow.Item(2).ToString
tr.Cells.Add(tc2)
tc = New TableCell()
tc.Text = drow.Item(3).ToString tr.Cells.Add(tc)
Dim but As New Button
but.Width = 70
but.Visible = True
but.Text = "view pdf"
but.PostBackUrl = "http://www.yahoo.com?x=" & tc.Text
Dim tc3 As New TableCell
tc3.Controls.Add(but)
tr.Cells.Add(tc3)
Table1.Rows.Add(tr)
Catch ex As Exception
End Try
Next
End Sub
Private Function make(ByVal a As String) As String
Response.Redirect(a)
make = "1"
End Function
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
End Sub
End Class
''end of code
i added a button to the table so i can view the details of the item i need, and the the dropdownlist is used to navigate the results.
now you need to open this page from another page, to have a results, the page must assign the sql statment to a session variable Session("sql").
i tested this code and it work good and fast.
please tell me if it is ok to use or not
thank you
Yamen Saleh
|