DataGrid problem

S

Swandi Candra

Folks,

I found the following example from dotnetjunkies, which worked
fine.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<html>
<head>
<title>ASPNextGen.com - Paging in the DataGrid Part 1</title>
<script runat="server" language="VB">
Sub Page_Load(Source As Object, E As EventArgs)
If Not Page.IsPostBack Then
BindData()
End If
End Sub

Sub BindData()
Dim ds As New DataSet
Dim dsc As SQLDataAdapter
Dim strSQL As String
Dim strCon As String

strSQL = "SELECT CompanyName, ContactName, " & _
" ContactTitle, Phone, Fax FROM Customers " & _
" ORDER BY CompanyName"
strCon = "server=localhost;database=Northwind;uid=sa;pwd=;"

dsc = New SQLDataSetCommand(strSQL, strCon)
dsc.Fill(Ds, "Customers")

myDataGrid.DataSource = ds.Tables("Customers").DefaultView
myDataGrid.DataBind()
End Sub

Sub myDataGrid_PageChanger(Source As Object, _
E As DataGridPageChangedEventArgs)
myDataGrid.CurrentPageIndex = E.NewPageIndex
BindData()
End Sub
</script>
<style>
.DataGrid {font:x-small Verdana, Arial, sans-serif}
</style>
</head>

<body>
<form runat="server" method="post">
<asp:DataGrid runat="server" id="myDataGrid"
Border="0"
Cellpadding="4"
Cellspacing="0"
AlternatingItemStyle-BackColor="#EFEFEF"
ShowHeader="True"
CssClass="DataGrid"
HeaderStyle-BackColor="Black"
HeaderStyle-ForeColor="White"
HeaderStyle-Font-Bold="True"
AllowPaging="True"
PageSize="10"
PagerStyle-Mode="NumericPages"
OnPageIndexChanged="myDataGrid_PageChanger"
/>
</form>
</body>
</html>

However, it used SqlClient as its provider, instead of OleDb.
Therefore, I changed the binding part as follow

Sub BindData()
Dim conn As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim strSQL As String
Dim strCon As String

strSQL = "SELECT CompanyName, ContactName, " & _
" ContactTitle, Phone, Fax FROM Customers " & _
" ORDER BY CompanyName"
strCon = "server=localhost;database=Northwind;uid=sa;pwd=;"

conn = New OleDbConnection(strCon)
conn.open

cmd = new OleDbCommand()
cmd.Connection = conn
cmd.CommandText = strSQL

myDataGrid.DataSource = cmd.ExecuteReader()
myDataGrid.DataBind()
End Sub

When I ran it, the ASP.NET displayed an error indicating that
I should include AllowCustomPaging=True property in asp:DataGrid.
After I added it, the list can be displayed property, except with
no paging.

Does anyone know what's going on?
Can anyone suggest me, how to do it properly with OleDb provider?

Thanks for your help.
SC
 
S

Swandi Candra

Jos,

I think, you missed my points.
Please read it carefully again.

What I'm asking is, how can use OleDB provider
for DataGrid's data source. I don't want to use
SqlConnection as it will prevent you from use
non Microsoft SQL Server database.
 
P

Prasad

Hi

DataReader does not support Automatic Paging. That's why it is throwing
error. Use OleDbAdapter and Dataset as in the Example and it will work.

HTH
Prasad
 
L

Leonard

Oh,yes. To use OLEDB you have to use the OleDbDataReader
class instead of SqlDataReader, of course.
-----Original Message-----
Jos,

You can use the DataReader and with paging as follows:

// the DataGrid is dg
SqlConnection connObject;
SqlCommand cmdObject;
SqlDataReader rdr;

connObject.ConnectionString = "[whatever]";
connObject.Open();
string strCMD = "Select count(on some column like the
PK) as nbrRows FROM [your table or whatever]";
cmdObject = new SqlCommand(strCMD, connObject);
cmdObject.CommandType = CommandType.Text;
rdr = cmdObject.ExecuteReader();
if (!rdrDocs.HasRows)
throw new [Your app exception - catch it below];
rdr.Read();
int nbr = Convert.ToInt32(rdrDocs["nbrRows"]);
rdr.Close();

strCMD = "SELECT [your projection] FROM [your table or
whatever]";
cmdObject.CommandText = strCMD;

rdr = cmdObject.ExecuteReader();
if (!rdr.HasRows)
throw new [Your app exception - catch it below];

dg.DataSource = rdr;
dg.VirtualItemCount = nbr; // This sets the number of
rows in grid
dg.DataBind();
rdr.Close();

This works for the Sql... variety. I haven't tried it
with the OleDb... flavor, but I assume it works. Try it
and let us know.
-----Original Message-----
Hi

DataReader does not support Automatic Paging. That's why it is throwing
error. Use OleDbAdapter and Dataset as in the Example
and
it will work.
HTH
Prasad
instead
of OleDb. in
asp:DataGrid.
(System.Data.Sql
instead of
.
 

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

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top