Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
HTML
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label></td>
    <td> 
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </td>
    </tr>
    </table>
    </div>
    </form>
</body>
</html>


---------------
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Default4 : System.Web.UI.Page
{  
    protected void Page_Load(object sender, EventArgs e)
    {     
        SqlConnection con = new SqlConnection
        (@"Data Source=DESKTOP-UT3J1JD\SQL14ONUR;
         Initial Catalog=onurgridview;
         Integrated Security=True;MultipleActiveResultSets=True;
         Connection Timeout=900");

        String str = "select * from country";
        SqlCommand cmd = new SqlCommand(str, con);
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            TextBox1.Text = dr["countryid"].ToString();
            Label1.Text = dr["photo"].ToString();
        }
        con.Close();
    }
}


What I have tried:

The code I sent is working smoothly, but I have 5 data records and I see only one of them during run-time. I think I need to produce 5 textboxes and labels for the list. I couldn't figure out whether this should be done on the back-end side or the web designer side. I am a teacher. I defined a label and textbox in the web designer section, but I couldn't do it. Solution?
Posted
Updated 2-Oct-23 3:44am
v2

1 solution

Simple: you are overwriting the single textbox and label pair ever time you process a new row.
So the only row you will ever see is the final one of the set.

If you need to always display 5 items, then add four more Textboxes and four more labels, and use them instead of a single pair.

If you need a variable number, then don't use individual Textboxes and Labels: use a grid based control instead and set it's DataSource as the DataReader (or better use a DataAdapter and a DataTable instead).
 
Share this answer
 
Comments
[no name] 26-Sep-23 17:15pm    
I forgot to mention that I do not want to use grid-based controls. Sir, my data row size is not always 5. I wanted to have as many textbox and label elements as the number of data records, rather than a variable grid-based control. I do not want to use repeater gridview buildingstring literal and similar structures. I guess I use textbox and label expressions as many as the number of data. In the run-time web designer section, I reasoned that it should be produced in a for loop, but I could not do it.
OriginalGriff 27-Sep-23 1:34am    
If you are dynamically adding textboxes and labels - and that's not difficult - then you are effectively creating your own grid based control, but giving yourself a lot more hassle at the same time. Seriously, a grid-based control is there to make your life easier, and prevent you having to do it all yourself!

But ... in a loop, just create the controls and call Controls.Add to add them to the HTML when it's is rendered: I'd suggest adding a panel to hold all of them, so it's easier to ensure they all fit in the same area rather than tacked onto the end of the form.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900