i hava retrieve the data from database to text box but i am getting only one row from the table i want to retrieve all the rows of a table in the text boxes how it is possible please help me sir.

Dani AI

Generated

asked why only one row appears in a TextBox. correctly suggested iterating rows; asked for code and suggested better titles. Below are focused, practical checks and safer patterns that avoid common pitfalls and scale better than repeatedly assigning a control inside a loop.

Start-of-troubleshooting checklist:

  • Verify the SQL actually returns multiple rows (run the query in SSMS or log row count).
  • Confirm the data-access code reads every row (a single Read() or accessing Rows[0] will return just one).
  • Avoid overwriting the control each iteration; build the full result in memory and assign once for better correctness and UI performance.
  • Prefer a control suited to tabular data (DataGridView/ListBox) rather than manually creating many TextBox controls.

Efficient patterns (examples here use different techniques than earlier replies). Accumulate text and assign once:

var sb = new System.Text.StringBuilder();
foreach (System.Data.DataRow r in dt.Rows)
{
    sb.AppendLine(r[0].ToString()); // or use column name
}
textBox1.Text = sb.ToString();

If using a DataReader:

using (var cmd = new SqlCommand(sql, conn))
using (var rdr = cmd.ExecuteReader())
{
    var sb = new System.Text.StringBuilder();
    while (rdr.Read())
    {
        sb.AppendLine(rdr[0].ToString());
    }
    textBox1.Text = sb.ToString();
}

When each database row must become its own TextBox, create controls dynamically and add them to a container (Panel/TableLayoutPanel) instead of hardcoding many controls. For tabular displays, bind the DataTable directly:

dataGridView1.DataSource = dt;

Cautions: avoid creating dozens of controls for large result sets; use paging or a grid. If retrieval runs off the UI thread, marshal updates to the UI thread (Invoke/BeginInvoke or async/await). These steps cover the common causes of seeing a single row and give better patterns for multi-row output.

Recommended Answers

All 3 Replies

without seeing your code its very hard to see what you might need to change.
Please post any code relevant to your question in [code]

[/code] tags.


As a sidenote, I see you have several threads named C#.
Please give your threads a more appropriate name.
It will help us all.:)

As some one else has mentioned include your code. However what I guess you could do is the following:

foreach (datarow dr in dt.rows)
{
txtTextbox.text = txtTextBox.text + "\n" dr[0].tostring() ; 
}

what this does is loop through your rows and adds them to the text box adding a new line as they go denoted by the "\n". Note however that this will only work if you have multiline set to true.

Try this I can't quite remember all the ins and outs and you will have to correct the capital letters etc...

--Taylby

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.