Antonio Mailtraq
Antonio Mailtraq

Reputation: 1407

Repeater Control with DataReader and TextBox Control on Panel in c#

In MySQL Database I have this table :

+------------+----+
| IDE        | Of |
+------------+----+
| 01 Line 01 | E  |
| 02 Line 08 | E  |
| 21 Tras 03 | E  |
| 22 Tras 09 | E  |
| 31 Client  | E  |
+------------+----+
5 rows in set

I need to fill with data of this table an aspx page with TextBox Control and Repeater Control:

<asp:Panel ID="pnOf" runat="server">
   <asp:TextBox ID="txOf" runat="server" Enabled="false"></asp:TextBox>
</asp:Panel>
<br />
<asp:Panel ID="pnIDE" runat="server" Visible="false">
   <asp:Repeater ID="rptIDE" runat="server" EnableViewState="true">
      <ItemTemplate>
        <asp:TextBox ID="txIDE" runat="server" Text='<%# Eval("IDE") %>'></asp:TextBox>
      </ItemTemplate>
    </asp:Repeater>
</asp:Panel>

And I have tried with this code-behind :

using (OdbcDataReader reader = cmd.ExecuteReader())
{
    if (reader.HasRows)
    {
        while (reader.Read())
        {
            txOf.Text = reader["Of"].ToString(); 

            rptIDE.DataSource = reader;
            rptIDE.DataBind();

        }
    }

}

But in the output I don't have the first row of database table:

+------------+----+
| IDE        | Of |
+------------+----+
| 01 Line 01 | E  |
+------------+----+

And I have all other rows of database table :

+------------+----+
| IDE        | Of |
+------------+----+
| 02 Line 08 | E  |
| 21 Tras 03 | E  |
| 22 Tras 09 | E  |
| 31 Client  | E  |
+------------+----+

Can anybody help me?

Thanks in advance.

Upvotes: 1

Views: 517

Answers (1)

Adil
Adil

Reputation: 148120

You are assign the reader to DataSource after reading the first record, this is probably reason for missing first row. Assign it before readying.

using (OdbcDataReader reader = cmd.ExecuteReader())
{
    if (reader.HasRows)
    {     
        //txOf.Text = reader["Of"].ToString(); 
        rptIDE.DataSource = reader;
        rptIDE.DataBind();
    }
}

As you are using first row for filling text of txOf.Text you can put data in datatable and get first row data and bind DataTable

using (OdbcDataReader reader = cmd.ExecuteReader())
{
    if (reader.HasRows)
    {     
        var dataTable = new DataTable();
        dataTable.Load(reader); 
        txOf.Text = dataTable.Rows[0]["Of"].ToString();
        rptIDE.DataSource = dataTable;
        rptIDE.DataBind();
    }
}

Upvotes: 1

Related Questions