DataGrid problem

Discussion in 'ASP .Net Datagrid Control' started by Swandi Candra, Aug 6, 2003.

  1. 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
     
    Swandi Candra, Aug 6, 2003
    #1
    1. Advertising

  2. 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.


    > Swandi Candra wrote:
    > > Folks,

    > <cut>
    > > 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

    > <etc...>
    > > 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

    >
    > You have to change ALL the OleDb's into Sql's
    > OleDbConnection -> SqlConnection
    > OleDbCommand -> SqlCommand
    > etc...
    >
    > Also change your imports accordingly (System.Data.Sql instead of
    > System.Data.OleDb).
     
    Swandi Candra, Aug 7, 2003
    #2
    1. Advertising

  3. Swandi Candra

    Prasad Guest

    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

    "Swandi Candra" <> wrote in message
    news:...
    > 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.
    >
    >
    > > Swandi Candra wrote:
    > > > Folks,

    > > <cut>
    > > > 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

    > > <etc...>
    > > > 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

    > >
    > > You have to change ALL the OleDb's into Sql's
    > > OleDbConnection -> SqlConnection
    > > OleDbCommand -> SqlCommand
    > > etc...
    > >
    > > Also change your imports accordingly (System.Data.Sql instead of
    > > System.Data.OleDb).
     
    Prasad, Aug 7, 2003
    #3
  4. Swandi Candra

    Leonard Guest

    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
    >>
    >>"Swandi Candra" <> wrote in

    >message
    >>news:...
    >>> 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.
    >>>
    >>>
    >>> > Swandi Candra wrote:
    >>> > > Folks,
    >>> > <cut>
    >>> > > 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
    >>> > <etc...>
    >>> > > 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
    >>> >
    >>> > You have to change ALL the OleDb's into Sql's
    >>> > OleDbConnection -> SqlConnection
    >>> > OleDbCommand -> SqlCommand
    >>> > etc...
    >>> >
    >>> > Also change your imports accordingly

    (System.Data.Sql
    >instead of
    >>> > System.Data.OleDb).

    >>
    >>
    >>.
    >>

    >.
    >
     
    Leonard, Aug 10, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sajvargs
    Replies:
    0
    Views:
    392
    sajvargs
    Feb 20, 2005
  2. DC
    Replies:
    2
    Views:
    624
  3. Andreas Klemt
    Replies:
    0
    Views:
    327
    Andreas Klemt
    Oct 8, 2003
  4. roberto
    Replies:
    1
    Views:
    238
    roberto
    Feb 17, 2005
  5. John Walsch
    Replies:
    0
    Views:
    211
    John Walsch
    Jul 9, 2004
Loading...

Share This Page