locked
Populate the `asp:Panel` in aspnet c# form with value rows selected of MySQL database table RRS feed

  • Question

  • User1036972001 posted

    hi all,

    i need populate the `asp:Panel` in aspnet c# form with value rows selected of MySQL database table

    in this example i have two rows returned form table

        mysql> SELECT
        	dt,
        	hh,
        	dtes,
        	dori,
        	sali
        FROM
        	`tbl_1` jjj
        JOIN `tbl_2` kkk ON jjj.dori = kkk.dori
        WHERE
        	dtes IN ('X250')
        AND dt IN (
        	DATE_SUB(CURDATE(), INTERVAL 1 DAY)
        )
        AND sali = 'S'
        GROUP BY
        	dt,
        	hh,
        	dori;
        +------------+----------+------+-------------+------+
        | dt         | hh       | dtes | dori        | sali |
        +------------+----------+------+-------------+------+
        | 2020-08-14 | 14:15:22 | X250 | X2001383334 | S    |
        | 2020-08-14 | 14:42:48 | X250 | Y2001380456 | S    |
        +------------+----------+------+-------------+------+
        2 rows in set

    this is my code behind for populate the `asp:Panel`

        using (OdbcDataReader reader = command.ExecuteReader())
        {
            int count = 0;
    
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    count++;
    
                    if (count >= 1)
                    {
                        //populate asp Panel;
                        DateTime dt = Convert.ToDateTime(reader["dt"]);
    
                        txdtes.Text = reader["dtes"].ToString();
                        txdt.Text = dt.ToShortDateString();
                        txhh.Text = reader["hh"].ToString();
                        txdori.Text = reader["dori"].ToString();
                        txsali.Text = reader["sali"].ToString();
                    }
                }
            }
        }

    my problem is that in `asp:Panel` is populate only values from first row of query

    but inserting the `while` loop it should not populate the `asp:Panel` with all the rows from the table?

    how to do resolve this?

    thanks in advance for any help and suggestion

    Saturday, August 15, 2020 10:49 AM

Answers

  • User-1519014291 posted

    Hi Koopers,
     For example you may want to display all records like this.

    YourPage.aspx

    <asp:Panel ID="pnl_1" runat="server">
    
     
                    <asp:Repeater runat="server" ID="my_rpt">
                        <ItemTemplate>
    
    
                            <asp:TextBox ID="txdtes" runat="server" Text='<%#Eval("dt") %>'></asp:TextBox>
                            <br />
                            <asp:TextBox ID="txdt" runat="server" Text='<%#Eval("dtes") %>'></asp:TextBox>
                            <br />
                            <asp:TextBox ID="txhh" runat="server" Text='<%#Eval("hh") %>'></asp:TextBox>
                            <br />
                            <asp:TextBox ID="txdori" runat="server" Text='<%#Eval("dori") %>'></asp:TextBox>
                            <br />
                            <asp:TextBox ID="txsali" runat="server" Text='<%#Eval("sali") %>'></asp:TextBox>
    
    
    
    
                        </ItemTemplate>
                    </asp:Repeater>
    
    
    
    
                </asp:Panel>

    The code behind 

    protected void Page_Load(object sender, System.EventArgs e)
    {
        DataTable YourDatatable = new DataTable();
        YourDatatable.Columns.Add("dt", typeof(DateTime));
        YourDatatable.Columns.Add("dtes", typeof(int));
        YourDatatable.Columns.Add("hh", typeof(string));
        YourDatatable.Columns.Add("dori", typeof(string));
        YourDatatable.Columns.Add("sali", typeof(string));
    


    using (OdbcConnection connection = new OdbcConnection("Put the connection string here")) {

    string sqlstr = "SELECT dt, hh, dtes, dori, sali FROM `tbl_1` jjj JOIN `tbl_2` kkk ON jjj.dori = kkk.dori WHERE dtes IN ('X250') AND dt IN ( DATE_SUB(CURDATE(), INTERVAL 1 DAY) ) AND sali = 'S' GROUP BY dt, hh, dori;";

    OdbcCommand command = new OdbcCommand(sqlstr, connection); connection.Open(); OdbcDataReader reader = command.ExecuteReader(); while (reader.Read()) YourDatatable.Rows.Add(reader("dt"), reader("dtes"), reader("hh"), reader("dori"), reader("sali")); reader.Close(); } my_rpt.DataSource = YourDatatable; my_rpt.DataBind(); }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 15, 2020 12:09 PM